micromachine 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -36,6 +36,15 @@ It can also have callbacks when entering some state:
36
36
  puts "Confirmed"
37
37
  end
38
38
 
39
+ Or callbacks on any transition:
40
+
41
+ machine.on(:any) do
42
+ puts "Transitioned..."
43
+ end
44
+
45
+ Note that `:any` is a special key. Using it as a state when declaring
46
+ transitions will give you unexpected results.
47
+
39
48
  Adding MicroMachine to your models
40
49
  ----------------------------------
41
50
 
@@ -82,6 +91,35 @@ This example asumes you have a :confirmation_state attribute in your
82
91
  model. This may look like a very verbose implementation, but you gain a
83
92
  lot in flexibility.
84
93
 
94
+ An alternative approach, using callbacks:
95
+
96
+ class Event < ActiveRecord::Base
97
+ def confirm!
98
+ confirmation.trigger(:confirm)
99
+ end
100
+
101
+ def cancel!
102
+ confirmation.trigger(:cancel)
103
+ end
104
+
105
+ def reset!
106
+ confirmation.trigger(:reset)
107
+ end
108
+
109
+ def confirmation
110
+ @confirmation ||= begin
111
+ confirmation = MicroMachine.new(confirmation_state || "pending")
112
+ confirmation.transitions_for[:confirm] = { "pending" => "confirmed" }
113
+ confirmation.transitions_for[:cancel] = { "confirmed" => "cancelled" }
114
+ confirmation.transitions_for[:reset] = { "confirmed" => "pending", "cancelled" => "pending" }
115
+ confirmation.on(:any) { self.confirmation_state = confirmation.state }
116
+ confirmation
117
+ end
118
+ end
119
+ end
120
+
121
+ Now, on any transition the `confirmation_state` attribute in the model will be updated.
122
+
85
123
  Installation
86
124
  ------------
87
125
 
data/lib/micromachine.rb CHANGED
@@ -17,7 +17,8 @@ class MicroMachine
17
17
  def trigger event
18
18
  if trigger?(event)
19
19
  @state = transitions_for[event][@state]
20
- @callbacks[@state].each { |callback| callback.call }
20
+ callbacks = @callbacks[@state] + @callbacks[:any]
21
+ callbacks.each { |callback| callback.call }
21
22
  true
22
23
  end
23
24
  end
data/micromachine.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'micromachine'
3
- s.version = '0.0.9'
3
+ s.version = '0.0.10'
4
4
  s.summary = %{Minimal Finite State Machine.}
5
5
  s.description = %Q{There are many finite state machine implementations for Ruby, and they all provide a nice DSL for declaring events, exceptions, callbacks, and all kinds of niceties in general.\n\nBut if all you want is a finite state machine, look no further: this is only 22 lines of code and provides everything a finite state machine must have, and nothing more.}
6
6
  s.author = "Michel Martens"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: micromachine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Martens
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-05 00:00:00 -03:00
12
+ date: 2010-03-01 00:00:00 -03:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -56,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
56
  requirements: []
57
57
 
58
58
  rubyforge_project:
59
- rubygems_version: 1.3.4
59
+ rubygems_version: 1.3.5
60
60
  signing_key:
61
61
  specification_version: 3
62
62
  summary: Minimal Finite State Machine.