Sha256: ac0cd71fc1eb163e7d0d989f7748902a2ec00ed496cdd58c24d044af5541a897

Contents?: true

Size: 931 Bytes

Versions: 76

Compression:

Stored size: 931 Bytes

Contents

class RailsOps::Hookup::DSLValidator
  attr_reader :error, :trace

  def initialize(hooks)
    @hooks = hooks
    @trace = []
  end

  def validate!
    # Check infinity loop
    if target_hooks.any? { |name, targets| recursion?(targets, name) }
      fail StandardError::SystemStackError.new, "Infinite loop detected in hooks configuration: #{inspect_trace}."
    end
  end

  def target_hooks
    @target_hooks ||= @hooks.map do |name, hash|
      [name, drilldown(hash)]
    end.to_h
  end

  private

  def inspect_trace
    @trace.map(&:to_s).join(' ~> ')
  end

  def recursion?(targets, name)
    if targets.include? name
      @trace << name.to_s
      return true
    end

    return targets.any? do |target|
      if @hooks[target]
        @trace |= [name, target]
        recursion? drilldown(@hooks[target]), name
      end
    end
  end

  def drilldown(hash)
    hash.values.flatten.map(&:target_operation)
  end
end

Version data entries

76 entries across 76 versions & 1 rubygems

Version Path
rails_ops-1.1.19 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.18 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.17 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.16 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.15 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.14 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.13 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.12 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.11 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.10 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.9 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.8 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.7 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.6 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.5 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.4 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.3 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.2 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.1 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.0 lib/rails_ops/hookup/dsl_validator.rb