Module: StateFlow::EventClient

Includes:
ExceptionHandlerClient
Included in:
Action, Guard, State
Defined in:
lib/state_flow/event_client.rb

Instance Method Summary collapse

Methods included from ExceptionHandlerClient

#exception_handlers, #exception_handling

Instance Method Details

#action_event(matcher, &block) ⇒ Object



35
36
37
38
39
# File 'lib/state_flow/event_client.rb', line 35

def action_event(matcher, &block)
  result = ActionEvent.new(self, matcher, &block)
  events << result
  result
end

#event(*name_or_matcher_or_exceptions, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/state_flow/event_client.rb', line 12

def event(*name_or_matcher_or_exceptions, &block)
  options = name_or_matcher_or_exceptions.extract_options!
  if name_or_matcher_or_exceptions.all?{|v| v.is_a?(Class) && v <= Exception}
    handle_exception(*name_or_matcher_or_exceptions.push(options), &block)
  else
    if name_or_matcher_or_exceptions.length > 1
      raise ArgumentError, "event(event_name) or event(action_result) or event(Exception1, Exception2...)"
    end
    name_or_matcher = name_or_matcher_or_exceptions.first
    if origin.is_a?(State)
      named_event(name_or_matcher, &block)
    else
      action_event(name_or_matcher, &block)
    end
  end
end

#event_else(&block) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/state_flow/event_client.rb', line 41

def event_else(&block)
  if origin.is_a?(State)
    raise ArgumentError, "event_else can't be after/in a state but an action"
  end
  result = ActionEvent.new(self, ActionEvent::ELSE, &block)
  events << result
  result
end

#event_for_action_result(result) ⇒ Object



65
66
67
# File 'lib/state_flow/event_client.rb', line 65

def event_for_action_result(result)
  events.detect{|ev| ev.match?(result)}
end

#eventsObject



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

def events
  @events ||= []
end

#handle_exception(*args, &block) ⇒ Object



50
51
52
53
54
# File 'lib/state_flow/event_client.rb', line 50

def handle_exception(*args, &block)
  result = ExceptionHandler.new(self, *args, &block)
  events << result
  result
end

#named_event(name, &block) ⇒ Object



29
30
31
32
33
# File 'lib/state_flow/event_client.rb', line 29

def named_event(name, &block)
  result = NamedEvent.new(self, name, &block)
  events << result
  result
end

#recover(*args, &block) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/state_flow/event_client.rb', line 56

def recover(*args, &block)
  options = {
    :recovering => true, 
    :rolling_back => true, 
    :logging => :error
  }.update(args.extract_options!)
  handle_exception(*(args << options), &block)
end