Class: Ruleby::Rulebook

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#__eval__(x) ⇒ Object



93
94
95
# File 'lib/rulebook.rb', line 93

def __eval__(x)
  eval(x)
end

#AND(*args) ⇒ Object



89
90
91
# File 'lib/rulebook.rb', line 89

def AND(*args)
  Ruleby::Ferrari::AndBuilder.new args
end

#assert(fact) ⇒ Object



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

def assert(fact)
  @engine.assert fact
end

#b(variable_name) ⇒ Object



69
70
71
# File 'lib/rulebook.rb', line 69

def b(variable_name)
  Ruleby::Ferrari::BindingBuilder.new(variable_name)
end

#binding(variable_name) ⇒ Object



73
74
75
# File 'lib/rulebook.rb', line 73

def binding(variable_name)
  b variable_name
end

#c(&block) ⇒ Object



77
78
79
# File 'lib/rulebook.rb', line 77

def c(&block)
  return lambda(&block)
end

#condition(&block) ⇒ Object



81
82
83
# File 'lib/rulebook.rb', line 81

def condition(&block)
  return lambda(&block)
end

#mObject



61
62
63
# File 'lib/rulebook.rb', line 61

def m
  Ruleby::Ferrari::MethodBuilder.new
end

#methodObject



65
66
67
# File 'lib/rulebook.rb', line 65

def method
  m
end

#modify(fact) ⇒ Object



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

def modify(fact)
  @engine.modify fact
end

#OR(*args) ⇒ Object



85
86
87
# File 'lib/rulebook.rb', line 85

def OR(*args)
  Ruleby::Ferrari::OrBuilder.new args
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
55
56
57
58
59
# File 'lib/rulebook.rb', line 36

def rule(*args, &block)
  unless args.empty?
    name = args[0].kind_of?(Symbol) ? args.shift : GeneratedTag.new
  end

  if args.empty?
    # use steel DSL
    r = Steel::RulebookHelper.new @engine
    r.rule name, &block
  else
    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