state_objects 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ ## v0.0.3
2
+ * Ported tests from gem selection_options_for
3
+
4
+ ## v0.0.2
5
+
6
+ * initial release as a rubygem
data/README.md CHANGED
@@ -8,6 +8,11 @@ State transitions are the responsibility of the state classes.
8
8
 
9
9
  Since this was extracted from an application, the specs are currently still in the main application. I will move the specs to this gem as soon as possible.
10
10
 
11
+ ## Focus
12
+ Many state machines focus on events and state transitions.
13
+ 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.
14
+
15
+
11
16
  ## Installation
12
17
 
13
18
  Add this line to your application's Gemfile:
@@ -52,7 +57,7 @@ Or install it yourself as:
52
57
  state_objects :color_state,
53
58
  LightGreenState,
54
59
  LightRedState
55
- state_object_events :change
60
+ state_object_events :color_state, :change
56
61
 
57
62
  scope :red, where("color_state = #{LightRedState.db_value}" )
58
63
  scope :green, where("color_state = #{LightGreenState.db_value}" )
@@ -76,14 +81,11 @@ TODO: there may be typos in the following. It should be cleared up when I move
76
81
  <%= select :walk_light, :color_state, WalkLight.color_states %>
77
82
  <%= select :walk_light, :color_state, [['','']] + WalkLight.color_states %>
78
83
 
79
- assert_equal ['Walk', 'Dont Walk'], WalkLight.color_state_hash.values
80
84
  assert_equal "['Walk', 'Dont Walk']", WalkLight.color_state_js_list
81
85
 
82
- color_state_symbols # returns hash of symbols
83
86
 
84
87
  adds the following INSTANCE METHODS to WalkLight
85
88
 
86
- color_state_hash
87
89
  color_state # returns the single character value as in the db
88
90
  color_state_label # returns the current values label
89
91
  color_state_symbol # returns the current values symbol
@@ -107,8 +109,6 @@ adds the following INSTANCE METHODS to WalkLight
107
109
  assert_equal "Dont Walk", walk_light.color_state_label
108
110
 
109
111
  assert_equal [["Walk", "G"], ["Dont Walk", "R"]], WalkLight.color_states
110
- assert_equal({"G"=>"Walk", "R"=>"Dont Walk",}, WalkLight.color_state_hash)
111
- assert_equals({'G'=>:green, 'R'=>:red }, WalkLight.color_state_symbols)
112
112
 
113
113
  ### Example #2: Selection list
114
114
 
@@ -13,6 +13,9 @@ module StateObjects
13
13
  def self.db_value
14
14
  '#{opts[1]}'
15
15
  end
16
+ def self.where
17
+ "(status_option ='#{opts[1]}')"
18
+ end
16
19
  def self.label
17
20
  '#{opts[2]}'
18
21
  end
@@ -24,6 +24,8 @@ module StateObjects
24
24
  EOF1
25
25
  end
26
26
  end # state_object_events
27
+ alias state_object_accessors state_object_events
28
+ alias state_object_mutators state_object_events
27
29
 
28
30
  def state_objects(id, *opts) # :nodoc:
29
31
  id = id.to_s
@@ -1,3 +1,3 @@
1
1
  module StateObjects
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -18,7 +18,8 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  # gem.add_development_dependency "supermodel" # TODO
21
- gem.add_development_dependency "activerecord"
21
+ # gem.add_development_dependency "activerecord"
22
+ gem.add_development_dependency "supermodel"
22
23
  gem.add_development_dependency "rspec-given"
23
24
  gem.add_runtime_dependency "activerecord"
24
25
 
@@ -0,0 +1,41 @@
1
+ ## ruby -Itest -Ilib test/state_objects_exceptions_test.rb
2
+ require 'test_helper'
3
+
4
+ #
5
+ # test bad data and exceptions
6
+ #
7
+
8
+ class LightGreenState < StateObjects::Base
9
+ state_object_values :green, 'G', 'Walk'
10
+ end
11
+
12
+ class ModelUnderTest < SuperModel::Base
13
+ extend StateObjects::ModelAdditions
14
+ state_objects :color_state,
15
+ LightGreenState
16
+
17
+ begin
18
+ state_object_events :color_state, :missing_event
19
+
20
+ rescue RuntimeError => ex
21
+ @@exception_missing_event = ex
22
+ end
23
+
24
+ def self.exception_missing_event
25
+ @@exception_missing_event
26
+ end
27
+
28
+ end
29
+
30
+ class TranslateOptionsForExTest < Test::Unit::TestCase
31
+ def setup
32
+ @model = ModelUnderTest
33
+ end
34
+
35
+ def test_exception_missing_event
36
+ assert_equal RuntimeError, ModelUnderTest.exception_missing_event.class
37
+ assert_equal "Invalid state class LightGreenState must implement #missing_event",
38
+ ModelUnderTest.exception_missing_event.message
39
+ end
40
+
41
+ end
@@ -0,0 +1,102 @@
1
+ ## ruby -Itest -Ilib test/state_objects_methods_test.rb
2
+ require 'test_helper'
3
+
4
+ #
5
+ # setup three classes for these tests
6
+ #
7
+ # 1. ModelUnderTest -- declare selection_options_for to be tested
8
+ # 2. SiblingOfModelUnderTest -- make sure that no meta-level leaks occur
9
+ # 3. SubClassOfModel -- should have it's own copy of class variables
10
+ #
11
+
12
+ class SiblingOfModelUnderTest < SuperModel::Base
13
+ extend StateObjects::ModelAdditions
14
+ end
15
+
16
+ class LightRedState < StateObjects::Base
17
+ state_object_values :red, 'R', "Dont Walk"
18
+ def change
19
+ model.color_state_green!
20
+ model.save!
21
+ end
22
+ end
23
+
24
+ class LightGreenState < StateObjects::Base
25
+ state_object_values :green, 'G', 'Walk'
26
+ def change
27
+ model.color_state_red!
28
+ model.save!
29
+ end
30
+ end
31
+
32
+ class ModelUnderTest < SuperModel::Base
33
+ extend StateObjects::ModelAdditions
34
+ state_objects :color_state,
35
+ LightGreenState,
36
+ LightRedState
37
+ state_object_events :color_state, :change
38
+
39
+ end
40
+
41
+ # class SubClassOfModel < ModelUnderTest
42
+ # selection_options_for :payment_method_option,
43
+ # [:advanced, 'A', 'Advanced'],
44
+ # [:ecash, 'E', 'ECash Account']
45
+ # end
46
+
47
+ class StateObjectsTest < Test::Unit::TestCase
48
+ def setup
49
+ @model = ModelUnderTest
50
+ end
51
+
52
+ def test_should_provide_symbol
53
+ target = @model.new
54
+ target.color_state_red!
55
+ assert_equal :red, target.color_state_symbol
56
+ end
57
+
58
+ def test_symbol_generation
59
+ target = @model.new
60
+ # green
61
+ target.color_state_green!
62
+ assert target.color_state_green?
63
+ assert !target.color_state_red?
64
+ # red
65
+ target.color_state_red!
66
+ assert !target.color_state_green?
67
+ assert target.color_state_red?
68
+ end
69
+
70
+ def test_sibling_of_target_not_effected_by_class_methods
71
+ SiblingOfModelUnderTest.payment_method_options
72
+ flunk "Should throw Exception"
73
+ rescue NoMethodError => ex
74
+ assert true
75
+ rescue
76
+ flunk "wrong exception thrown"
77
+ end
78
+
79
+ # def test_each_subclass_has_own_symbols
80
+ # assert_equal [["Advanced", "A"], ["ECash Account", "E"]] ,
81
+ # SubClassOfModel.color_payment_method_options.sort
82
+ # end
83
+
84
+ def test_default_status
85
+ target = @model.new
86
+ target.color_state_red!
87
+ assert_equal 'Dont Walk', target.color_state_label
88
+ target.color_state = 'G'
89
+ assert_equal 'Walk', target.color_state_label
90
+ end
91
+
92
+ def test_color_state_array
93
+ assert_equal [["Dont Walk", 'R'], ["Walk", "G"]].sort,
94
+ @model.color_states.sort
95
+ end
96
+
97
+ def test_color_state_js_list
98
+ assert_equal "['Walk', 'Dont Walk']",
99
+ @model.color_state_js_list
100
+ end
101
+
102
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'supermodel'
4
+ require File.dirname(__FILE__) + '/../lib/state_objects'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_objects
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,10 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-25 00:00:00.000000000 Z
12
+ date: 2012-10-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: activerecord
15
+ name: supermodel
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
@@ -67,6 +67,7 @@ extensions: []
67
67
  extra_rdoc_files: []
68
68
  files:
69
69
  - .gitignore
70
+ - CHANGELOG.md
70
71
  - Gemfile
71
72
  - LICENSE.txt
72
73
  - README.md
@@ -76,6 +77,9 @@ files:
76
77
  - lib/state_objects/model_additions.rb
77
78
  - lib/state_objects/version.rb
78
79
  - state_objects.gemspec
80
+ - test/state_objects_exeptions_test.rb
81
+ - test/state_objects_methods_test.rb
82
+ - test/test_helper.rb
79
83
  homepage: https://github.com/mwindholtz/state_objects
80
84
  licenses: []
81
85
  post_install_message:
@@ -100,4 +104,7 @@ rubygems_version: 1.8.24
100
104
  signing_key:
101
105
  specification_version: 3
102
106
  summary: ! '''State'' Design Pattern from the Gang of Four book'
103
- test_files: []
107
+ test_files:
108
+ - test/state_objects_exeptions_test.rb
109
+ - test/state_objects_methods_test.rb
110
+ - test/test_helper.rb