Class: StateMashine

Inherits:
Object show all
Defined in:
lib/RubyExt/StateMashine.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object = nil) ⇒ StateMashine

Returns a new instance of StateMashine.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/RubyExt/StateMashine.rb', line 4

def initialize object = nil;
  @object = object
  @on_entry, @on_exit, @on_event = {}, {}, {}
  
  @custom_states = {}
  if self.class.custom_state_get
    self.class.custom_state_get.each do |state, klass|
      @custom_states[state] = klass.new
    end    
  end
  
  is = self.class.initial_state_get
  raise_without_self "Initial State not defined!" unless is    
  fire_action @on_entry[is]            
  @state = is
end

Instance Attribute Details

#objectObject

Returns the value of attribute object.



2
3
4
# File 'lib/RubyExt/StateMashine.rb', line 2

def object
  @object
end

Class Method Details

.custom_state(state, klass) ⇒ Object



151
152
153
154
# File 'lib/RubyExt/StateMashine.rb', line 151

def custom_state state, klass; 
  @custom_state ||= {}
  @custom_state[state] = klass
end

.custom_state_getObject



130
# File 'lib/RubyExt/StateMashine.rb', line 130

def custom_state_get; @custom_state end

.from_actionsObject



127
# File 'lib/RubyExt/StateMashine.rb', line 127

def from_actions; @from_actions end

.from_statesObject



128
# File 'lib/RubyExt/StateMashine.rb', line 128

def from_states; @from_states end

.initial_state(state) ⇒ Object



156
# File 'lib/RubyExt/StateMashine.rb', line 156

def initial_state state; @initial_state = state end

.initial_state_getObject



129
# File 'lib/RubyExt/StateMashine.rb', line 129

def initial_state_get; @initial_state end

.transitions(*args) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/RubyExt/StateMashine.rb', line 132

def transitions *args
  @from_actions, @from_states = {}, {}
  args.each do |from, action, to|        
    actions = @from_actions[from]
    unless actions
      actions = {}
      @from_actions[from] = actions
    end        
    actions[action] = to
    
    states = @from_states[from]
    unless states
      states = {}
      @from_states[from] = states
    end        
    states[to] = action
  end
end

Instance Method Details

#==(other) ⇒ Object



109
110
111
112
113
114
# File 'lib/RubyExt/StateMashine.rb', line 109

def == other
  return true if equal? other
  return state == other if other.is_a? Symbol
  return state == other.state if other.is_a? StateMashine
  return false
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
124
# File 'lib/RubyExt/StateMashine.rb', line 120

def eql? other
  return true if equal? other
  return state == other.state if other.is_a? StateMashine
  return false
end

#event(event) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/RubyExt/StateMashine.rb', line 45

def event event        
  custom_state = @custom_states[state]
  
  to = if custom_state
    begin
      custom_state.send event
    rescue NoMethodError
      raise_without_self "There is no :#{event} Event in :#{state} State!"
    end
  else
    actions = self.class.from_actions[state]
    raise_without_self "There is no :#{event} Event in :#{state} State!" unless actions
    actions[event]      
  end
  raise_without_self "There is no :#{event} Event in :#{state} State!" unless to
  
  #    p [state, event, to]
  
  old_state = state
  
  @state = to
  
  begin
    fire_action @on_exit[old_state]
    fire_action @on_event[old_state + event] 
    fire_action @on_entry[to]                      
  rescue Exception => e
    @state = old_state
    raise e
  end
end

#hashObject



116
117
118
# File 'lib/RubyExt/StateMashine.rb', line 116

def hash
  state.hash
end

#inspectObject



163
164
165
# File 'lib/RubyExt/StateMashine.rb', line 163

def inspect
  to_s
end

#on_entry(state, method = nil, &b) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/RubyExt/StateMashine.rb', line 21

def on_entry state, method = nil, &b
  if method or b
    @on_entry[state] = method || b
  else
    @on_entry.delete state
  end
end

#on_event(from, event, method = nil, &b) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/RubyExt/StateMashine.rb', line 29

def on_event from, event, method = nil, &b
  if method or b
    @on_event[from + event] = method || b
  else      
    @on_event.delete from + event
  end
end

#on_exit(state, method = nil, &b) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/RubyExt/StateMashine.rb', line 37

def on_exit state, method = nil, &b
  if method or b
    @on_exit[state] = method || b
  else
    @on_exit.delete state
  end
end

#stateObject



77
# File 'lib/RubyExt/StateMashine.rb', line 77

def state; @state end

#state=(new_state) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/RubyExt/StateMashine.rb', line 79

def state= new_state
  custom_state = @custom_states[state]
  
  event = if custom_state
    begin
      custom_state.state new_state
    rescue Exception => e
      raise e
    end
  else
    states = self.class.from_states[state]
    raise_without_self "There is no way from :#{state} State into :#{new_state} State!" unless states
    states[new_state]      
  end
  raise_without_self "There is no way from :#{state} State into :#{new_state} State!" unless event
  
  old_state = state
  
  @state = new_state
  
  begin
    fire_action @on_exit[old_state]
    fire_action @on_event[old_state + event] 
    fire_action @on_entry[new_state]        
  rescue Exception => e
    @state = old_state
    raise e
  end
end

#to_sObject



159
160
161
# File 'lib/RubyExt/StateMashine.rb', line 159

def to_s
  "#<#{self.class.name} state = :#{state}"
end