Class: Ruleby::LeTigre::RulebookHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl/letigre.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, rulebook) ⇒ RulebookHelper

Returns a new instance of RulebookHelper.



16
17
18
19
# File 'lib/dsl/letigre.rb', line 16

def initialize(engine, rulebook)
  @rulebook = rulebook
  @engine = engine
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



21
22
23
# File 'lib/dsl/letigre.rb', line 21

def engine
  @engine
end

Instance Method Details

#rule(name, *args, &then_block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dsl/letigre.rb', line 23

def rule(name, *args, &then_block)
  if args.empty?
    raise 'No conditions supplied.'
  end

  options = args[0].kind_of?(Hash) ? args.shift : {}

  pb = PatternParser.new @rulebook
  pattern = pb.parse args
    
  rb = RuleBuilder.new name
  
  rb.when(pattern)
  rb.then(&then_block)
  rb.priority = options[:priority] if options[:priority]
  
  @engine.assert_rule rb.build_rule
end