Class: Stateful::StateInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/stateful/state_info.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childrenObject (readonly)

Returns the value of attribute children.



3
4
5
# File 'lib/stateful/state_info.rb', line 3

def children
  @children
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/stateful/state_info.rb', line 3

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



3
4
5
# File 'lib/stateful/state_info.rb', line 3

def parent
  @parent
end

#to_transitionsObject (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

Returns:

  • (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_statesObject



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_transitionsObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/stateful/state_info.rb', line 48

def expand_to_transitions
  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

#infosObject



31
32
33
# File 'lib/stateful/state_info.rb', line 31

def infos
  @state_class.__send__("#{@attr_name}_infos")
end

#is?(state) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


27
28
29
# File 'lib/stateful/state_info.rb', line 27

def is_group?
  !!@groupConfig
end