Class: StateTransition::StateMachine

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil) ⇒ StateMachine

Returns a new instance of StateMachine.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/state_transition.rb', line 10

def initialize(data = nil)
  @state_list = Hash.new{|hash, key| hash[key] = {}}
  @callbacks = {}
  @define_actions = []

  begin
    @current = data[:initial] 
  rescue 
    raise StandardError, "StateMachine: Not define first state."
  end

  @current = data[:initial].to_sym

  create_move_action(data[:actions] || [])
  create_callbacks(data[:callbacks] || [])
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



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

def current
  @current
end

#define_actionsObject (readonly)

Returns the value of attribute define_actions.



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

def define_actions
  @define_actions
end

Instance Method Details

#add_move_action(action) ⇒ Object



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

def add_move_action(action)
  create_move_action([action])
end

#can_move?(point, from: @current, to: nil) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/state_transition.rb', line 76

def can_move?(point, from: @current, to: nil)
  @state_list[from].values.include?(to||point)
end

#create_callbacks(callbacks) ⇒ Object



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

def create_callbacks(callbacks)
  callbacks.each do |name, function|
    @callbacks[name] = function
  end
end

#create_move_action(actions) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/state_transition.rb', line 41

def create_move_action(actions)
  actions.each do |action|
    key = action[:name].to_sym
    froms = ( action[:from].is_a?(Array) )? action[:from] : [action[:from]]
    to = action[:to].to_sym

    @define_actions << key.to_sym

    set_state(froms: froms, key: key, to: to)

    StateMachine.class_eval do
      froms.each do |from|
        define_method key do
          if can_move?(to)
            before_func = ("before_" + to.to_s).to_sym
            after_func  = ("after_" + to.to_s).to_sym

            @callbacks[before_func].call if @callbacks[before_func] 
            @current = to
            @callbacks[after_func].call if @callbacks[after_func]
          else
            raise StandardError, "Can not move from '#{@current}' to '#{to}'!"
          end
        end
      end
    end
  end
end

#have_state?(state) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/state_transition.rb', line 33

def have_state?(state)
  @state_list.keys.include?(state.to_sym)
end

#initialize_copy(org) ⇒ Object



27
28
29
30
31
# File 'lib/state_transition.rb', line 27

def initialize_copy(org)
  @callbacks = @callbacks.deep_copy
  @state_list = @state_list.deep_copy
  @define_actions = @define_actions.deep_copy
end

#set_state(froms:, key:, to:) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/state_transition.rb', line 80

def set_state(froms:, key:, to:)
  froms = [froms] unless froms.is_a?(Array)

  froms.each do |from|
    @state_list[to] = {} unless @state_list.has_key?(to)
    @state_list[from.to_sym][key] = to
  end
end