Class: Stateful::StateInfo
- Inherits:
-
Object
- Object
- Stateful::StateInfo
- Defined in:
- lib/stateful/state_info.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#to_transitions ⇒ Object
readonly
Returns the value of attribute to_transitions.
Instance Method Summary collapse
- #can_transition_to?(state) ⇒ Boolean
- #collect_child_states ⇒ Object
- #expand_to_transitions ⇒ Object
- #infos ⇒ Object
-
#initialize(state_class, attr_name, parent, name, config) ⇒ StateInfo
constructor
A new instance of StateInfo.
- #is?(state) ⇒ Boolean
- #is_group? ⇒ Boolean
Constructor Details
#initialize(state_class, attr_name, parent, name, config) ⇒ StateInfo
Returns a new instance of StateInfo.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/stateful/state_info.rb', line 4 def initialize(state_class, attr_name, parent, name, config) @attr_name = attr_name @state_class = state_class if parent @parent = parent parent.children << self end @name = name @to_transitions = [] if config.is_a?(Hash) @groupConfig = config @children = [] else @to_transitions = config ? (config.is_a?(Array) ? config : [config]) : [] end end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
3 4 5 |
# File 'lib/stateful/state_info.rb', line 3 def children @children end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/stateful/state_info.rb', line 3 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
3 4 5 |
# File 'lib/stateful/state_info.rb', line 3 def parent @parent end |
#to_transitions ⇒ Object (readonly)
Returns the value of attribute to_transitions.
3 4 5 |
# File 'lib/stateful/state_info.rb', line 3 def to_transitions @to_transitions end |
Instance Method Details
#can_transition_to?(state) ⇒ Boolean
35 36 37 38 39 40 41 42 |
# File 'lib/stateful/state_info.rb', line 35 def can_transition_to?(state) state_info = infos[state] if is_group? or state_info.nil? or state_info.is_group? false else to_transitions.include?(state) end end |
#collect_child_states ⇒ Object
44 45 46 |
# File 'lib/stateful/state_info.rb', line 44 def collect_child_states is_group? ? children.flat_map(&:collect_child_states) : [name] end |
#expand_to_transitions ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/stateful/state_info.rb', line 48 def if to_transitions.any? @to_transitions = to_transitions.flat_map do |to| info = infos[to] if info.is_group? info.collect_child_states else [info.name] end end end end |
#infos ⇒ Object
31 32 33 |
# File 'lib/stateful/state_info.rb', line 31 def infos @state_class.__send__("#{@attr_name}_infos") end |
#is?(state) ⇒ Boolean
23 24 25 |
# File 'lib/stateful/state_info.rb', line 23 def is?(state) !!(@name == state or (parent and parent.is?(state))) end |
#is_group? ⇒ Boolean
27 28 29 |
# File 'lib/stateful/state_info.rb', line 27 def is_group? !!@groupConfig end |