Class: Ruleby::Rulebook

Inherits:
Object
  • Object
show all
Includes:
Ruleby, RuleHelper
Defined in:
lib/rulebook.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RuleHelper

#AND, #OR, #__eval__, #b, #c, #f, #m, #method

Constructor Details

#initialize(engine) {|_self| ... } ⇒ Rulebook

Returns a new instance of Rulebook.

Yields:

  • (_self)

Yield Parameters:



20
21
22
23
# File 'lib/rulebook.rb', line 20

def initialize(engine, &block)
  @engine = engine
  yield self if block_given?
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



25
26
27
# File 'lib/rulebook.rb', line 25

def engine
  @engine
end

Instance Method Details

#assert(fact) ⇒ Object



27
28
29
# File 'lib/rulebook.rb', line 27

def assert(fact)
  @engine.assert fact
end

#modify(fact) ⇒ Object



33
34
35
# File 'lib/rulebook.rb', line 33

def modify(fact)
  @engine.modify fact
end

#retract(fact) ⇒ Object



30
31
32
# File 'lib/rulebook.rb', line 30

def retract(fact)
  @engine.retract fact
end

#rule(*args, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rulebook.rb', line 36

def rule(*args, &block)
  if args.empty?
    raise 'Must provide arguments to rule'
  else
    name = args[0].kind_of?(Symbol) ? args.shift : GeneratedTag.new
    i = args[0].kind_of?(Hash) ? 1 : 0
    if [Array, Ruleby::Ferrari::OrBuilder, Ruleby::Ferrari::AndBuilder].include? args[i].class
      # use ferrari DSL
      r = Ferrari::RulebookHelper.new @engine
      r.rule name, *args, &block
    elsif args[i].kind_of? String
      # use letigre DSL
      r = LeTigre::RulebookHelper.new @engine, self
      r.rule name, *args, &block
    else
      raise 'Rule format not recognized.'
    end
  end
end