lev 2.0.5 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZDA3ZTU4MzVlZmQxZDAyYzc3NzUyMmMxNmU5MmQ3MWM1ODE5MDhmZQ==
5
+ data.tar.gz: !binary |-
6
+ NzkyYzQ5NDRmMDk1NzgzNmY1YzhhM2Q2OGIwOGVkNzAyNDU1ZmIzNQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NzhhZmQ3ZDdjM2IxMTRiNGQ2NGI3ZjgyNWQ5NTUzNWY2MmVkYWJlMDUyOTE2
10
+ MzU3MTMzZmNjNGIxYmQ2ZDQ2YjdhMDNiMmU2MTAyZDNjYWE5YjNmMjU2ZWFi
11
+ NjFlMGM3ZDM1NzQ4YjZjNTMwZjhmOGE0OGNhNzgwN2FiYzVhOTU=
12
+ data.tar.gz: !binary |-
13
+ Njc3OTEwZjJhZjQyNTZhMDM2ZGFiYjc0ZGMyN2IzMjA5NTFiNTk5ZTgxNWYx
14
+ YTNlMGEyODQ5Y2Y1ZWUyMGUwZmI2ZGI4MzhiYmYyYjUwYjZmNDg5Y2QzMDQ3
15
+ MDU1MzRlMWJlZDBmNWE3MjY3OTBmYzg2NGYwMzU0MTUyODBiZWU=
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --color
2
2
  --format progress
3
+ --debug
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lev (2.0.5)
4
+ lev (2.1.0)
5
5
  actionpack (>= 3.0)
6
6
  active_attr
7
7
  activemodel (>= 3.0)
@@ -13,33 +13,33 @@ PATH
13
13
  GEM
14
14
  remote: https://rubygems.org/
15
15
  specs:
16
- actionpack (4.0.3)
17
- activesupport (= 4.0.3)
18
- builder (~> 3.1.0)
19
- erubis (~> 2.7.0)
16
+ actionpack (4.1.1)
17
+ actionview (= 4.1.1)
18
+ activesupport (= 4.1.1)
20
19
  rack (~> 1.5.2)
21
20
  rack-test (~> 0.6.2)
22
- active_attr (0.8.2)
23
- activemodel (>= 3.0.2, < 4.1)
24
- activesupport (>= 3.0.2, < 4.1)
25
- activemodel (4.0.3)
26
- activesupport (= 4.0.3)
27
- builder (~> 3.1.0)
28
- activerecord (4.0.3)
29
- activemodel (= 4.0.3)
30
- activerecord-deprecated_finders (~> 1.0.2)
31
- activesupport (= 4.0.3)
32
- arel (~> 4.0.0)
33
- activerecord-deprecated_finders (1.0.3)
34
- activesupport (4.0.3)
35
- i18n (~> 0.6, >= 0.6.4)
36
- minitest (~> 4.2)
37
- multi_json (~> 1.3)
21
+ actionview (4.1.1)
22
+ activesupport (= 4.1.1)
23
+ builder (~> 3.1)
24
+ erubis (~> 2.7.0)
25
+ active_attr (0.8.3)
26
+ activemodel (>= 3.0.2, < 4.2)
27
+ activesupport (>= 3.0.2, < 4.2)
28
+ activemodel (4.1.1)
29
+ activesupport (= 4.1.1)
30
+ builder (~> 3.1)
31
+ activerecord (4.1.1)
32
+ activemodel (= 4.1.1)
33
+ activesupport (= 4.1.1)
34
+ arel (~> 5.0.0)
35
+ activesupport (4.1.1)
36
+ i18n (~> 0.6, >= 0.6.9)
37
+ json (~> 1.7, >= 1.7.7)
38
+ minitest (~> 5.1)
38
39
  thread_safe (~> 0.1)
39
- tzinfo (~> 0.3.37)
40
- arel (4.0.2)
41
- atomic (1.1.15)
42
- builder (3.1.4)
40
+ tzinfo (~> 1.1)
41
+ arel (5.0.1.20140414130214)
42
+ builder (3.2.2)
43
43
  columnize (0.3.6)
44
44
  debugger (1.6.2)
45
45
  columnize (>= 0.3.1)
@@ -49,10 +49,10 @@ GEM
49
49
  debugger-ruby_core_source (1.2.3)
50
50
  diff-lcs (1.2.4)
51
51
  erubis (2.7.0)
52
- hashie (2.0.5)
52
+ hashie (2.1.1)
53
53
  i18n (0.6.9)
54
- minitest (4.7.5)
55
- multi_json (1.8.4)
54
+ json (1.8.1)
55
+ minitest (5.3.4)
56
56
  rack (1.5.2)
57
57
  rack-test (0.6.2)
58
58
  rack (>= 1.0)
@@ -66,14 +66,14 @@ GEM
66
66
  diff-lcs (>= 1.1.3, < 2.0)
67
67
  rspec-mocks (2.14.3)
68
68
  sqlite3 (1.3.9)
69
- thread_safe (0.2.0)
70
- atomic (>= 1.1.7, < 2)
69
+ thread_safe (0.3.4)
71
70
  transaction_isolation (1.0.3)
72
71
  activerecord (>= 3.0.11)
73
72
  transaction_retry (1.0.2)
74
73
  activerecord (>= 3.0.11)
75
74
  transaction_isolation (>= 1.0.2)
76
- tzinfo (0.3.38)
75
+ tzinfo (1.2.0)
76
+ thread_safe (~> 0.1)
77
77
 
78
78
  PLATFORMS
79
79
  ruby
data/README.md CHANGED
@@ -28,6 +28,7 @@ Handlers...
28
28
  1. Help you verify that the calling user is authorized to run the handler
29
29
  2. Provide ways to validate incoming parameters in a very ActiveModel-like way (even when the parameters are not associated with a model)
30
30
  3. Integrate will with basic routines
31
+ 4. Map one-to-one with controller actions; by keeping the logic in each controller action encapsulated in a Handler, the code becomes independently-testable and also prevents the controller from being "fat" with 7 different actions all containing disparate logic touching different models.
31
32
 
32
33
  In a Lev-oriented Rails app, controllers are just responsible for connecting routes to Handlers. In fact, controller methods just end up being calls to ```handle_with(MyHandler)```, ```handle_with``` being a helper method provided by Lev.
33
34
 
@@ -46,6 +47,20 @@ When using Lev, model classes have the following responsibilities:
46
47
 
47
48
  The result of the principles above and below, model classes end up being very small. This is good because a lot of code depends on the models and having the be small normally means they are also stable.
48
49
 
50
+ ## Naming Conventions
51
+
52
+ As mentioned above, a handler is intended to replace the logic in one controller action. As such, one convention that works well is to name a handler based on the controller name and the action name, e.g. for the `ProductsController#show` action, we would have a handler named `ProductsShow`.
53
+
54
+ Routines on the other hand are more or less glorified functions that work with multiple models to get something done, so we typically start their names with verbs, e.g. `CreateUser`, `SetPassword`, `ConfirmEmail`, etc.
55
+
56
+ ## Differences between Lev and Rails' Concerns
57
+
58
+ Both Lev and Concerns remove lines of code from models, but the major difference between the two is that with Concerns, the code still lives logically in the models whereas code in Lev is completely outside of and separate from the models.
59
+
60
+ Lev's routines (and handlers) know about models, but the models don't know anything about nor are they dependent on the code in routines. This makes the models simpler and more stable (a Good Thing).
61
+
62
+ Since a Concern's code is essentially embedded in model code, if that Concern breaks it can potentially break other unrelated features, something that can't happen with routines.
63
+
49
64
  ## Installation
50
65
 
51
66
  Add this line to your application's Gemfile:
@@ -31,8 +31,10 @@ module Lev
31
31
  # instance objects based on the params.
32
32
  # 2) Call the class method "paramify" to declare, cast, and validate parts of
33
33
  # the params hash. The first argument to paramify is the key in params
34
- # which points to a hash of params to be paramified. The block passed to
35
- # paramify looks just like the guts of an ActiveAttr model.
34
+ # which points to a hash of params to be paramified. If this first argument
35
+ # is unspecified (or specified as `:paramify`, a reserved symbol), the entire
36
+ # params hash will be paramified. The block passed to paramify looks just
37
+ # like the guts of an ActiveAttr model.
36
38
  #
37
39
  # When the incoming params includes :search => {:type, :terms, :num_results}
38
40
  # the Handler class would look like:
@@ -122,7 +124,8 @@ module Lev
122
124
  call(options)
123
125
  end
124
126
 
125
- def paramify(group, options={}, &block)
127
+ def paramify(group = :paramify, options={}, &block)
128
+
126
129
  method_name = "#{group.to_s}_params"
127
130
  variable_sym = "@#{method_name}".to_sym
128
131
 
@@ -145,11 +148,15 @@ module Lev
145
148
  end
146
149
 
147
150
  # Define the "#{group}_params" method to get the paramifier
148
- # instance wrapping the params
151
+ # instance wrapping the params. Choose the subset of params
152
+ # based on the group, choosing all params if the default group
153
+ # is used.
154
+
149
155
  define_method method_name.to_sym do
150
156
  if !instance_variable_get(variable_sym)
157
+ params_subset = group == :paramify ? params : params[group]
151
158
  instance_variable_set(variable_sym,
152
- self.class.paramify_classes[group].new(params[group]))
159
+ self.class.paramify_classes[group].new(params_subset))
153
160
  end
154
161
  instance_variable_get(variable_sym)
155
162
  end
@@ -1,3 +1,3 @@
1
1
  module Lev
2
- VERSION = "2.0.5"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe ParamifyHandlerA do
4
+ it 'should error out on badly formatted params' do
5
+ result = ParamifyHandlerA.handle(params: {terms: {type: 'blah'}})
6
+ errors = result.errors.collect { |error| error.translate }
7
+ expect(errors).to eq(['Type is not valid'])
8
+ end
9
+ end
10
+
11
+ describe ParamifyHandlerB do
12
+ it 'should error out on badly formatted ungrouped params' do
13
+ result = ParamifyHandlerB.handle(params: {type: 'blah'})
14
+ errors = result.errors.collect { |error| error.translate }
15
+ expect(errors).to eq(['Type is not valid'])
16
+ end
17
+
18
+ it 'should provide access to top-level params' do
19
+ result = ParamifyHandlerB.handle(params: {type: 'Name', value: 2})
20
+ errors = result.errors.collect { |error| error.translate }
21
+ expect(result.outputs.success).to eq true
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ class ParamifyHandlerA
2
+
3
+ include Lev::Handler
4
+
5
+ paramify :terms do
6
+ attribute :type, type: String
7
+ validates :type, presence: true,
8
+ inclusion: { in: %w(Name Username Any),
9
+ message: "is not valid" }
10
+ end
11
+
12
+ protected
13
+
14
+ def authorized?; true; end
15
+ def handle; end
16
+
17
+ end
@@ -0,0 +1,22 @@
1
+ class ParamifyHandlerB
2
+
3
+ include Lev::Handler
4
+
5
+ paramify do
6
+ attribute :type, type: String
7
+ validates :type, presence: true,
8
+ inclusion: { in: %w(Name Username Any),
9
+ message: "is not valid" }
10
+
11
+ attribute :value, type: Integer
12
+ end
13
+
14
+ protected
15
+
16
+ def authorized?; true; end
17
+
18
+ def handle
19
+ outputs[:success] = true if paramify_params.value == 2
20
+ end
21
+
22
+ end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lev
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.5
5
- prerelease:
4
+ version: 2.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - JP Slavinsky
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-03-03 00:00:00.000000000 Z
11
+ date: 2014-05-28 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activemodel
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: activerecord
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ! '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ! '>='
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: actionpack
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ! '>='
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ! '>='
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: transaction_isolation
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ! '>='
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :runtime
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ! '>='
76
67
  - !ruby/object:Gem::Version
@@ -78,7 +69,6 @@ dependencies:
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: transaction_retry
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
73
  - - ! '>='
84
74
  - !ruby/object:Gem::Version
@@ -86,7 +76,6 @@ dependencies:
86
76
  type: :runtime
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
80
  - - ! '>='
92
81
  - !ruby/object:Gem::Version
@@ -94,7 +83,6 @@ dependencies:
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: active_attr
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
87
  - - ! '>='
100
88
  - !ruby/object:Gem::Version
@@ -102,7 +90,6 @@ dependencies:
102
90
  type: :runtime
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
94
  - - ! '>='
108
95
  - !ruby/object:Gem::Version
@@ -110,7 +97,6 @@ dependencies:
110
97
  - !ruby/object:Gem::Dependency
111
98
  name: hashie
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
101
  - - ! '>='
116
102
  - !ruby/object:Gem::Version
@@ -118,7 +104,6 @@ dependencies:
118
104
  type: :runtime
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
107
  requirements:
123
108
  - - ! '>='
124
109
  - !ruby/object:Gem::Version
@@ -126,7 +111,6 @@ dependencies:
126
111
  - !ruby/object:Gem::Dependency
127
112
  name: bundler
128
113
  requirement: !ruby/object:Gem::Requirement
129
- none: false
130
114
  requirements:
131
115
  - - ~>
132
116
  - !ruby/object:Gem::Version
@@ -134,7 +118,6 @@ dependencies:
134
118
  type: :development
135
119
  prerelease: false
136
120
  version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
121
  requirements:
139
122
  - - ~>
140
123
  - !ruby/object:Gem::Version
@@ -142,7 +125,6 @@ dependencies:
142
125
  - !ruby/object:Gem::Dependency
143
126
  name: rake
144
127
  requirement: !ruby/object:Gem::Requirement
145
- none: false
146
128
  requirements:
147
129
  - - ! '>='
148
130
  - !ruby/object:Gem::Version
@@ -150,7 +132,6 @@ dependencies:
150
132
  type: :development
151
133
  prerelease: false
152
134
  version_requirements: !ruby/object:Gem::Requirement
153
- none: false
154
135
  requirements:
155
136
  - - ! '>='
156
137
  - !ruby/object:Gem::Version
@@ -158,7 +139,6 @@ dependencies:
158
139
  - !ruby/object:Gem::Dependency
159
140
  name: rspec
160
141
  requirement: !ruby/object:Gem::Requirement
161
- none: false
162
142
  requirements:
163
143
  - - ! '>='
164
144
  - !ruby/object:Gem::Version
@@ -166,7 +146,6 @@ dependencies:
166
146
  type: :development
167
147
  prerelease: false
168
148
  version_requirements: !ruby/object:Gem::Requirement
169
- none: false
170
149
  requirements:
171
150
  - - ! '>='
172
151
  - !ruby/object:Gem::Version
@@ -174,7 +153,6 @@ dependencies:
174
153
  - !ruby/object:Gem::Dependency
175
154
  name: sqlite3
176
155
  requirement: !ruby/object:Gem::Requirement
177
- none: false
178
156
  requirements:
179
157
  - - ! '>='
180
158
  - !ruby/object:Gem::Version
@@ -182,7 +160,6 @@ dependencies:
182
160
  type: :development
183
161
  prerelease: false
184
162
  version_requirements: !ruby/object:Gem::Requirement
185
- none: false
186
163
  requirements:
187
164
  - - ! '>='
188
165
  - !ruby/object:Gem::Version
@@ -190,7 +167,6 @@ dependencies:
190
167
  - !ruby/object:Gem::Dependency
191
168
  name: debugger
192
169
  requirement: !ruby/object:Gem::Requirement
193
- none: false
194
170
  requirements:
195
171
  - - ! '>='
196
172
  - !ruby/object:Gem::Version
@@ -198,7 +174,6 @@ dependencies:
198
174
  type: :development
199
175
  prerelease: false
200
176
  version_requirements: !ruby/object:Gem::Requirement
201
- none: false
202
177
  requirements:
203
178
  - - ! '>='
204
179
  - !ruby/object:Gem::Version
@@ -243,52 +218,51 @@ files:
243
218
  - spec/create_sprocket_spec.rb
244
219
  - spec/deep_merge_spec.rb
245
220
  - spec/outputs_spec.rb
221
+ - spec/paramify_handler_spec.rb
246
222
  - spec/routine_spec.rb
247
223
  - spec/spec_helper.rb
248
224
  - spec/sprocket_handler_spec.rb
249
225
  - spec/sprocket_spec.rb
250
226
  - spec/support/create_sprocket.rb
227
+ - spec/support/paramify_handler_a.rb
228
+ - spec/support/paramify_handler_b.rb
251
229
  - spec/support/sprocket.rb
252
230
  - spec/support/sprocket_handler.rb
253
231
  homepage: http://github.com/lml/lev
254
232
  licenses:
255
233
  - MIT
234
+ metadata: {}
256
235
  post_install_message:
257
236
  rdoc_options: []
258
237
  require_paths:
259
238
  - lib
260
239
  required_ruby_version: !ruby/object:Gem::Requirement
261
- none: false
262
240
  requirements:
263
241
  - - ! '>='
264
242
  - !ruby/object:Gem::Version
265
243
  version: '0'
266
- segments:
267
- - 0
268
- hash: -2969297821958856203
269
244
  required_rubygems_version: !ruby/object:Gem::Requirement
270
- none: false
271
245
  requirements:
272
246
  - - ! '>='
273
247
  - !ruby/object:Gem::Version
274
248
  version: '0'
275
- segments:
276
- - 0
277
- hash: -2969297821958856203
278
249
  requirements: []
279
250
  rubyforge_project:
280
- rubygems_version: 1.8.25
251
+ rubygems_version: 2.2.2
281
252
  signing_key:
282
- specification_version: 3
253
+ specification_version: 4
283
254
  summary: Ride the rails but don't touch them.
284
255
  test_files:
285
256
  - spec/create_sprocket_spec.rb
286
257
  - spec/deep_merge_spec.rb
287
258
  - spec/outputs_spec.rb
259
+ - spec/paramify_handler_spec.rb
288
260
  - spec/routine_spec.rb
289
261
  - spec/spec_helper.rb
290
262
  - spec/sprocket_handler_spec.rb
291
263
  - spec/sprocket_spec.rb
292
264
  - spec/support/create_sprocket.rb
265
+ - spec/support/paramify_handler_a.rb
266
+ - spec/support/paramify_handler_b.rb
293
267
  - spec/support/sprocket.rb
294
268
  - spec/support/sprocket_handler.rb