Class: Brule::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/brule/engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules:) ⇒ Engine

Returns a new instance of Engine.



5
6
7
# File 'lib/brule/engine.rb', line 5

def initialize(rules:)
  @rules = rules
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



3
4
5
# File 'lib/brule/engine.rb', line 3

def context
  @context
end

Instance Method Details

#call(context = {}) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/brule/engine.rb', line 9

def call(context = {})
  @history = {}
  @context = Context.wrap(context)
  snapshot!(tag: :initial)
  @rules.each { |rule| apply(rule) }
  result
end

#history(key:) ⇒ Object



17
18
19
20
21
# File 'lib/brule/engine.rb', line 17

def history(key:)
  return [] unless defined?(@history)

  @history.map { |tag, content| [tag, content.fetch(key, nil)] }
end