Class: Laminar::Flow::Branch

Inherits:
Object
  • Object
show all
Includes:
OptionsValidator
Defined in:
lib/laminar/flow/branch.rb

Overview

Specification for a target rule transition.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OptionsValidator

included

Constructor Details

#initialize(name, options = {}) ⇒ Branch

Returns a new instance of Branch.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
# 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

Instance Attribute Details

#conditionObject

Returns the branch condition (method name symbol or Proc/lambda).

Returns:

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



18
# File 'lib/laminar/flow/branch.rb', line 18

attr_accessor :name, :condition, :condition_type

#condition_typeObject

Returns the value of attribute condition_type.



18
19
20
# File 'lib/laminar/flow/branch.rb', line 18

def condition_type
  @condition_type
end

#nameObject

Returns target rule to branch to.

Returns:

  • target rule to branch to



18
19
20
# File 'lib/laminar/flow/branch.rb', line 18

def name
  @name
end

Instance Method Details

#meets_condition?(target) ⇒ Boolean

Returns true if condition is satisfied in the context.

Parameters:

  • context (RuleBase)

    a given rule implementation

Returns:

  • (Boolean)

    true if condition is satisfied in the context.



31
32
33
34
35
36
# 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