class Laminar::Flow::Branch

Specification for a target rule transition.

Attributes

condition[RW]

@!attribute name

@return target rule to branch to

@!attribute condition

@return the branch condition (method name symbol or Proc/lambda)
condition_type[RW]

@!attribute name

@return target rule to branch to

@!attribute condition

@return the branch condition (method name symbol or Proc/lambda)
name[RW]

@!attribute name

@return target rule to branch to

@!attribute condition

@return the branch condition (method name symbol or Proc/lambda)

Public Class Methods

new(name, options = {}) click to toggle source
# File lib/laminar/flow/branch.rb, line 20
def initialize(name, options = {})
  raise ArgumentError, 'invalid name' unless name.class.method_defined?(:to_sym)

  validate_options(options)
  @name = name.to_sym
  define_condition(options)
end

Public Instance Methods

meets_condition?(target) click to toggle source

@param [RuleBase] context a given rule implementation

@return [Boolean] true if condition is satisfied in the context.

# File lib/laminar/flow/branch.rb, line 31
def meets_condition?(target)
  return true if condition.nil?

  result = run_condition(target)
  condition_type == :if ? result : !result
end

Private Instance Methods

define_condition(options) click to toggle source
# File lib/laminar/flow/branch.rb, line 48
def define_condition(options)
  @condition_type = (options.keys & %i[if unless]).first
  return if @condition_type.nil?

  @condition = options[@condition_type]
  return if @condition.nil? || @condition.is_a?(Symbol) || @condition.is_a?(Proc)

  raise TypeError, 'condition must be a method symbol or Proc).'
end
run_condition(target) click to toggle source
# File lib/laminar/flow/branch.rb, line 40
def run_condition(target)
  if @condition.is_a?(Proc)
    @condition.call(target.context)
  else
    target.send(@condition)
  end
end