Class: Rule::Engine::Base

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column) ⇒ Base

Returns a new instance of Base.



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

def initialize(column)
  @column = column
end

Class Method Details

.initial_state(state) ⇒ Object



13
14
15
# File 'lib/rule/engine/base.rb', line 13

def self.initial_state(state)
  @initial_state = find_or_create_state(state)
end

.state(state) ⇒ Object



9
10
11
# File 'lib/rule/engine/base.rb', line 9

def self.state(state)
  find_or_create_state(state)
end

.terminal_state(state) ⇒ Object



17
18
19
20
# File 'lib/rule/engine/base.rb', line 17

def self.terminal_state(state)
  @terminal_states ||= []
  @terminal_states << find_or_create_state(state)
end

.transition(from, to, &blk) ⇒ Object



22
23
24
25
26
27
# File 'lib/rule/engine/base.rb', line 22

def self.transition(from, to, &blk)
  from_state = find_state!(from)
  to_state   = find_state!(to)

  from_state.add_transition(to_state, blk)
end

Instance Method Details

#run!(object) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rule/engine/base.rb', line 29

def run!(object)
  # state = self.class.find_state(object.send(@column).try(:to_sym)) || self.class.instance_variable_get("@initial_state")
  # Revalidate from initial state each time. This is more along the lines of the behaviour dave wants.
  state = self.class.instance_variable_get("@initial_state")
  loop do
    prev_state = state
    state = prev_state.next_state(object)
    break if prev_state == state
  end

  object.send("#{@column}=", state.name)
end