Module: Yasm::Manager

Defined in:
lib/yasm/manager.rb

Class Method Summary collapse

Class Method Details

.change_state(options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/yasm/manager.rb', line 5

def change_state(options)
  new_state       = options[:to]
  state_container = options[:on]
  
  raise(
    Yasm::TimeLimitNotYetReached,
    "We're sorry, but the time limit on the state `#{state_container.state}` has not yet been reached."
  ) if state_container.state and !state_container.state.reached_minimum_time_limit?
  
  new_state = new_state.to_class if new_state.respond_to? :to_class
  new_state = new_state.new
  new_state.instantiated_at = Time.now

  state_container.state = new_state
end

.execute(options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/yasm/manager.rb', line 21

def execute(options)
  context = options[:context]
  actions = options[:actions]
  state_container = options[:state_container]

  actions.each do |action|
    action                  = action.new if action.class == Class
    action.context          = context
    action.state_container  = state_container
    

    # Verify that the action is possible given the current state
    if state_container.state.class.final?
      raise Yasm::FinalStateException, "We're sorry, but the current state `#{state_container.state}` is final. It does not accept any actions."
    elsif !state_container.state.class.is_allowed?(action.class)
      raise Yasm::InvalidActionException, "We're sorry, but the action `#{action.class}` is not possible given the current state `#{state_container.state}`." 
    end

    change_state :to => action.triggers.to_class, :on => state_container if action.triggers 
    action.execute 
  end
end