command_model 1.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.
Files changed (72) hide show
  1. data/.gitignore +17 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +4 -0
  4. data/Guardfile +9 -0
  5. data/LICENSE +22 -0
  6. data/README.md +99 -0
  7. data/Rakefile +2 -0
  8. data/command_model.gemspec +23 -0
  9. data/examples/bank/.gitignore +15 -0
  10. data/examples/bank/Gemfile +38 -0
  11. data/examples/bank/README.rdoc +261 -0
  12. data/examples/bank/Rakefile +7 -0
  13. data/examples/bank/app/assets/images/rails.png +0 -0
  14. data/examples/bank/app/assets/javascripts/accounts.js.coffee +3 -0
  15. data/examples/bank/app/assets/javascripts/application.js +15 -0
  16. data/examples/bank/app/assets/stylesheets/accounts.css.scss +3 -0
  17. data/examples/bank/app/assets/stylesheets/application.css +13 -0
  18. data/examples/bank/app/controllers/accounts_controller.rb +62 -0
  19. data/examples/bank/app/controllers/application_controller.rb +3 -0
  20. data/examples/bank/app/helpers/accounts_helper.rb +2 -0
  21. data/examples/bank/app/helpers/application_helper.rb +2 -0
  22. data/examples/bank/app/mailers/.gitkeep +0 -0
  23. data/examples/bank/app/models/.gitkeep +0 -0
  24. data/examples/bank/app/models/account.rb +65 -0
  25. data/examples/bank/app/views/accounts/deposit_form.html.erb +23 -0
  26. data/examples/bank/app/views/accounts/index.html.erb +22 -0
  27. data/examples/bank/app/views/accounts/transfer_form.html.erb +32 -0
  28. data/examples/bank/app/views/accounts/withdraw_form.html.erb +23 -0
  29. data/examples/bank/app/views/layouts/application.html.erb +15 -0
  30. data/examples/bank/config.ru +4 -0
  31. data/examples/bank/config/application.rb +65 -0
  32. data/examples/bank/config/boot.rb +6 -0
  33. data/examples/bank/config/environment.rb +5 -0
  34. data/examples/bank/config/environments/development.rb +31 -0
  35. data/examples/bank/config/environments/production.rb +64 -0
  36. data/examples/bank/config/environments/test.rb +35 -0
  37. data/examples/bank/config/initializers/accounts.rb +6 -0
  38. data/examples/bank/config/initializers/backtrace_silencers.rb +7 -0
  39. data/examples/bank/config/initializers/inflections.rb +15 -0
  40. data/examples/bank/config/initializers/mime_types.rb +5 -0
  41. data/examples/bank/config/initializers/secret_token.rb +7 -0
  42. data/examples/bank/config/initializers/session_store.rb +8 -0
  43. data/examples/bank/config/initializers/wrap_parameters.rb +10 -0
  44. data/examples/bank/config/locales/en.yml +5 -0
  45. data/examples/bank/config/routes.rb +12 -0
  46. data/examples/bank/db/seeds.rb +7 -0
  47. data/examples/bank/lib/assets/.gitkeep +0 -0
  48. data/examples/bank/lib/tasks/.gitkeep +0 -0
  49. data/examples/bank/log/.gitkeep +0 -0
  50. data/examples/bank/public/404.html +26 -0
  51. data/examples/bank/public/422.html +26 -0
  52. data/examples/bank/public/500.html +25 -0
  53. data/examples/bank/public/favicon.ico +0 -0
  54. data/examples/bank/public/robots.txt +5 -0
  55. data/examples/bank/script/rails +6 -0
  56. data/examples/bank/test/fixtures/.gitkeep +0 -0
  57. data/examples/bank/test/functional/.gitkeep +0 -0
  58. data/examples/bank/test/functional/accounts_controller_test.rb +19 -0
  59. data/examples/bank/test/integration/.gitkeep +0 -0
  60. data/examples/bank/test/performance/browsing_test.rb +12 -0
  61. data/examples/bank/test/test_helper.rb +7 -0
  62. data/examples/bank/test/unit/.gitkeep +0 -0
  63. data/examples/bank/test/unit/helpers/accounts_helper_test.rb +4 -0
  64. data/examples/bank/vendor/assets/javascripts/.gitkeep +0 -0
  65. data/examples/bank/vendor/assets/stylesheets/.gitkeep +0 -0
  66. data/examples/bank/vendor/plugins/.gitkeep +0 -0
  67. data/lib/command_model.rb +7 -0
  68. data/lib/command_model/model.rb +173 -0
  69. data/lib/command_model/version.rb +3 -0
  70. data/spec/model_spec.rb +222 -0
  71. data/spec/spec_helper.rb +2 -0
  72. metadata +164 -0
@@ -0,0 +1,3 @@
1
+ module CommandModel
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,222 @@
1
+ require 'spec_helper'
2
+
3
+ class ExampleCommand < CommandModel::Model
4
+ parameter :name, :presence => true
5
+ end
6
+
7
+ describe CommandModel::Model do
8
+ let(:example_command) { ExampleCommand.new :name => "John" }
9
+ let(:invalid_example_command) { ExampleCommand.new }
10
+
11
+ describe "self.parameter" do
12
+ let(:klass) { Class.new(CommandModel::Model) }
13
+
14
+ it "creates an attribute reader" do
15
+ klass.parameter :foo
16
+ klass.new.methods.should include(:foo)
17
+ end
18
+
19
+ it "creates an attribute writer" do
20
+ klass.parameter :foo
21
+ klass.new.methods.should include(:foo=)
22
+ end
23
+
24
+ it "round trips values through writing and reading" do
25
+ klass.parameter :foo
26
+ instance = klass.new
27
+ instance.foo = 42
28
+ instance.foo.should eq(42)
29
+ end
30
+
31
+ it "accepts multiple attributes" do
32
+ klass.parameter :foo, :bar
33
+ klass.new.methods.should include(:foo)
34
+ klass.new.methods.should include(:foo=)
35
+ klass.new.methods.should include(:bar)
36
+ klass.new.methods.should include(:bar=)
37
+ end
38
+
39
+ it "accepts multiple attributes with typecast" do
40
+ klass.parameter :foo, :bar, :typecast => "integer"
41
+ klass.new.methods.should include(:foo)
42
+ klass.new.methods.should include(:foo=)
43
+ klass.new.methods.should include(:bar)
44
+ klass.new.methods.should include(:bar=)
45
+ end
46
+
47
+ it "accepts multiple attributes with validation" do
48
+ klass.parameter :foo, :bar, :presence => true
49
+ klass.new.methods.should include(:foo)
50
+ klass.new.methods.should include(:foo=)
51
+ klass.new.methods.should include(:bar)
52
+ klass.new.methods.should include(:bar=)
53
+ end
54
+
55
+ it "creates typecasting writer" do
56
+ klass.send(:define_method, :typecast_42) { |value| 42 }
57
+ klass.parameter :answer, :typecast => "42"
58
+ instance = klass.new
59
+ instance.answer = "foo"
60
+ instance.answer.should eq(42)
61
+ end
62
+
63
+ it "creates validations" do
64
+ instance = ExampleCommand.new
65
+ instance.should_not be_valid
66
+ instance.errors[:name].should be_present
67
+ end
68
+ end
69
+
70
+
71
+ describe "self.execute" do
72
+ it "accepts object of same kind and returns it" do
73
+ ExampleCommand.execute(example_command) {}.should eq(example_command)
74
+ end
75
+
76
+ it "accepts attributes, creates object, and returns it" do
77
+ c = ExampleCommand.execute(:name => "John") {}
78
+ c.should be_kind_of(ExampleCommand)
79
+ c.name.should eq("John")
80
+ end
81
+
82
+ it "calls passed block when there are no validation errors on Model" do
83
+ block_ran = false
84
+ ExampleCommand.execute(example_command) { block_ran = true }
85
+ block_ran.should eq(true)
86
+ end
87
+
88
+ it "does not call passed block when there are validation errors on Model" do
89
+ block_ran = false
90
+ ExampleCommand.execute(invalid_example_command) { block_ran = true }
91
+ block_ran.should eq(false)
92
+ end
93
+
94
+ it "records execution attempt when there not no validation errors on Model" do
95
+ ExampleCommand.execute(example_command) {}
96
+ example_command.execution_attempted?.should eq(true)
97
+ end
98
+
99
+ it "records execution attempt when there are validation errors on Model" do
100
+ ExampleCommand.execute(invalid_example_command) {}
101
+ invalid_example_command.execution_attempted?.should eq(true)
102
+ end
103
+
104
+ it "is not successful if block adds error to Model" do
105
+ ExampleCommand.execute(example_command) do |command|
106
+ command.errors.add :base, "foo"
107
+ end
108
+
109
+ example_command.should_not be_success
110
+ end
111
+ end
112
+
113
+ describe "self.success" do
114
+ it "creates a successful command model" do
115
+ response = ExampleCommand.success
116
+ response.should be_kind_of(ExampleCommand)
117
+ response.should be_success
118
+ end
119
+ end
120
+
121
+ describe "self.failure" do
122
+ it "creates a command model with an error" do
123
+ response = ExampleCommand.failure "something broke"
124
+ response.should be_kind_of(ExampleCommand)
125
+ response.should_not be_success
126
+ response.errors[:base].should eq(["something broke"])
127
+ end
128
+ end
129
+
130
+
131
+ describe "initialize" do
132
+ it "assigns attributes" do
133
+ m = ExampleCommand.new :name => "John"
134
+ m.name.should eq("John")
135
+ end
136
+ end
137
+
138
+ describe "execution_attempted!" do
139
+ it "sets execution_attempted? to true" do
140
+ example_command.execution_attempted!
141
+ example_command.execution_attempted?.should eq(true)
142
+ end
143
+ end
144
+
145
+ describe "success?" do
146
+ it "is false before execution" do
147
+ example_command.should_not be_success
148
+ end
149
+
150
+ it "is false after execution with errors" do
151
+ example_command.execution_attempted!
152
+ example_command.errors.add :base, "foo"
153
+ example_command.success?.should eq(false)
154
+ end
155
+
156
+ it "is true after execution without errors" do
157
+ example_command.execution_attempted!
158
+ example_command.success?.should eq(true)
159
+ end
160
+ end
161
+
162
+ describe "typecast_integer" do
163
+ it "casts to integer when valid string" do
164
+ example_command.send(:typecast_integer, "42").should eq(42)
165
+ end
166
+
167
+ it "returns nil when invalid string" do
168
+ example_command.send(:typecast_integer, "asdf").should be_nil
169
+ example_command.send(:typecast_integer, nil).should be_nil
170
+ example_command.send(:typecast_integer, "").should be_nil
171
+ example_command.send(:typecast_integer, "0.1").should be_nil
172
+ end
173
+ end
174
+
175
+ describe "typecast_float" do
176
+ it "casts to float when valid string" do
177
+ example_command.send(:typecast_float, "42").should eq(42.0)
178
+ example_command.send(:typecast_float, "42.5").should eq(42.5)
179
+ end
180
+
181
+ it "returns nil when invalid string" do
182
+ example_command.send(:typecast_float, "asdf").should be_nil
183
+ example_command.send(:typecast_float, nil).should be_nil
184
+ example_command.send(:typecast_float, "").should be_nil
185
+ end
186
+ end
187
+
188
+ describe "typecast_date" do
189
+ it "casts to date when valid string" do
190
+ example_command.send(:typecast_date, "01/01/2000").should eq(Date.civil(2000,1,1))
191
+ example_command.send(:typecast_date, "1/1/2000").should eq(Date.civil(2000,1,1))
192
+ example_command.send(:typecast_date, "2000-01-01").should eq(Date.civil(2000,1,1))
193
+ end
194
+
195
+ it "returns existing date unchanged" do
196
+ date = Date.civil(2000,1,1)
197
+ example_command.send(:typecast_date, date).should eq(date)
198
+ end
199
+
200
+ it "returns nil when invalid string" do
201
+ example_command.send(:typecast_date, "asdf").should be_nil
202
+ example_command.send(:typecast_date, nil).should be_nil
203
+ example_command.send(:typecast_date, "").should be_nil
204
+ example_command.send(:typecast_date, "3/50/1290").should be_nil
205
+ end
206
+ end
207
+
208
+ it "includes typecasting errors in validations" do
209
+ example_command.instance_variable_get(:@typecast_errors)["name"] = "integer"
210
+ example_command.should_not be_valid
211
+ example_command.errors["name"].should be
212
+ end
213
+
214
+ it "does not include typecasting error in validations if the attribute already has an error" do
215
+ invalid_example_command.instance_variable_get(:@typecast_errors)["name"] = "integer"
216
+ invalid_example_command.should_not be_valid
217
+ invalid_example_command.errors["name"].should be
218
+ invalid_example_command.errors["name"].find { |e| e =~ /integer/ }.should_not be
219
+ end
220
+
221
+
222
+ end
@@ -0,0 +1,2 @@
1
+ require 'command_model'
2
+ require 'rspec'
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: command_model
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jack Christensen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: &15163980 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *15163980
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &15163060 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.9.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *15163060
36
+ - !ruby/object:Gem::Dependency
37
+ name: guard
38
+ requirement: &15162120 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *15162120
47
+ - !ruby/object:Gem::Dependency
48
+ name: guard-rspec
49
+ requirement: &15161240 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.7.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *15161240
58
+ description: CommandModel - when update_attributes isn't enough.
59
+ email:
60
+ - jack@jackchristensen.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - .rspec
67
+ - Gemfile
68
+ - Guardfile
69
+ - LICENSE
70
+ - README.md
71
+ - Rakefile
72
+ - command_model.gemspec
73
+ - examples/bank/.gitignore
74
+ - examples/bank/Gemfile
75
+ - examples/bank/README.rdoc
76
+ - examples/bank/Rakefile
77
+ - examples/bank/app/assets/images/rails.png
78
+ - examples/bank/app/assets/javascripts/accounts.js.coffee
79
+ - examples/bank/app/assets/javascripts/application.js
80
+ - examples/bank/app/assets/stylesheets/accounts.css.scss
81
+ - examples/bank/app/assets/stylesheets/application.css
82
+ - examples/bank/app/controllers/accounts_controller.rb
83
+ - examples/bank/app/controllers/application_controller.rb
84
+ - examples/bank/app/helpers/accounts_helper.rb
85
+ - examples/bank/app/helpers/application_helper.rb
86
+ - examples/bank/app/mailers/.gitkeep
87
+ - examples/bank/app/models/.gitkeep
88
+ - examples/bank/app/models/account.rb
89
+ - examples/bank/app/views/accounts/deposit_form.html.erb
90
+ - examples/bank/app/views/accounts/index.html.erb
91
+ - examples/bank/app/views/accounts/transfer_form.html.erb
92
+ - examples/bank/app/views/accounts/withdraw_form.html.erb
93
+ - examples/bank/app/views/layouts/application.html.erb
94
+ - examples/bank/config.ru
95
+ - examples/bank/config/application.rb
96
+ - examples/bank/config/boot.rb
97
+ - examples/bank/config/environment.rb
98
+ - examples/bank/config/environments/development.rb
99
+ - examples/bank/config/environments/production.rb
100
+ - examples/bank/config/environments/test.rb
101
+ - examples/bank/config/initializers/accounts.rb
102
+ - examples/bank/config/initializers/backtrace_silencers.rb
103
+ - examples/bank/config/initializers/inflections.rb
104
+ - examples/bank/config/initializers/mime_types.rb
105
+ - examples/bank/config/initializers/secret_token.rb
106
+ - examples/bank/config/initializers/session_store.rb
107
+ - examples/bank/config/initializers/wrap_parameters.rb
108
+ - examples/bank/config/locales/en.yml
109
+ - examples/bank/config/routes.rb
110
+ - examples/bank/db/seeds.rb
111
+ - examples/bank/lib/assets/.gitkeep
112
+ - examples/bank/lib/tasks/.gitkeep
113
+ - examples/bank/log/.gitkeep
114
+ - examples/bank/public/404.html
115
+ - examples/bank/public/422.html
116
+ - examples/bank/public/500.html
117
+ - examples/bank/public/favicon.ico
118
+ - examples/bank/public/robots.txt
119
+ - examples/bank/script/rails
120
+ - examples/bank/test/fixtures/.gitkeep
121
+ - examples/bank/test/functional/.gitkeep
122
+ - examples/bank/test/functional/accounts_controller_test.rb
123
+ - examples/bank/test/integration/.gitkeep
124
+ - examples/bank/test/performance/browsing_test.rb
125
+ - examples/bank/test/test_helper.rb
126
+ - examples/bank/test/unit/.gitkeep
127
+ - examples/bank/test/unit/helpers/accounts_helper_test.rb
128
+ - examples/bank/vendor/assets/javascripts/.gitkeep
129
+ - examples/bank/vendor/assets/stylesheets/.gitkeep
130
+ - examples/bank/vendor/plugins/.gitkeep
131
+ - lib/command_model.rb
132
+ - lib/command_model/model.rb
133
+ - lib/command_model/version.rb
134
+ - spec/model_spec.rb
135
+ - spec/spec_helper.rb
136
+ homepage: https://github.com/JackC/command_model
137
+ licenses: []
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ! '>='
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 1.8.17
157
+ signing_key:
158
+ specification_version: 3
159
+ summary: CommandModel integrates Rails validations with command objects. This allows
160
+ errors from command execution to easily be handled with the familiar Rails validation
161
+ system.
162
+ test_files:
163
+ - spec/model_spec.rb
164
+ - spec/spec_helper.rb