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.0.17.1 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.4.1 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.4.0 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.3.0 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.2.3 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.2.2 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.2.1 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.2.0 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.31 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.30 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.29 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.28 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.27 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.26 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.25 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.24 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.23 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.22 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.21 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.1.20 lib/rails_ops/hookup/dsl_validator.rb