Sha256: e4d70e7d01d5610ed02d619660344a64730dd06a8a70234f77cad0eee874c34a

Contents?: true

Size: 944 Bytes

Versions: 2

Compression:

Stored size: 944 Bytes

Contents

class RailsOps::Hookup::DSLValidator
  attr_reader :error
  attr_reader :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

2 entries across 2 versions & 1 rubygems

Version Path
rails_ops-1.4.3 lib/rails_ops/hookup/dsl_validator.rb
rails_ops-1.4.2 lib/rails_ops/hookup/dsl_validator.rb