dcadenas-state_pattern 1.0.2 → 1.1.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 +7 -7
- data/VERSION +1 -1
- data/lib/state_pattern.rb +12 -11
- data/state_pattern.gemspec +1 -1
- data/test/state_pattern_test.rb +0 -1
- data/test/test_class_creation_helper.rb +0 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -21,15 +21,8 @@ A Ruby state pattern implementation.
|
|
21
21
|
|
22
22
|
class Button
|
23
23
|
include StatePattern
|
24
|
-
add_states On, Off
|
25
24
|
set_initial_state Off
|
26
25
|
valid_transitions [On, :press] => Off, [Off, :press] => On
|
27
|
-
|
28
|
-
#this method can be removed as it will be mapped automatically anyways
|
29
|
-
#but it is good to leave the option to do the delegation yourself in case you want more freedom
|
30
|
-
def press
|
31
|
-
delegate_to_event(:press)
|
32
|
-
end
|
33
26
|
|
34
27
|
def button_name
|
35
28
|
"Light button"
|
@@ -41,6 +34,9 @@ A Ruby state pattern implementation.
|
|
41
34
|
puts button.press # => "Light button is off"
|
42
35
|
puts button.press # => "Light button is on"
|
43
36
|
|
37
|
+
== Installation
|
38
|
+
sudo gem install state_pattern
|
39
|
+
|
44
40
|
== Validations
|
45
41
|
|
46
42
|
One of the few drawbacks the state pattern has is that it can get difficult to see the global picture of your state machine when dealing with complex cases.
|
@@ -57,6 +53,10 @@ With more than one target state
|
|
57
53
|
Using event names to gain more detail
|
58
54
|
valid_transitions [Up, :switch] => [Middle, Down], [Down, :switch] => Middle, [Middle, :switch] => Up
|
59
55
|
|
56
|
+
== Collaborate
|
57
|
+
|
58
|
+
http://github.com/dcadenas/state_pattern
|
59
|
+
|
60
60
|
== Copyright
|
61
61
|
|
62
62
|
Copyright (c) 2009 Daniel Cadenas. See LICENSE for details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0
|
1
|
+
1.1.0
|
data/lib/state_pattern.rb
CHANGED
@@ -8,7 +8,18 @@ module StatePattern
|
|
8
8
|
|
9
9
|
module ClassMethods
|
10
10
|
def state_classes
|
11
|
-
|
11
|
+
state_classes_in_transisions_hash = []
|
12
|
+
|
13
|
+
if transitions_hash
|
14
|
+
state_classes_in_transisions_hash = transitions_hash.map do |from, to|
|
15
|
+
from_class = from.respond_to?(:to_ary) ? from.first : from
|
16
|
+
to_classes = to.respond_to?(:to_ary) ? to : [to]
|
17
|
+
to_classes + [from_class]
|
18
|
+
end.flatten
|
19
|
+
end
|
20
|
+
|
21
|
+
state_classes_in_transisions_hash << initial_state_class
|
22
|
+
state_classes_in_transisions_hash.uniq
|
12
23
|
end
|
13
24
|
|
14
25
|
def initial_state_class
|
@@ -19,16 +30,6 @@ module StatePattern
|
|
19
30
|
@initial_state_class = state_class
|
20
31
|
end
|
21
32
|
|
22
|
-
def add_states(*state_classes)
|
23
|
-
state_classes.each do |state_class|
|
24
|
-
add_state_class(state_class)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def add_state_class(state_class)
|
29
|
-
state_classes << state_class
|
30
|
-
end
|
31
|
-
|
32
33
|
def valid_transitions(transitions_hash)
|
33
34
|
@transitions_hash = transitions_hash
|
34
35
|
@transitions_hash.each do |key, value|
|
data/state_pattern.gemspec
CHANGED
data/test/state_pattern_test.rb
CHANGED
@@ -22,7 +22,6 @@ module TestClassCreationHelper
|
|
22
22
|
|
23
23
|
created_consts << create_class(main_state_module_name) do
|
24
24
|
include StatePattern
|
25
|
-
add_states *options[:states].map{|s| Object.const_get(s)} if options.has_key?(:states)
|
26
25
|
set_initial_state Object.const_get(options[:initial_state]) if options.has_key?(:initial_state)
|
27
26
|
if options.has_key?(:valid_transitions)
|
28
27
|
valid_transitions_with_constants = {}
|