state_objects 0.9.1 → 0.9.2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +2 -1
- data/lib/state_objects/base.rb +5 -1
- data/lib/state_objects/model_additions.rb +5 -5
- data/lib/state_objects/version.rb +1 -1
- data/test/missing_event_ex_test.rb +2 -2
- data/test/state_object_values_class_test.rb +2 -2
- data/test/state_object_values_test.rb +2 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 938432a87a9eb1b7b7ce8899f3420232932c3a88
|
4
|
+
data.tar.gz: c763fe9fd28af4114e6c8a831405dbb9ef962047
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82bf299546f3e69835fd79f06b9f0dbde29688a46018c32c0090fa412384b65817384756c85e9ffabecc4f9a1d57b8e5d73cec788885456fab16908e60fea3fa
|
7
|
+
data.tar.gz: 2b6b1f4ec19e52378462de6ee60067f017f62b31a44df07c81c43dfde1b4bffb8ec6cf8531c87ba6d60598d13bbbe2e63f5658bb285f77fa5e00d8285b60087e
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -11,6 +11,7 @@ Many other state machines focus on events and state transitions.
|
|
11
11
|
This state machine focuses on behavior.
|
12
12
|
The main benefit of this gem is to reduce conditional logic by removing #if checks in the model and moving the logic into the state objects. Using Composition in this way can go a long way to simplify Rails models.
|
13
13
|
|
14
|
+
This gem works well with ActiveRecord classes, however ActiveRecord is not required. It can manage the state of regular Ruby objects as well.
|
14
15
|
|
15
16
|
## Installation
|
16
17
|
|
@@ -133,4 +134,4 @@ It's now easy to add scopes with using _occurs, which will generate your where s
|
|
133
134
|
5. Create new Pull Request
|
134
135
|
|
135
136
|
## Thanks To
|
136
|
-
* Scott Baron - for helping with the unit tests.
|
137
|
+
* Scott Baron (https://github.com/rubyist) - for helping with the unit tests.
|
data/lib/state_objects/base.rb
CHANGED
@@ -1,4 +1,8 @@
|
|
1
1
|
module StateObjects
|
2
|
+
|
3
|
+
class Error < RuntimeError
|
4
|
+
end
|
5
|
+
|
2
6
|
class Base
|
3
7
|
|
4
8
|
def initialize(model)
|
@@ -7,7 +11,7 @@ module StateObjects
|
|
7
11
|
|
8
12
|
def self.state_object_values(*opts) # :nodoc:
|
9
13
|
unless opts.size == 3
|
10
|
-
raise @model.to_s + "#state_object_values Must have 3 arguments: symbol, db_value, label. For Example: state_object_values :red,'R','Dont Walk'"
|
14
|
+
raise StateObjects::Error.new @model.to_s + "#state_object_values Must have 3 arguments: symbol, db_value, label. For Example: state_object_values :red,'R','Dont Walk'"
|
11
15
|
end
|
12
16
|
|
13
17
|
class_eval <<-EOF
|
@@ -3,14 +3,14 @@ module StateObjects
|
|
3
3
|
|
4
4
|
def state_object_events(id,*methods) # :nodoc:
|
5
5
|
unless self.respond_to?("#{id}_klasses")
|
6
|
-
raise "Invalid call sequence. #state_objects must be defined before #state_object_events"
|
6
|
+
raise StateObjects::Error.new "Invalid call sequence. #state_objects must be defined before #state_object_events"
|
7
7
|
end
|
8
8
|
|
9
9
|
# check methods on State classes
|
10
10
|
self.send("#{id}_states").each do |klass|
|
11
11
|
methods.each do |method|
|
12
12
|
unless klass.new(nil).respond_to?(method)
|
13
|
-
raise "Invalid state class #{klass} must implement ##{method}"
|
13
|
+
raise StateObjects::Error.new "Invalid state class #{klass} must implement ##{method}"
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
@@ -51,7 +51,7 @@ module StateObjects
|
|
51
51
|
|
52
52
|
def #{id}_state_klass
|
53
53
|
unless result = self.#{id_klasses}[self.#{id}]
|
54
|
-
raise #{id} + " was not a valid state in: " + self.#{id_klasses}.keys.join(', ')
|
54
|
+
raise StateObjects::Error.new #{id} + " was not a valid state in: " + self.#{id_klasses}.keys.join(', ')
|
55
55
|
end
|
56
56
|
result
|
57
57
|
end
|
@@ -72,14 +72,14 @@ module StateObjects
|
|
72
72
|
opts.each do |option_klass|
|
73
73
|
[:symbol, :label, :db_value].each do |required_method|
|
74
74
|
unless option_klass.respond_to?(required_method)
|
75
|
-
raise "Invalid State class ["+ option_klass.to_s + "]. Must implement a class method named: ##{required_method}. Use #state_object_values to setup StateObject"
|
75
|
+
raise StateObjects::Error.new "Invalid State class ["+ option_klass.to_s + "]. Must implement a class method named: ##{required_method}. Use #state_object_values to setup StateObject"
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
79
|
letter = option_klass.db_value
|
80
80
|
display_text = option_klass.label
|
81
81
|
if(send(id_klasses).has_key?(letter))
|
82
|
-
raise "Duplicate key during state_objects :" + id + ". key: " +
|
82
|
+
raise StateObjects::Error.new "Duplicate key during state_objects :" + id + ". key: " +
|
83
83
|
letter + ' for text: ' + display_text
|
84
84
|
end
|
85
85
|
send(id_klasses)[letter] = option_klass
|
@@ -17,7 +17,7 @@ class MissingEventExModelUnderTest < SuperModel::Base
|
|
17
17
|
begin
|
18
18
|
state_object_events :color_state, :missing_event
|
19
19
|
|
20
|
-
rescue
|
20
|
+
rescue StateObjects::Error => ex
|
21
21
|
@@exception_missing_event = ex
|
22
22
|
end
|
23
23
|
|
@@ -33,7 +33,7 @@ class MissingEventExTest < Test::Unit::TestCase
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def test_exception_missing_event
|
36
|
-
assert_equal
|
36
|
+
assert_equal StateObjects::Error, MissingEventExModelUnderTest.exception_missing_event.class
|
37
37
|
assert_equal "Invalid state class LightGreenState must implement #missing_event",
|
38
38
|
MissingEventExModelUnderTest.exception_missing_event.message
|
39
39
|
end
|
@@ -5,7 +5,7 @@ class StateObjectUnderTest < StateObjects::Base
|
|
5
5
|
|
6
6
|
begin
|
7
7
|
state_object_values :green, 'G'
|
8
|
-
rescue
|
8
|
+
rescue StateObjects::Error => ex
|
9
9
|
@@ex_incomplete_values = ex
|
10
10
|
end
|
11
11
|
|
@@ -21,7 +21,7 @@ class StateObjectValuesClassTest < Test::Unit::TestCase
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_ex_incomplete_values
|
24
|
-
assert_equal
|
24
|
+
assert_equal StateObjects::Error, StateObjectUnderTest.ex_incomplete_values.class
|
25
25
|
assert_equal "#state_object_values Must have 3 arguments: symbol, db_value, label. For Example: state_object_values :red,'R','Dont Walk'",
|
26
26
|
StateObjectUnderTest.ex_incomplete_values.message
|
27
27
|
end
|
@@ -14,7 +14,7 @@ class ModelUnderTest < SuperModel::Base
|
|
14
14
|
begin
|
15
15
|
state_objects :no_state,
|
16
16
|
NoStateObjectValues
|
17
|
-
rescue
|
17
|
+
rescue StateObjects::Error => ex
|
18
18
|
@@ex_no_state_values = ex
|
19
19
|
end
|
20
20
|
|
@@ -30,7 +30,7 @@ class StateObjectValuesTest < Test::Unit::TestCase
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def test_no_state_values
|
33
|
-
assert_equal
|
33
|
+
assert_equal StateObjects::Error, ModelUnderTest.ex_no_state_values.class
|
34
34
|
assert_equal "Invalid State class [NoStateObjectValues]. Must implement a class method named: #symbol. Use #state_object_values to setup StateObject",
|
35
35
|
ModelUnderTest.ex_no_state_values.message
|
36
36
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: state_objects
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Windholtz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: supermodel
|
@@ -97,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
97
|
version: '0'
|
98
98
|
requirements: []
|
99
99
|
rubyforge_project:
|
100
|
-
rubygems_version: 2.
|
100
|
+
rubygems_version: 2.4.8
|
101
101
|
signing_key:
|
102
102
|
specification_version: 4
|
103
103
|
summary: "'State' Design Pattern from the Gang of Four book"
|