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.



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

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

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

  set_state(data[:initial])

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

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



5
6
7
# File 'lib/state_transition.rb', line 5

def current
  @current
end

#define_actionsObject (readonly)

Returns the value of attribute define_actions.



5
6
7
# File 'lib/state_transition.rb', line 5

def define_actions
  @define_actions
end

#state_listObject

Returns the value of attribute state_list.



6
7
8
# File 'lib/state_transition.rb', line 6

def state_list
  @state_list
end

Instance Method Details

#add_action(action) ⇒ Object



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

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

#can_move?(state) ⇒ Boolean

Returns:

  • (Boolean)


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

def can_move?(state)
  @state_graph[@current].include?(state)
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_edge(from, to) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/state_transition.rb', line 84

def create_edge(from, to)
  if from.kind_of?(Array) 
    from.each do |state|
      state = state.to_sym
      @state_graph[state].push to unless @state_graph[state].include?(to)
    end
  else
    @state_graph[from].push to
  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|
    name = action[:name].to_sym
    from = action[:from]
      to = action[:to].to_sym

    @define_actions << name.to_sym

    set_state(from)
    set_state(to)
    create_edge(from, to)

    StateMachine.class_eval do
      define_method name 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

#have_state?(state) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#initialize_copy(org) ⇒ Object



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

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

#set_state(state) ⇒ Object



80
81
82
# File 'lib/state_transition.rb', line 80

def set_state(state)
  @state_list.push state.to_sym unless !state.respond_to?(:to_sym) || have_state?(state)
end