simple_state_machine 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.2
|
@@ -8,12 +8,12 @@ module SimpleStateMachine::ActiveRecord
|
|
8
8
|
|
9
9
|
class Decorator < SimpleStateMachine::Decorator
|
10
10
|
|
11
|
-
def decorate
|
12
|
-
super
|
13
|
-
unless @subject.method_defined?("#{event_name}_and_save")
|
14
|
-
@subject.send(:define_method, "#{event_name}_and_save") do |*args|
|
11
|
+
def decorate transition
|
12
|
+
super transition
|
13
|
+
unless @subject.method_defined?("#{transition.event_name}_and_save")
|
14
|
+
@subject.send(:define_method, "#{transition.event_name}_and_save") do |*args|
|
15
15
|
old_state = state
|
16
|
-
send "#{event_name}", *args
|
16
|
+
send "#{transition.event_name}", *args
|
17
17
|
if !self.errors.entries.empty?
|
18
18
|
self.state = old_state
|
19
19
|
return false
|
@@ -27,10 +27,10 @@ module SimpleStateMachine::ActiveRecord
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
30
|
-
unless @subject.method_defined?("#{event_name}_and_save!")
|
31
|
-
@subject.send(:define_method, "#{event_name}_and_save!") do |*args|
|
30
|
+
unless @subject.method_defined?("#{transition.event_name}_and_save!")
|
31
|
+
@subject.send(:define_method, "#{transition.event_name}_and_save!") do |*args|
|
32
32
|
old_state = state
|
33
|
-
send "#{event_name}", *args
|
33
|
+
send "#{transition.event_name}", *args
|
34
34
|
if !self.errors.entries.empty?
|
35
35
|
self.state = old_state
|
36
36
|
raise ActiveRecord::RecordInvalid.new(self)
|
@@ -4,19 +4,30 @@ module SimpleStateMachine
|
|
4
4
|
|
5
5
|
def event event_name, state_transitions
|
6
6
|
state_transitions.each do |from, to|
|
7
|
-
state_machine_definition.add_transition(event_name, from, to)
|
8
|
-
state_machine_decorator(self).decorate(
|
7
|
+
transition = state_machine_definition.add_transition(event_name, from, to)
|
8
|
+
state_machine_decorator(self).decorate(transition)
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
def state_machine_definition
|
13
13
|
@state_machine_definition ||= StateMachineDefinition.new
|
14
14
|
end
|
15
|
+
|
16
|
+
def state_machine_definition= state_machine_definition
|
17
|
+
@state_machine_definition = state_machine_definition
|
18
|
+
state_machine_definition.transitions.each do |transition|
|
19
|
+
state_machine_decorator(self).decorate(transition)
|
20
|
+
end
|
21
|
+
end
|
15
22
|
|
16
23
|
def state_machine_decorator subject
|
17
24
|
Decorator.new subject
|
18
25
|
end
|
19
26
|
|
27
|
+
def inherited(subclass)
|
28
|
+
subclass.state_machine_definition = state_machine_definition.clone
|
29
|
+
super
|
30
|
+
end
|
20
31
|
end
|
21
32
|
|
22
33
|
include EventMixin
|
@@ -24,12 +35,13 @@ module SimpleStateMachine
|
|
24
35
|
class StateMachineDefinition
|
25
36
|
|
26
37
|
def transitions
|
27
|
-
@transitions ||=
|
38
|
+
@transitions ||= []
|
28
39
|
end
|
29
40
|
|
30
41
|
def add_transition event_name, from, to
|
31
|
-
|
32
|
-
transitions
|
42
|
+
transition = Transition.new(event_name.to_s, from.to_s, to.to_s)
|
43
|
+
transitions << transition
|
44
|
+
transition
|
33
45
|
end
|
34
46
|
|
35
47
|
end
|
@@ -41,7 +53,8 @@ module SimpleStateMachine
|
|
41
53
|
end
|
42
54
|
|
43
55
|
def next_state(event_name)
|
44
|
-
transitions
|
56
|
+
transition = transitions.select{|t| t.event_name.to_s == event_name.to_s && @subject.state.to_s == t.from.to_s}.first
|
57
|
+
transition ? transition.to : nil
|
45
58
|
end
|
46
59
|
|
47
60
|
def transition(event_name)
|
@@ -77,6 +90,9 @@ module SimpleStateMachine
|
|
77
90
|
|
78
91
|
end
|
79
92
|
|
93
|
+
class Transition < Struct.new(:event_name, :from, :to)
|
94
|
+
end
|
95
|
+
|
80
96
|
class Decorator
|
81
97
|
|
82
98
|
def initialize(subject)
|
@@ -86,11 +102,11 @@ module SimpleStateMachine
|
|
86
102
|
define_state_setter_method
|
87
103
|
end
|
88
104
|
|
89
|
-
def decorate
|
90
|
-
define_state_helper_method(from)
|
91
|
-
define_state_helper_method(to)
|
92
|
-
define_event_method(event_name)
|
93
|
-
decorate_event_method(event_name)
|
105
|
+
def decorate transition
|
106
|
+
define_state_helper_method(transition.from)
|
107
|
+
define_state_helper_method(transition.to)
|
108
|
+
define_event_method(transition.event_name)
|
109
|
+
decorate_event_method(transition.event_name)
|
94
110
|
end
|
95
111
|
|
96
112
|
private
|
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{simple_state_machine}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Marek de Heus", "Petrik de Heus"]
|
@@ -58,6 +58,17 @@ describe SimpleStateMachine do
|
|
58
58
|
|
59
59
|
end
|
60
60
|
|
61
|
+
describe "state_machine_definition" do
|
62
|
+
it "is inherited by subclasses" do
|
63
|
+
example = Class.new(SimpleExample).new
|
64
|
+
example.should be_state1
|
65
|
+
example.event1
|
66
|
+
example.should be_state2
|
67
|
+
example.event1
|
68
|
+
example.should be_state3
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
61
72
|
describe "state_machine" do
|
62
73
|
it "has a next_state method" do
|
63
74
|
example = SimpleExample.new
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_state_machine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 2
|
10
|
+
version: 0.2.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Marek de Heus
|