state_pattern 1.3.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +17 -41
- data/Rakefile +0 -1
- data/examples/rails_2_3_8_button_example/app/models/button/off.rb +1 -1
- data/examples/rails_2_3_8_button_example/app/models/button/on.rb +1 -1
- data/examples/rails_3_button_example/app/models/button/off.rb +1 -1
- data/examples/rails_3_button_example/app/models/button/on.rb +1 -1
- data/examples/rails_3_button_example/app/views/buttons/_form.html.erb +21 -0
- data/examples/rails_3_button_example/app/views/buttons/edit.html.erb +6 -0
- data/examples/rails_3_button_example/app/views/buttons/index.html.erb +23 -0
- data/examples/rails_3_button_example/app/views/buttons/new.html.erb +5 -0
- data/examples/rails_3_button_example/config/environments/production.rb +49 -0
- data/examples/rails_3_button_example/config/environments/test.rb +35 -0
- data/examples/rails_3_button_example/public/javascripts/controls.js +965 -0
- data/examples/rails_3_button_example/public/javascripts/dragdrop.js +974 -0
- data/examples/rails_3_button_example/public/javascripts/effects.js +1123 -0
- data/examples/rails_3_button_example/public/robots.txt +5 -0
- data/lib/state_pattern.rb +10 -78
- data/lib/state_pattern/active_record.rb +2 -2
- data/lib/state_pattern/state.rb +8 -4
- data/rails/init.rb +0 -1
- data/test/hook_test.rb +6 -7
- data/test/state_pattern/active_record/active_record_test.rb +2 -3
- data/test/state_pattern/active_record/test_helper.rb +2 -1
- data/test/state_pattern_test.rb +12 -11
- data/test/test_class_creation_helper.rb +0 -11
- metadata +24 -9
- data/lib/state_pattern/invalid_transition_exception.rb +0 -14
- data/test/querying_test.rb +0 -87
- data/test/transition_validations_test.rb +0 -55
@@ -1,14 +0,0 @@
|
|
1
|
-
module StatePattern
|
2
|
-
class InvalidTransitionException < RuntimeError
|
3
|
-
attr_reader :from_module, :to_module, :event
|
4
|
-
def initialize(from_module, to_module, event)
|
5
|
-
@from_module = from_module
|
6
|
-
@to_module = to_module
|
7
|
-
@event = event
|
8
|
-
end
|
9
|
-
|
10
|
-
def message
|
11
|
-
"Event #@event cannot transition from #@from_module to #@to_module"
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
data/test/querying_test.rb
DELETED
@@ -1,87 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module Querying
|
4
|
-
class StateBaseBase < StatePattern::State
|
5
|
-
def common_event
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
class StateBase < StateBaseBase
|
10
|
-
def another_common_event
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
class On < StateBase
|
15
|
-
def press
|
16
|
-
transition_to(Off)
|
17
|
-
end
|
18
|
-
|
19
|
-
def one_event
|
20
|
-
end
|
21
|
-
|
22
|
-
def another_event
|
23
|
-
end
|
24
|
-
|
25
|
-
def enter
|
26
|
-
end
|
27
|
-
|
28
|
-
def exit
|
29
|
-
end
|
30
|
-
private
|
31
|
-
def not_an_event
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
class Off < StateBase
|
36
|
-
def press
|
37
|
-
transition_to(On)
|
38
|
-
end
|
39
|
-
|
40
|
-
def one_event
|
41
|
-
end
|
42
|
-
|
43
|
-
def another_event
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
class Button
|
48
|
-
include StatePattern
|
49
|
-
set_initial_state On
|
50
|
-
valid_transitions [On, :press] => Off, [Off, :press] => On
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
Expectations do
|
55
|
-
expect %w(Querying::Off Querying::On) do
|
56
|
-
Querying::Button.state_classes.map{|c| c.name}.sort
|
57
|
-
end
|
58
|
-
|
59
|
-
expect ["another_common_event", "another_event", "common_event", "one_event", "press"] do
|
60
|
-
Querying::Button.state_methods.map{|s| s.to_s}.sort
|
61
|
-
end
|
62
|
-
|
63
|
-
expect [:press] do
|
64
|
-
Querying::Button.state_events
|
65
|
-
end
|
66
|
-
|
67
|
-
expect true do
|
68
|
-
Querying::Button.new.on?
|
69
|
-
end
|
70
|
-
|
71
|
-
expect false do
|
72
|
-
Querying::Button.new.off?
|
73
|
-
end
|
74
|
-
|
75
|
-
expect false do
|
76
|
-
button = Querying::Button.new
|
77
|
-
button.press
|
78
|
-
button.on?
|
79
|
-
end
|
80
|
-
|
81
|
-
expect true do
|
82
|
-
button = Querying::Button.new
|
83
|
-
button.press
|
84
|
-
button.off?
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
@@ -1,55 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
Expectations do
|
4
|
-
expect "up" do
|
5
|
-
with_test_class("Switch", :states => ["Up", "Down", "Middle"], :initial_state => "Middle",
|
6
|
-
:transitions => {["Up", :push_down] => "Middle",
|
7
|
-
["Down", :push_up] => "Middle",
|
8
|
-
["Middle", :push_up] => "Up",
|
9
|
-
["Middle", :push_down] => "Down"}) do
|
10
|
-
switch = Switch.new
|
11
|
-
switch.push_up
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
expect "up" do
|
16
|
-
with_test_class("Switch", :states => ["Up", "Down", "Middle"], :initial_state => "Middle",
|
17
|
-
:transitions => {["Up", :push_down] => "Middle",
|
18
|
-
["Down", :push_up] => "Middle",
|
19
|
-
["Middle", :push_up] => "Up",
|
20
|
-
["Middle", :push_down] => "Down"},
|
21
|
-
:valid_transitions => {["Up", :push_down] => "Middle",
|
22
|
-
["Down", :push_up] => "Middle",
|
23
|
-
["Middle", :push_up] => "Up",
|
24
|
-
["Middle", :push_down] => "Down"}) do
|
25
|
-
switch = Switch.new
|
26
|
-
switch.push_up
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
expect StatePattern::InvalidTransitionException do
|
31
|
-
with_test_class("Switch", :states => ["Up", "Down", "Middle"], :initial_state => "Middle",
|
32
|
-
:transitions => {["Up", :push_down] => "Middle",
|
33
|
-
["Down", :push_up] => "Middle",
|
34
|
-
["Middle", :push_up] => "Up",
|
35
|
-
["Middle", :push_down] => "Down"},
|
36
|
-
:valid_transitions => {["Up", :push_down] => "Middle",
|
37
|
-
["Down", :push_up] => "Middle",
|
38
|
-
["Middle", :push_down] => "Down"}) do
|
39
|
-
switch = Switch.new
|
40
|
-
switch.push_up
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
expect StatePattern::InvalidTransitionException do
|
45
|
-
with_test_class("Switch", :states => ["Up", "Down", "Middle"], :initial_state => "Middle",
|
46
|
-
:transitions => {["Up", :push_down] => "Middle",
|
47
|
-
["Down", :push_up] => "Middle",
|
48
|
-
["Middle", :push_up] => "Up",
|
49
|
-
["Middle", :push_down] => "Down"},
|
50
|
-
:valid_transitions => {"Up" => "Middle", "Down" => "Middle", "Middle" => "Down"}) do
|
51
|
-
switch = Switch.new
|
52
|
-
switch.push_up
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|