Class: StateFlow::State

Inherits:
Object
  • Object
show all
Includes:
ActionClient, ElementVisitable, EventClient, GuardClient
Defined in:
lib/state_flow/state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ElementVisitable

#visit

Methods included from ActionClient

#action

Methods included from GuardClient

#guard, #guard_else, #guard_for, #guards

Methods included from EventClient

#action_event, #event, #event_else, #event_for_action_result, #events, #handle_exception, #named_event, #recover

Methods included from ExceptionHandlerClient

#exception_handling

Constructor Details

#initialize(flow_or_parent, name, &block) ⇒ State

Returns a new instance of State.



13
14
15
16
17
18
19
# File 'lib/state_flow/state.rb', line 13

def initialize(flow_or_parent, name, &block)
  @name = name
  @parent = flow_or_parent if flow_or_parent.is_a?(State)
  @flow = flow_or_parent.is_a?(State) ? flow_or_parent.flow : flow_or_parent
  @concreate = @flow.state_cd_by_key(@name)
  instance_eval(&block) if block
end

Instance Attribute Details

#flowObject (readonly)

Returns the value of attribute flow.



11
12
13
# File 'lib/state_flow/state.rb', line 11

def flow
  @flow
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/state_flow/state.rb', line 11

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



11
12
13
# File 'lib/state_flow/state.rb', line 11

def parent
  @parent
end

#termination(name = nil) ⇒ Object

Returns the value of attribute termination.



12
13
14
# File 'lib/state_flow/state.rb', line 12

def termination
  @termination
end

Instance Method Details

#childrenObject



36
37
38
# File 'lib/state_flow/state.rb', line 36

def children
  @children ||= []
end

#concrete?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/state_flow/state.rb', line 49

def concrete?
  @concreate
end

#descendantsObject



40
41
42
# File 'lib/state_flow/state.rb', line 40

def descendants
  [self, children.map{|c|c.descendants}].flatten
end

#exception_handlersObject

override ExceptionHandlerClient#exception_handlers



70
71
72
73
74
# File 'lib/state_flow/state.rb', line 70

def exception_handlers
  result = events.select{|ev| ev.is_a?(ExceptionHandler)}
  result.concat(parent.exception_handlers) if parent
  result
end

#include?(state_or_name) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/state_flow/state.rb', line 44

def include?(state_or_name)
  name = state_or_name.is_a?(State) ? state_or_name.name : state_or_name
  descendants.any?{|state| state.name == name}
end

#inspectObject



86
87
88
# File 'lib/state_flow/state.rb', line 86

def inspect
  "#<%s:%#x @name=%s>" % [self.class.name, self.object_id, name_path.inspect]
end

#name_path(separator = '>') ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/state_flow/state.rb', line 76

def name_path(separator = '>')
  result = []
  current = self
  while current
    result << current.name
    current = current.parent
  end
  result.reverse.join(separator)
end

#process(context) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/state_flow/state.rb', line 60

def process(context)
  context.trace(self)
  exception_handling(context) do
    guard = guard_for(context)
    return guard.process(context) if guard
    return action.process(context) if action
  end
end

#state(name, &block) ⇒ Object Also known as: from, group, state_group



21
22
23
24
25
# File 'lib/state_flow/state.rb', line 21

def state(name, &block)
  result = State.new(self, name, &block)
  children << result
  result
end