Class: StateFlow::NamedEvent

Inherits:
Event show all
Defined in:
lib/state_flow/named_event.rb

Instance Attribute Summary collapse

Attributes inherited from Element

#destination, #origin

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExceptionHandlerClient

#exception_handlers, #exception_handling

Methods included from ActionClient

#action

Methods included from GuardClient

#guard, #guard_else, #guard_for, #guards

Methods inherited from Element

#flow, #inspect, #retry_in_recovering, #state, #to, uninspected_var, uninspected_vars, #update_to_destination

Methods included from ElementVisitable

#visit

Constructor Details

#initialize(origin, name, &block) ⇒ NamedEvent

Returns a new instance of NamedEvent.



7
8
9
10
# File 'lib/state_flow/named_event.rb', line 7

def initialize(origin, name, &block)
  @name = name
  super(origin, &block)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/state_flow/named_event.rb', line 6

def name
  @name
end

Class Method Details

.build_event_methods(flow) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/state_flow/named_event.rb', line 19

def build_event_methods(flow)
  named_events = []
  flow.all_states.each do |state_name, state|
    state.visit do |event|
      named_events << event if event.is_a?(NamedEvent)
      [:events, :guards, :action]
    end
  end
  method_name_to_events = {}
  named_events.each do |ev|
    method_name_to_events[ev.name] ||= []
    method_name_to_events[ev.name] << ev
  end
  method_name_to_events.each do |name, events|
    flow.klass.module_eval do
      # イベントを通知するときに呼び出されるメソッド
      define_method(name) do |*args|
        result = nil
        events.each do |event|
          if event.state.include?(self.send(flow.attr_key_name))
            context = flow.prepare_context(self, args.first)
            self.state_flow_contexts[flow.attr_name] = context
            result = context.process(event)
            break
          end
        end
        result # return
      end

    end
  end
end

Instance Method Details

#process(context) ⇒ Object



12
13
14
15
16
# File 'lib/state_flow/named_event.rb', line 12

def process(context)
  state.exception_handling(context) do
    super
  end
end