Module: Control::State::ClassMethods

Defined in:
lib/control/state.rb

Instance Method Summary collapse

Instance Method Details

#final?Boolean

Checks if a state is final. There can be no transitions.

Returns:

  • (Boolean)


55
56
57
# File 'lib/control/state.rb', line 55

def final?
  !!@final
end

#is_state?Boolean

Checks if class is a state

Returns:

  • (Boolean)


37
38
39
# File 'lib/control/state.rb', line 37

def is_state?
  true
end

#next_states(*states) ⇒ Array<State>

Getter/setter for possible next_states

Parameters:

  • states

    state classes in underscore format. Can also use :none to mark state as final

Returns:

  • (Array<State>)

    called with no parameters, an array is returned with next possible states



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/control/state.rb', line 15

def next_states(*states)
  if states.any?
    if states.include? :none
      @final = true
    else
      @next_states = states.map{|s| s.to_s.classify}.compact
    end
  else
    if @final                                         # state is final, there are no possible next states
      Array.new
    else                                              # state is not final, carry on
      if @next_states                                 # possible next states were previously declared in class
        @next_states.map { |s| Kernel.qualified_const_get(s) }
      else                                            # all states connected to the workflow are valid, no constrains
        workflow_class.states
      end
    end
  end
end

#workflow_classClass

Gets the workflow class associated with the state

Returns:

  • (Class)

    workflow class object



43
44
45
46
47
48
49
50
51
# File 'lib/control/state.rb', line 43

def workflow_class
  unless @workflow_class
    reflect_on_all_associations.each do |a|
      klass = a.klass
      @workflow_class = klass if klass.respond_to?(:is_workflow?) && klass.is_workflow?
    end
  end
  @workflow_class
end