Module: Statesman::Events

Defined in:
lib/statesman/events.rb,
lib/statesman/events/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
# File 'lib/statesman/events.rb', line 6

def self.included(base)
  unless base.respond_to?(:states)
    raise "Statesman::Events included before/without Statesman::Machine"
  end
  base.extend(ClassMethods)
end

Instance Method Details

#available_eventsObject



44
45
46
47
48
49
# File 'lib/statesman/events.rb', line 44

def available_events
  state = current_state
  self.class.events.select do |_, transitions|
    transitions.key?(state)
  end.map(&:first)
end

#trigger(event_name, metadata = {}) ⇒ Object



38
39
40
41
42
# File 'lib/statesman/events.rb', line 38

def trigger(event_name,  = {})
  self.trigger!(event_name, )
rescue Statesman::TransitionFailedError, Statesman::GuardFailedError
  false
end

#trigger!(event_name, metadata = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/statesman/events.rb', line 23

def trigger!(event_name,  = {})
  transitions = self.class.events.fetch(event_name) do
    raise Statesman::TransitionFailedError,
          "Event #{event_name} not found"
  end

  new_state = transitions.fetch(current_state) do
    raise Statesman::TransitionFailedError,
          "State #{current_state} not found for Event #{event_name}"
  end

  transition_to!(new_state.first, )
  true
end