sinatra-param 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b64e12a2936520c3207c50339213644d8610709c
4
- data.tar.gz: 41d317945c4128feb98c69f096797fc380932d38
3
+ metadata.gz: 02fe2998b85c6052f32119c3e1e72e760e0bf538
4
+ data.tar.gz: 313e3983abc73ddf696f75f3a566fdaf5a1b6680
5
5
  SHA512:
6
- metadata.gz: fef20f2728a516b7c19d85058d27119e9976e49f5f20d8a238de837c0637f7ad3fb2abc368ba48aa8a55ff8bcd8c93da48792bbba18c4ad47e48b8c95b79ea30
7
- data.tar.gz: e1aa8c9c2f62bf7c44e349401aae77f880b27a40c7d1109e00a54256ac01ce489315aff0756f0214a4ea3fee11af7361b86186f45fe43d427be87568a999243c
6
+ metadata.gz: 008d02adbb3f7aaeaea103da5ee486f72ac56fe05054dc713f74a5ed9e6ccdc82d5198c16c8c0803c93efadcbba5a779fcb30aee100614d9d88c22ae9c63907a
7
+ data.tar.gz: c980ebcc718992765de9a4b847c5ce90878ecc8854a6035ad6a9e036ad1452b2eca295f4f89116cc9e571f717fa0fc19ddbd25c9cda690a55bedc651349d5075
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sinatra-param (1.1.0)
4
+ sinatra-param (1.1.1)
5
5
  sinatra (~> 1.3)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -62,9 +62,19 @@ Encapsulate business logic in a consistent way with validations. If a parameter
62
62
 
63
63
  ### Defaults and Transformations
64
64
 
65
- Passing a `default` option will provide a default value for a parameter if none is passed.
65
+ Passing a `default` option will provide a default value for a parameter if none is passed. A `default` can defined as either a default or as a `Proc`:
66
66
 
67
- Use the `transform` option to take even more of the business logic of parameter I/O out of your code. Anything that responds to `to_proc` (including Procs and symbols) will do.
67
+ ```ruby
68
+ param :attribution, String, default: "©"
69
+ param :year, Integer, default: lambda { Time.now.year }
70
+ ```
71
+
72
+ Use the `transform` option to take even more of the business logic of parameter I/O out of your code. Anything that responds to `to_proc` (including `Proc` and symbols) will do.
73
+
74
+ ```ruby
75
+ param :order, String, in: ["ASC", "DESC"], transform: :upcase, default: "ASC"
76
+ param :offset, Integer, min: 0, transform: lambda {|n| n - (n % 10)}
77
+ ```
68
78
 
69
79
  ### Exceptions
70
80
 
@@ -72,14 +82,14 @@ By default, when a parameter precondition fails, `Sinatra::Param` will `halt 400
72
82
 
73
83
  ```json
74
84
  {
85
+ "message": "Invalid parameter, order",
75
86
  "errors": {
76
87
  "order": "Param must be within [\"ASC\", \"DESC\"]"
77
- },
78
- "message": "Invalid parameter, order"
88
+ }
79
89
  }
80
90
  ```
81
91
 
82
- To change this, you can set `:raise_sinatra_param_exceptions` to `true`, and intercept `Sinatra::Param::InvalidParameterError` with a Sinatra `error do...end` block. _(To make this work in development, set `:show_exceptions` to `false`)_:
92
+ To change this, you can set `:raise_sinatra_param_exceptions` to `true`, and intercept `Sinatra::Param::InvalidParameterError` with a Sinatra `error do...end` block. (To make this work in development, set `:show_exceptions` to `false`):
83
93
 
84
94
  ```ruby
85
95
  set :raise_sinatra_param_exceptions, true
data/coverage/index.html CHANGED
@@ -14,27 +14,27 @@
14
14
  <img src="./assets/0.7.1/loading.gif" alt="loading"/>
15
15
  </div>
16
16
  <div id="wrapper" style="display:none;">
17
- <div class="timestamp">Generated <abbr class="timeago" title="2013-12-02T12:12:13-08:00">2013-12-02T12:12:13-08:00</abbr></div>
17
+ <div class="timestamp">Generated <abbr class="timeago" title="2014-02-18T09:42:37-08:00">2014-02-18T09:42:37-08:00</abbr></div>
18
18
  <ul class="group_tabs"></ul>
19
19
 
20
20
  <div id="content">
21
21
  <div class="file_list_container" id="AllFiles">
22
22
  <h2>
23
23
  <span class="group_name">All Files</span>
24
- (<span class="covered_percent"><span class="green">100.0%</span></span>
24
+ (<span class="covered_percent"><span class="green">97.1%</span></span>
25
25
  covered at
26
26
  <span class="covered_strength">
27
27
  <span class="green">
28
- 18.4
28
+ 17.6
29
29
  </span>
30
30
  </span> hits/line)
31
31
  </h2>
32
32
  <a name="AllFiles"></a>
33
33
  <div>
34
34
  <b>1</b> files in total.
35
- <b>64</b> relevant lines.
36
- <span class="green"><b>64</b> lines covered</span> and
37
- <span class="red"><b>0</b> lines missed </span>
35
+ <b>69</b> relevant lines.
36
+ <span class="green"><b>67</b> lines covered</span> and
37
+ <span class="red"><b>2</b> lines missed </span>
38
38
  </div>
39
39
  <table class="file_list">
40
40
  <thead>
@@ -52,12 +52,12 @@
52
52
 
53
53
  <tr>
54
54
  <td class="strong"><a href="#0f7926ededdfd8418773c107837b6dc4182659b8" class="src_link" title="lib/sinatra/param.rb">lib/sinatra/param.rb</a></td>
55
- <td class="green strong">100.0 %</td>
56
- <td>112</td>
57
- <td>64</td>
58
- <td>64</td>
59
- <td>0</td>
60
- <td>18.4</td>
55
+ <td class="green strong">97.1 %</td>
56
+ <td>119</td>
57
+ <td>69</td>
58
+ <td>67</td>
59
+ <td>2</td>
60
+ <td>17.6</td>
61
61
  </tr>
62
62
 
63
63
  </tbody>
@@ -79,11 +79,11 @@
79
79
  <div class="source_table" id="0f7926ededdfd8418773c107837b6dc4182659b8">
80
80
  <div class="header">
81
81
  <h3>lib/sinatra/param.rb</h3>
82
- <h4><span class="green">100.0 %</span> covered</h4>
82
+ <h4><span class="green">97.1 %</span> covered</h4>
83
83
  <div>
84
- <b>64</b> relevant lines.
85
- <span class="green"><b>64</b> lines covered</span> and
86
- <span class="red"><b>0</b> lines missed.</span>
84
+ <b>69</b> relevant lines.
85
+ <span class="green"><b>67</b> lines covered</span> and
86
+ <span class="red"><b>2</b> lines missed.</span>
87
87
  </div>
88
88
  </div>
89
89
 
@@ -147,616 +147,658 @@
147
147
  <li class="covered" data-hits="1" data-linenumber="10">
148
148
  <span class="hits">1</span>
149
149
 
150
- <code class="ruby"> class InvalidParameterError &lt; StandardError; end</code>
150
+ <code class="ruby"> class InvalidParameterError &lt; StandardError</code>
151
151
  </li>
152
152
 
153
- <li class="never" data-hits="" data-linenumber="11">
153
+ <li class="covered" data-hits="1" data-linenumber="11">
154
+ <span class="hits">1</span>
155
+
156
+ <code class="ruby"> attr_accessor :param, :options</code>
157
+ </li>
158
+
159
+ <li class="never" data-hits="" data-linenumber="12">
160
+
161
+
162
+ <code class="ruby"> end</code>
163
+ </li>
164
+
165
+ <li class="never" data-hits="" data-linenumber="13">
154
166
 
155
167
 
156
168
  <code class="ruby"></code>
157
169
  </li>
158
170
 
159
- <li class="covered" data-hits="1" data-linenumber="12">
171
+ <li class="covered" data-hits="1" data-linenumber="14">
160
172
  <span class="hits">1</span>
161
173
 
162
174
  <code class="ruby"> def param(name, type, options = {})</code>
163
175
  </li>
164
176
 
165
- <li class="covered" data-hits="76" data-linenumber="13">
166
- <span class="hits">76</span>
177
+ <li class="covered" data-hits="77" data-linenumber="15">
178
+ <span class="hits">77</span>
167
179
 
168
180
  <code class="ruby"> name = name.to_s</code>
169
181
  </li>
170
182
 
171
- <li class="never" data-hits="" data-linenumber="14">
183
+ <li class="never" data-hits="" data-linenumber="16">
172
184
 
173
185
 
174
186
  <code class="ruby"></code>
175
187
  </li>
176
188
 
177
- <li class="covered" data-hits="76" data-linenumber="15">
178
- <span class="hits">76</span>
189
+ <li class="covered" data-hits="77" data-linenumber="17">
190
+ <span class="hits">77</span>
179
191
 
180
192
  <code class="ruby"> return unless params.member?(name) or present?(options[:default]) or options[:required]</code>
181
193
  </li>
182
194
 
183
- <li class="never" data-hits="" data-linenumber="16">
195
+ <li class="never" data-hits="" data-linenumber="18">
184
196
 
185
197
 
186
198
  <code class="ruby"></code>
187
199
  </li>
188
200
 
189
- <li class="covered" data-hits="67" data-linenumber="17">
190
- <span class="hits">67</span>
201
+ <li class="covered" data-hits="68" data-linenumber="19">
202
+ <span class="hits">68</span>
191
203
 
192
204
  <code class="ruby"> begin</code>
193
205
  </li>
194
206
 
195
- <li class="covered" data-hits="67" data-linenumber="18">
196
- <span class="hits">67</span>
207
+ <li class="covered" data-hits="68" data-linenumber="20">
208
+ <span class="hits">68</span>
197
209
 
198
210
  <code class="ruby"> params[name] = coerce(params[name], type, options)</code>
199
211
  </li>
200
212
 
201
- <li class="covered" data-hits="67" data-linenumber="19">
202
- <span class="hits">67</span>
213
+ <li class="covered" data-hits="68" data-linenumber="21">
214
+ <span class="hits">68</span>
203
215
 
204
- <code class="ruby"> params[name] = options[:default] if params[name].nil? and options[:default]</code>
216
+ <code class="ruby"> params[name] = (options[:default].call if options[:default].is_a?(Proc)) || options[:default] if params[name].nil? and options[:default]</code>
205
217
  </li>
206
218
 
207
- <li class="covered" data-hits="67" data-linenumber="20">
208
- <span class="hits">67</span>
219
+ <li class="covered" data-hits="68" data-linenumber="22">
220
+ <span class="hits">68</span>
209
221
 
210
222
  <code class="ruby"> params[name] = options[:transform].to_proc.call(params[name]) if options[:transform]</code>
211
223
  </li>
212
224
 
213
- <li class="covered" data-hits="67" data-linenumber="21">
214
- <span class="hits">67</span>
225
+ <li class="covered" data-hits="68" data-linenumber="23">
226
+ <span class="hits">68</span>
215
227
 
216
228
  <code class="ruby"> validate!(params[name], options)</code>
217
229
  </li>
218
230
 
219
- <li class="never" data-hits="" data-linenumber="22">
231
+ <li class="covered" data-hits="13" data-linenumber="24">
232
+ <span class="hits">13</span>
233
+
234
+ <code class="ruby"> rescue InvalidParameterError =&gt; exception</code>
235
+ </li>
236
+
237
+ <li class="covered" data-hits="13" data-linenumber="25">
238
+ <span class="hits">13</span>
239
+
240
+ <code class="ruby"> if options[:raise] or (settings.raise_sinatra_param_exceptions rescue false)</code>
241
+ </li>
242
+
243
+ <li class="missed" data-hits="0" data-linenumber="26">
244
+
245
+
246
+ <code class="ruby"> exception.param, exception.options = name, options</code>
247
+ </li>
248
+
249
+ <li class="missed" data-hits="0" data-linenumber="27">
220
250
 
221
251
 
222
- <code class="ruby"> rescue</code>
252
+ <code class="ruby"> raise exception</code>
223
253
  </li>
224
254
 
225
- <li class="covered" data-hits="13" data-linenumber="23">
255
+ <li class="never" data-hits="" data-linenumber="28">
256
+
257
+
258
+ <code class="ruby"> end</code>
259
+ </li>
260
+
261
+ <li class="never" data-hits="" data-linenumber="29">
262
+
263
+
264
+ <code class="ruby"></code>
265
+ </li>
266
+
267
+ <li class="covered" data-hits="13" data-linenumber="30">
226
268
  <span class="hits">13</span>
227
269
 
228
- <code class="ruby"> error = &quot;Invalid parameter, #{name}&quot;</code>
270
+ <code class="ruby"> error = &quot;Invalid Parameter: #{name}&quot;</code>
229
271
  </li>
230
272
 
231
- <li class="covered" data-hits="13" data-linenumber="24">
273
+ <li class="covered" data-hits="13" data-linenumber="31">
232
274
  <span class="hits">13</span>
233
275
 
234
276
  <code class="ruby"> if content_type and content_type.match(mime_type(:json))</code>
235
277
  </li>
236
278
 
237
- <li class="covered" data-hits="13" data-linenumber="25">
279
+ <li class="covered" data-hits="13" data-linenumber="32">
238
280
  <span class="hits">13</span>
239
281
 
240
- <code class="ruby"> error = {message: error}.to_json</code>
282
+ <code class="ruby"> error = {message: error, errors: {name =&gt; exception.message}}.to_json</code>
241
283
  </li>
242
284
 
243
- <li class="never" data-hits="" data-linenumber="26">
285
+ <li class="never" data-hits="" data-linenumber="33">
244
286
 
245
287
 
246
288
  <code class="ruby"> end</code>
247
289
  </li>
248
290
 
249
- <li class="never" data-hits="" data-linenumber="27">
291
+ <li class="never" data-hits="" data-linenumber="34">
250
292
 
251
293
 
252
294
  <code class="ruby"></code>
253
295
  </li>
254
296
 
255
- <li class="covered" data-hits="13" data-linenumber="28">
297
+ <li class="covered" data-hits="13" data-linenumber="35">
256
298
  <span class="hits">13</span>
257
299
 
258
300
  <code class="ruby"> halt 400, error</code>
259
301
  </li>
260
302
 
261
- <li class="never" data-hits="" data-linenumber="29">
303
+ <li class="never" data-hits="" data-linenumber="36">
262
304
 
263
305
 
264
306
  <code class="ruby"> end</code>
265
307
  </li>
266
308
 
267
- <li class="never" data-hits="" data-linenumber="30">
309
+ <li class="never" data-hits="" data-linenumber="37">
268
310
 
269
311
 
270
312
  <code class="ruby"> end</code>
271
313
  </li>
272
314
 
273
- <li class="never" data-hits="" data-linenumber="31">
315
+ <li class="never" data-hits="" data-linenumber="38">
274
316
 
275
317
 
276
318
  <code class="ruby"></code>
277
319
  </li>
278
320
 
279
- <li class="covered" data-hits="1" data-linenumber="32">
321
+ <li class="covered" data-hits="1" data-linenumber="39">
280
322
  <span class="hits">1</span>
281
323
 
282
324
  <code class="ruby"> def one_of(*names)</code>
283
325
  </li>
284
326
 
285
- <li class="covered" data-hits="6" data-linenumber="33">
327
+ <li class="covered" data-hits="6" data-linenumber="40">
286
328
  <span class="hits">6</span>
287
329
 
288
330
  <code class="ruby"> count = 0</code>
289
331
  </li>
290
332
 
291
- <li class="covered" data-hits="6" data-linenumber="34">
333
+ <li class="covered" data-hits="6" data-linenumber="41">
292
334
  <span class="hits">6</span>
293
335
 
294
336
  <code class="ruby"> names.each do |name|</code>
295
337
  </li>
296
338
 
297
- <li class="covered" data-hits="16" data-linenumber="35">
339
+ <li class="covered" data-hits="16" data-linenumber="42">
298
340
  <span class="hits">16</span>
299
341
 
300
342
  <code class="ruby"> if params[name] and present?(params[name])</code>
301
343
  </li>
302
344
 
303
- <li class="covered" data-hits="9" data-linenumber="36">
345
+ <li class="covered" data-hits="9" data-linenumber="43">
304
346
  <span class="hits">9</span>
305
347
 
306
348
  <code class="ruby"> count += 1</code>
307
349
  </li>
308
350
 
309
- <li class="covered" data-hits="9" data-linenumber="37">
351
+ <li class="covered" data-hits="9" data-linenumber="44">
310
352
  <span class="hits">9</span>
311
353
 
312
354
  <code class="ruby"> next unless count &gt; 1</code>
313
355
  </li>
314
356
 
315
- <li class="never" data-hits="" data-linenumber="38">
357
+ <li class="never" data-hits="" data-linenumber="45">
316
358
 
317
359
 
318
360
  <code class="ruby"></code>
319
361
  </li>
320
362
 
321
- <li class="covered" data-hits="3" data-linenumber="39">
363
+ <li class="covered" data-hits="3" data-linenumber="46">
322
364
  <span class="hits">3</span>
323
365
 
324
366
  <code class="ruby"> error = &quot;Parameters #{names.join(&#39;, &#39;)} are mutually exclusive&quot;</code>
325
367
  </li>
326
368
 
327
- <li class="covered" data-hits="3" data-linenumber="40">
369
+ <li class="covered" data-hits="3" data-linenumber="47">
328
370
  <span class="hits">3</span>
329
371
 
330
372
  <code class="ruby"> if content_type and content_type.match(mime_type(:json))</code>
331
373
  </li>
332
374
 
333
- <li class="covered" data-hits="3" data-linenumber="41">
375
+ <li class="covered" data-hits="3" data-linenumber="48">
334
376
  <span class="hits">3</span>
335
377
 
336
378
  <code class="ruby"> error = {message: error}.to_json</code>
337
379
  </li>
338
380
 
339
- <li class="never" data-hits="" data-linenumber="42">
381
+ <li class="never" data-hits="" data-linenumber="49">
340
382
 
341
383
 
342
384
  <code class="ruby"> end</code>
343
385
  </li>
344
386
 
345
- <li class="never" data-hits="" data-linenumber="43">
387
+ <li class="never" data-hits="" data-linenumber="50">
346
388
 
347
389
 
348
390
  <code class="ruby"></code>
349
391
  </li>
350
392
 
351
- <li class="covered" data-hits="3" data-linenumber="44">
393
+ <li class="covered" data-hits="3" data-linenumber="51">
352
394
  <span class="hits">3</span>
353
395
 
354
396
  <code class="ruby"> halt 400, error</code>
355
397
  </li>
356
398
 
357
- <li class="never" data-hits="" data-linenumber="45">
399
+ <li class="never" data-hits="" data-linenumber="52">
358
400
 
359
401
 
360
402
  <code class="ruby"> end</code>
361
403
  </li>
362
404
 
363
- <li class="never" data-hits="" data-linenumber="46">
405
+ <li class="never" data-hits="" data-linenumber="53">
364
406
 
365
407
 
366
408
  <code class="ruby"> end</code>
367
409
  </li>
368
410
 
369
- <li class="never" data-hits="" data-linenumber="47">
411
+ <li class="never" data-hits="" data-linenumber="54">
370
412
 
371
413
 
372
414
  <code class="ruby"> end</code>
373
415
  </li>
374
416
 
375
- <li class="never" data-hits="" data-linenumber="48">
417
+ <li class="never" data-hits="" data-linenumber="55">
376
418
 
377
419
 
378
420
  <code class="ruby"></code>
379
421
  </li>
380
422
 
381
- <li class="covered" data-hits="1" data-linenumber="49">
423
+ <li class="covered" data-hits="1" data-linenumber="56">
382
424
  <span class="hits">1</span>
383
425
 
384
426
  <code class="ruby"> private</code>
385
427
  </li>
386
428
 
387
- <li class="never" data-hits="" data-linenumber="50">
429
+ <li class="never" data-hits="" data-linenumber="57">
388
430
 
389
431
 
390
432
  <code class="ruby"></code>
391
433
  </li>
392
434
 
393
- <li class="covered" data-hits="1" data-linenumber="51">
435
+ <li class="covered" data-hits="1" data-linenumber="58">
394
436
  <span class="hits">1</span>
395
437
 
396
438
  <code class="ruby"> def coerce(param, type, options = {})</code>
397
439
  </li>
398
440
 
399
- <li class="covered" data-hits="67" data-linenumber="52">
400
- <span class="hits">67</span>
441
+ <li class="covered" data-hits="68" data-linenumber="59">
442
+ <span class="hits">68</span>
401
443
 
402
444
  <code class="ruby"> return nil if param.nil?</code>
403
445
  </li>
404
446
 
405
- <li class="covered" data-hits="64" data-linenumber="53">
447
+ <li class="covered" data-hits="64" data-linenumber="60">
406
448
  <span class="hits">64</span>
407
449
 
408
450
  <code class="ruby"> return param if (param.is_a?(type) rescue false)</code>
409
451
  </li>
410
452
 
411
- <li class="covered" data-hits="39" data-linenumber="54">
453
+ <li class="covered" data-hits="39" data-linenumber="61">
412
454
  <span class="hits">39</span>
413
455
 
414
456
  <code class="ruby"> return Integer(param) if type == Integer</code>
415
457
  </li>
416
458
 
417
- <li class="covered" data-hits="30" data-linenumber="55">
459
+ <li class="covered" data-hits="30" data-linenumber="62">
418
460
  <span class="hits">30</span>
419
461
 
420
462
  <code class="ruby"> return Float(param) if type == Float</code>
421
463
  </li>
422
464
 
423
- <li class="covered" data-hits="29" data-linenumber="56">
465
+ <li class="covered" data-hits="29" data-linenumber="63">
424
466
  <span class="hits">29</span>
425
467
 
426
468
  <code class="ruby"> return String(param) if type == String</code>
427
469
  </li>
428
470
 
429
- <li class="covered" data-hits="28" data-linenumber="57">
471
+ <li class="covered" data-hits="28" data-linenumber="64">
430
472
  <span class="hits">28</span>
431
473
 
432
474
  <code class="ruby"> return Time.parse(param) if type == Time</code>
433
475
  </li>
434
476
 
435
- <li class="covered" data-hits="27" data-linenumber="58">
477
+ <li class="covered" data-hits="27" data-linenumber="65">
436
478
  <span class="hits">27</span>
437
479
 
438
480
  <code class="ruby"> return Date.parse(param) if type == Date</code>
439
481
  </li>
440
482
 
441
- <li class="covered" data-hits="26" data-linenumber="59">
483
+ <li class="covered" data-hits="26" data-linenumber="66">
442
484
  <span class="hits">26</span>
443
485
 
444
486
  <code class="ruby"> return DateTime.parse(param) if type == DateTime</code>
445
487
  </li>
446
488
 
447
- <li class="covered" data-hits="25" data-linenumber="60">
489
+ <li class="covered" data-hits="25" data-linenumber="67">
448
490
  <span class="hits">25</span>
449
491
 
450
492
  <code class="ruby"> return Array(param.split(options[:delimiter] || &quot;,&quot;)) if type == Array</code>
451
493
  </li>
452
494
 
453
- <li class="covered" data-hits="25" data-linenumber="61">
495
+ <li class="covered" data-hits="25" data-linenumber="68">
454
496
  <span class="hits">25</span>
455
497
 
456
498
  <code class="ruby"> return Hash[param.split(options[:delimiter] || &quot;,&quot;).map{|c| c.split(options[:separator] || &quot;:&quot;)}] if type == Hash</code>
457
499
  </li>
458
500
 
459
- <li class="covered" data-hits="21" data-linenumber="62">
501
+ <li class="covered" data-hits="21" data-linenumber="69">
460
502
  <span class="hits">21</span>
461
503
 
462
504
  <code class="ruby"> return (/(false|f|no|n|0)$/i === param.to_s ? false : (/(true|t|yes|y|1)$/i === param.to_s ? true : nil)) if type == TrueClass || type == FalseClass || type == Boolean</code>
463
505
  </li>
464
506
 
465
- <li class="never" data-hits="" data-linenumber="63">
507
+ <li class="never" data-hits="" data-linenumber="70">
466
508
 
467
509
 
468
510
  <code class="ruby"> return nil</code>
469
511
  </li>
470
512
 
471
- <li class="never" data-hits="" data-linenumber="64">
513
+ <li class="never" data-hits="" data-linenumber="71">
472
514
 
473
515
 
474
516
  <code class="ruby"> end</code>
475
517
  </li>
476
518
 
477
- <li class="never" data-hits="" data-linenumber="65">
519
+ <li class="never" data-hits="" data-linenumber="72">
478
520
 
479
521
 
480
522
  <code class="ruby"></code>
481
523
  </li>
482
524
 
483
- <li class="covered" data-hits="1" data-linenumber="66">
525
+ <li class="covered" data-hits="1" data-linenumber="73">
484
526
  <span class="hits">1</span>
485
527
 
486
528
  <code class="ruby"> def validate!(param, options)</code>
487
529
  </li>
488
530
 
489
- <li class="covered" data-hits="67" data-linenumber="67">
490
- <span class="hits">67</span>
531
+ <li class="covered" data-hits="68" data-linenumber="74">
532
+ <span class="hits">68</span>
491
533
 
492
534
  <code class="ruby"> options.each do |key, value|</code>
493
535
  </li>
494
536
 
495
- <li class="covered" data-hits="38" data-linenumber="68">
496
- <span class="hits">38</span>
537
+ <li class="covered" data-hits="39" data-linenumber="75">
538
+ <span class="hits">39</span>
497
539
 
498
540
  <code class="ruby"> case key</code>
499
541
  </li>
500
542
 
501
- <li class="never" data-hits="" data-linenumber="69">
543
+ <li class="never" data-hits="" data-linenumber="76">
502
544
 
503
545
 
504
546
  <code class="ruby"> when :required</code>
505
547
  </li>
506
548
 
507
- <li class="covered" data-hits="3" data-linenumber="70">
549
+ <li class="covered" data-hits="3" data-linenumber="77">
508
550
  <span class="hits">3</span>
509
551
 
510
- <code class="ruby"> raise InvalidParameterError if value &amp;&amp; param.nil?</code>
552
+ <code class="ruby"> raise InvalidParameterError, &quot;Parameter is required&quot; if value &amp;&amp; param.nil?</code>
511
553
  </li>
512
554
 
513
- <li class="never" data-hits="" data-linenumber="71">
555
+ <li class="never" data-hits="" data-linenumber="78">
514
556
 
515
557
 
516
558
  <code class="ruby"> when :blank</code>
517
559
  </li>
518
560
 
519
- <li class="never" data-hits="" data-linenumber="72">
561
+ <li class="never" data-hits="" data-linenumber="79">
520
562
 
521
563
 
522
- <code class="ruby"> raise InvalidParameterError if !value &amp;&amp; case param</code>
564
+ <code class="ruby"> raise InvalidParameterError, &quot;Parameter cannot be blank&quot; if !value &amp;&amp; case param</code>
523
565
  </li>
524
566
 
525
- <li class="never" data-hits="" data-linenumber="73">
567
+ <li class="never" data-hits="" data-linenumber="80">
526
568
 
527
569
 
528
570
  <code class="ruby"> when String</code>
529
571
  </li>
530
572
 
531
- <li class="covered" data-hits="1" data-linenumber="74">
573
+ <li class="covered" data-hits="1" data-linenumber="81">
532
574
  <span class="hits">1</span>
533
575
 
534
576
  <code class="ruby"> !(/\S/ === param)</code>
535
577
  </li>
536
578
 
537
- <li class="never" data-hits="" data-linenumber="75">
579
+ <li class="never" data-hits="" data-linenumber="82">
538
580
 
539
581
 
540
582
  <code class="ruby"> when Array, Hash</code>
541
583
  </li>
542
584
 
543
- <li class="covered" data-hits="2" data-linenumber="76">
585
+ <li class="covered" data-hits="2" data-linenumber="83">
544
586
  <span class="hits">2</span>
545
587
 
546
588
  <code class="ruby"> param.empty?</code>
547
589
  </li>
548
590
 
549
- <li class="never" data-hits="" data-linenumber="77">
591
+ <li class="never" data-hits="" data-linenumber="84">
550
592
 
551
593
 
552
594
  <code class="ruby"> else</code>
553
595
  </li>
554
596
 
555
- <li class="covered" data-hits="1" data-linenumber="78">
597
+ <li class="covered" data-hits="1" data-linenumber="85">
556
598
  <span class="hits">1</span>
557
599
 
558
600
  <code class="ruby"> param.nil?</code>
559
601
  </li>
560
602
 
561
- <li class="covered" data-hits="5" data-linenumber="79">
603
+ <li class="covered" data-hits="5" data-linenumber="86">
562
604
  <span class="hits">5</span>
563
605
 
564
606
  <code class="ruby"> end</code>
565
607
  </li>
566
608
 
567
- <li class="never" data-hits="" data-linenumber="80">
609
+ <li class="never" data-hits="" data-linenumber="87">
568
610
 
569
611
 
570
612
  <code class="ruby"> when :is</code>
571
613
  </li>
572
614
 
573
- <li class="covered" data-hits="2" data-linenumber="81">
615
+ <li class="covered" data-hits="2" data-linenumber="88">
574
616
  <span class="hits">2</span>
575
617
 
576
- <code class="ruby"> raise InvalidParameterError unless value === param</code>
618
+ <code class="ruby"> raise InvalidParameterError, &quot;Parameter must be #{value}&quot; unless value === param</code>
577
619
  </li>
578
620
 
579
- <li class="never" data-hits="" data-linenumber="82">
621
+ <li class="never" data-hits="" data-linenumber="89">
580
622
 
581
623
 
582
624
  <code class="ruby"> when :in, :within, :range</code>
583
625
  </li>
584
626
 
585
- <li class="never" data-hits="" data-linenumber="83">
627
+ <li class="never" data-hits="" data-linenumber="90">
586
628
 
587
629
 
588
- <code class="ruby"> raise InvalidParameterError unless param.nil? || case value</code>
630
+ <code class="ruby"> raise InvalidParameterError, &quot;Parameter must be within #{value}&quot; unless param.nil? || case value</code>
589
631
  </li>
590
632
 
591
- <li class="never" data-hits="" data-linenumber="84">
633
+ <li class="never" data-hits="" data-linenumber="91">
592
634
 
593
635
 
594
636
  <code class="ruby"> when Range</code>
595
637
  </li>
596
638
 
597
- <li class="covered" data-hits="4" data-linenumber="85">
639
+ <li class="covered" data-hits="4" data-linenumber="92">
598
640
  <span class="hits">4</span>
599
641
 
600
642
  <code class="ruby"> value.include?(param)</code>
601
643
  </li>
602
644
 
603
- <li class="never" data-hits="" data-linenumber="86">
645
+ <li class="never" data-hits="" data-linenumber="93">
604
646
 
605
647
 
606
648
  <code class="ruby"> else</code>
607
649
  </li>
608
650
 
609
- <li class="covered" data-hits="2" data-linenumber="87">
651
+ <li class="covered" data-hits="2" data-linenumber="94">
610
652
  <span class="hits">2</span>
611
653
 
612
654
  <code class="ruby"> Array(value).include?(param)</code>
613
655
  </li>
614
656
 
615
- <li class="covered" data-hits="6" data-linenumber="88">
657
+ <li class="covered" data-hits="6" data-linenumber="95">
616
658
  <span class="hits">6</span>
617
659
 
618
660
  <code class="ruby"> end</code>
619
661
  </li>
620
662
 
621
- <li class="never" data-hits="" data-linenumber="89">
663
+ <li class="never" data-hits="" data-linenumber="96">
622
664
 
623
665
 
624
666
  <code class="ruby"> when :min</code>
625
667
  </li>
626
668
 
627
- <li class="covered" data-hits="2" data-linenumber="90">
669
+ <li class="covered" data-hits="2" data-linenumber="97">
628
670
  <span class="hits">2</span>
629
671
 
630
- <code class="ruby"> raise InvalidParameterError unless param.nil? || value &lt;= param</code>
672
+ <code class="ruby"> raise InvalidParameterError, &quot;Parameter cannot be less than #{value}&quot; unless param.nil? || value &lt;= param</code>
631
673
  </li>
632
674
 
633
- <li class="never" data-hits="" data-linenumber="91">
675
+ <li class="never" data-hits="" data-linenumber="98">
634
676
 
635
677
 
636
678
  <code class="ruby"> when :max</code>
637
679
  </li>
638
680
 
639
- <li class="covered" data-hits="2" data-linenumber="92">
681
+ <li class="covered" data-hits="2" data-linenumber="99">
640
682
  <span class="hits">2</span>
641
683
 
642
- <code class="ruby"> raise InvalidParameterError unless param.nil? || value &gt;= param</code>
684
+ <code class="ruby"> raise InvalidParameterError, &quot;Parameter cannot be greater than #{value}&quot; unless param.nil? || value &gt;= param</code>
643
685
  </li>
644
686
 
645
- <li class="never" data-hits="" data-linenumber="93">
687
+ <li class="never" data-hits="" data-linenumber="100">
646
688
 
647
689
 
648
690
  <code class="ruby"> when :min_length</code>
649
691
  </li>
650
692
 
651
- <li class="covered" data-hits="1" data-linenumber="94">
693
+ <li class="covered" data-hits="1" data-linenumber="101">
652
694
  <span class="hits">1</span>
653
695
 
654
- <code class="ruby"> raise InvalidParameterError unless param.nil? || value &lt;= param.length</code>
696
+ <code class="ruby"> raise InvalidParameterError, &quot;Parameter cannot have length less than #{value}&quot; unless param.nil? || value &lt;= param.length</code>
655
697
  </li>
656
698
 
657
- <li class="never" data-hits="" data-linenumber="95">
699
+ <li class="never" data-hits="" data-linenumber="102">
658
700
 
659
701
 
660
702
  <code class="ruby"> when :max_length</code>
661
703
  </li>
662
704
 
663
- <li class="covered" data-hits="3" data-linenumber="96">
705
+ <li class="covered" data-hits="3" data-linenumber="103">
664
706
  <span class="hits">3</span>
665
707
 
666
- <code class="ruby"> raise InvalidParameterError unless param.nil? || value &gt;= param.length</code>
708
+ <code class="ruby"> raise InvalidParameterError, &quot;Parameter cannot have length greater than #{value}&quot; unless param.nil? || value &gt;= param.length</code>
667
709
  </li>
668
710
 
669
- <li class="never" data-hits="" data-linenumber="97">
711
+ <li class="never" data-hits="" data-linenumber="104">
670
712
 
671
713
 
672
714
  <code class="ruby"> end</code>
673
715
  </li>
674
716
 
675
- <li class="never" data-hits="" data-linenumber="98">
717
+ <li class="never" data-hits="" data-linenumber="105">
676
718
 
677
719
 
678
720
  <code class="ruby"> end</code>
679
721
  </li>
680
722
 
681
- <li class="never" data-hits="" data-linenumber="99">
723
+ <li class="never" data-hits="" data-linenumber="106">
682
724
 
683
725
 
684
726
  <code class="ruby"> end</code>
685
727
  </li>
686
728
 
687
- <li class="never" data-hits="" data-linenumber="100">
729
+ <li class="never" data-hits="" data-linenumber="107">
688
730
 
689
731
 
690
732
  <code class="ruby"></code>
691
733
  </li>
692
734
 
693
- <li class="never" data-hits="" data-linenumber="101">
735
+ <li class="never" data-hits="" data-linenumber="108">
694
736
 
695
737
 
696
738
  <code class="ruby"> # ActiveSupport #present? and #blank? without patching Object</code>
697
739
  </li>
698
740
 
699
- <li class="covered" data-hits="1" data-linenumber="102">
741
+ <li class="covered" data-hits="1" data-linenumber="109">
700
742
  <span class="hits">1</span>
701
743
 
702
744
  <code class="ruby"> def present?(object)</code>
703
745
  </li>
704
746
 
705
- <li class="covered" data-hits="21" data-linenumber="103">
706
- <span class="hits">21</span>
747
+ <li class="covered" data-hits="22" data-linenumber="110">
748
+ <span class="hits">22</span>
707
749
 
708
750
  <code class="ruby"> !blank?(object)</code>
709
751
  </li>
710
752
 
711
- <li class="never" data-hits="" data-linenumber="104">
753
+ <li class="never" data-hits="" data-linenumber="111">
712
754
 
713
755
 
714
756
  <code class="ruby"> end</code>
715
757
  </li>
716
758
 
717
- <li class="never" data-hits="" data-linenumber="105">
759
+ <li class="never" data-hits="" data-linenumber="112">
718
760
 
719
761
 
720
762
  <code class="ruby"></code>
721
763
  </li>
722
764
 
723
- <li class="covered" data-hits="1" data-linenumber="106">
765
+ <li class="covered" data-hits="1" data-linenumber="113">
724
766
  <span class="hits">1</span>
725
767
 
726
768
  <code class="ruby"> def blank?(object)</code>
727
769
  </li>
728
770
 
729
- <li class="covered" data-hits="21" data-linenumber="107">
730
- <span class="hits">21</span>
771
+ <li class="covered" data-hits="22" data-linenumber="114">
772
+ <span class="hits">22</span>
731
773
 
732
774
  <code class="ruby"> object.respond_to?(:empty?) ? object.empty? : !object</code>
733
775
  </li>
734
776
 
735
- <li class="never" data-hits="" data-linenumber="108">
777
+ <li class="never" data-hits="" data-linenumber="115">
736
778
 
737
779
 
738
780
  <code class="ruby"> end</code>
739
781
  </li>
740
782
 
741
- <li class="never" data-hits="" data-linenumber="109">
783
+ <li class="never" data-hits="" data-linenumber="116">
742
784
 
743
785
 
744
786
  <code class="ruby"> end</code>
745
787
  </li>
746
788
 
747
- <li class="never" data-hits="" data-linenumber="110">
789
+ <li class="never" data-hits="" data-linenumber="117">
748
790
 
749
791
 
750
792
  <code class="ruby"></code>
751
793
  </li>
752
794
 
753
- <li class="covered" data-hits="1" data-linenumber="111">
795
+ <li class="covered" data-hits="1" data-linenumber="118">
754
796
  <span class="hits">1</span>
755
797
 
756
798
  <code class="ruby"> helpers Param</code>
757
799
  </li>
758
800
 
759
- <li class="never" data-hits="" data-linenumber="112">
801
+ <li class="never" data-hits="" data-linenumber="119">
760
802
 
761
803
 
762
804
  <code class="ruby">end</code>
data/lib/sinatra/param.rb CHANGED
@@ -18,7 +18,7 @@ module Sinatra
18
18
 
19
19
  begin
20
20
  params[name] = coerce(params[name], type, options)
21
- params[name] = options[:default] if params[name].nil? and options[:default]
21
+ params[name] = (options[:default].call if options[:default].is_a?(Proc)) || options[:default] if params[name].nil? and options[:default]
22
22
  params[name] = options[:transform].to_proc.call(params[name]) if options[:transform]
23
23
  validate!(params[name], options)
24
24
  rescue InvalidParameterError => exception
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module Param
3
- VERSION = '1.1.0'
3
+ VERSION = '1.1.1'
4
4
  end
5
5
  end
Binary file
data/spec/dummy/app.rb CHANGED
@@ -76,6 +76,11 @@ class App < Sinatra::Base
76
76
  params.to_json
77
77
  end
78
78
 
79
+ get '/default/proc' do
80
+ param :year, Integer, default: proc { 2014 }
81
+ params.to_json
82
+ end
83
+
79
84
  get '/default/boolean/true' do
80
85
  param :arg, Boolean, default: true
81
86
  params.to_json
@@ -8,6 +8,13 @@ describe 'Parameter Transformations' do
8
8
  JSON.parse(response.body)['sort'].should == 'title'
9
9
  end
10
10
  end
11
+
12
+ it 'sets a default value from a proc' do
13
+ get('/default/proc') do |response|
14
+ response.status.should == 200
15
+ JSON.parse(response.body)['year'].should == 2014
16
+ end
17
+ end
11
18
  end
12
19
 
13
20
  describe 'transform' do
@@ -5,7 +5,7 @@ describe 'Parameter Validations' do
5
5
  it 'returns 400 on requests without required fields' do
6
6
  get('/validation/required') do |response|
7
7
  response.status.should eq(400)
8
- JSON.parse(response.body)['message'].should eq('Invalid parameter, arg')
8
+ JSON.parse(response.body)['message'].should eq('Invalid Parameter: arg')
9
9
  end
10
10
  end
11
11
 
@@ -20,28 +20,28 @@ describe 'Parameter Validations' do
20
20
  it 'returns 400 on requests when string is blank' do
21
21
  get('/validation/blank/string', arg: '') do |response|
22
22
  response.status.should eq(400)
23
- JSON.parse(response.body)['message'].should eq('Invalid parameter, arg')
23
+ JSON.parse(response.body)['message'].should eq('Invalid Parameter: arg')
24
24
  end
25
25
  end
26
26
 
27
27
  it 'returns 400 on requests when array is blank' do
28
28
  get('/validation/blank/array', arg: '') do |response|
29
29
  response.status.should eq(400)
30
- JSON.parse(response.body)['message'].should eq('Invalid parameter, arg')
30
+ JSON.parse(response.body)['message'].should eq('Invalid Parameter: arg')
31
31
  end
32
32
  end
33
33
 
34
34
  it 'returns 400 on requests when hash is blank' do
35
35
  get('/validation/blank/hash', arg: '') do |response|
36
36
  response.status.should eq(400)
37
- JSON.parse(response.body)['message'].should eq('Invalid parameter, arg')
37
+ JSON.parse(response.body)['message'].should eq('Invalid Parameter: arg')
38
38
  end
39
39
  end
40
40
 
41
41
  it 'returns 400 on requests when hash is blank' do
42
42
  get('/validation/blank/other', arg: '') do |response|
43
43
  response.status.should eq(400)
44
- JSON.parse(response.body)['message'].should eq('Invalid parameter, arg')
44
+ JSON.parse(response.body)['message'].should eq('Invalid Parameter: arg')
45
45
  end
46
46
  end
47
47
 
@@ -56,7 +56,7 @@ describe 'Parameter Validations' do
56
56
  it 'returns 400 on requests when value is other than defined' do
57
57
  get('/validation/is', arg: 'bar') do |response|
58
58
  response.status.should eq(400)
59
- JSON.parse(response.body)['message'].should eq('Invalid parameter, arg')
59
+ JSON.parse(response.body)['message'].should eq('Invalid Parameter: arg')
60
60
  end
61
61
  end
62
62
 
@@ -71,7 +71,7 @@ describe 'Parameter Validations' do
71
71
  it 'returns 400 on requests with a value not in the set' do
72
72
  get('/validation/in', arg: 'MISC') do |response|
73
73
  response.status.should eq(400)
74
- JSON.parse(response.body)['message'].should eq('Invalid parameter, arg')
74
+ JSON.parse(response.body)['message'].should eq('Invalid Parameter: arg')
75
75
  end
76
76
  end
77
77
 
@@ -86,7 +86,7 @@ describe 'Parameter Validations' do
86
86
  it 'returns 400 on requests with a value outside the range' do
87
87
  get('/validation/within', arg: 20) do |response|
88
88
  response.status.should eq(400)
89
- JSON.parse(response.body)['message'].should eq('Invalid parameter, arg')
89
+ JSON.parse(response.body)['message'].should eq('Invalid Parameter: arg')
90
90
  end
91
91
  end
92
92
 
@@ -101,7 +101,7 @@ describe 'Parameter Validations' do
101
101
  it 'returns 400 on requests with a value outside the range' do
102
102
  get('/validation/range', arg: 20) do |response|
103
103
  response.status.should eq(400)
104
- JSON.parse(response.body)['message'].should eq('Invalid parameter, arg')
104
+ JSON.parse(response.body)['message'].should eq('Invalid Parameter: arg')
105
105
  end
106
106
  end
107
107
 
@@ -116,7 +116,7 @@ describe 'Parameter Validations' do
116
116
  it 'returns 400 on requests with a value smaller than min' do
117
117
  get('/validation/min', arg: 5) do |response|
118
118
  response.status.should eq(400)
119
- JSON.parse(response.body)['message'].should eq('Invalid parameter, arg')
119
+ JSON.parse(response.body)['message'].should eq('Invalid Parameter: arg')
120
120
  end
121
121
  end
122
122
 
@@ -131,7 +131,7 @@ describe 'Parameter Validations' do
131
131
  it 'returns 400 on requests with a value larger than max' do
132
132
  get('/validation/max', arg: 100) do |response|
133
133
  response.status.should eq(400)
134
- JSON.parse(response.body)['message'].should eq('Invalid parameter, arg')
134
+ JSON.parse(response.body)['message'].should eq('Invalid Parameter: arg')
135
135
  end
136
136
  end
137
137
 
@@ -146,7 +146,7 @@ describe 'Parameter Validations' do
146
146
  it 'returns 400 on requests with a string shorter than min_length' do
147
147
  get('/validation/min_length', arg: 'hi') do |response|
148
148
  response.status.should eq(400)
149
- JSON.parse(response.body)['message'].should eq('Invalid parameter, arg')
149
+ JSON.parse(response.body)['message'].should eq('Invalid Parameter: arg')
150
150
  end
151
151
  end
152
152
 
@@ -161,7 +161,7 @@ describe 'Parameter Validations' do
161
161
  it 'returns 400 on requests with a string longer than max_length' do
162
162
  get('/validation/max_length', arg: 'reallylongstringlongerthanmax') do |response|
163
163
  response.status.should eq(400)
164
- JSON.parse(response.body)['message'].should eq('Invalid parameter, arg')
164
+ JSON.parse(response.body)['message'].should eq('Invalid Parameter: arg')
165
165
  end
166
166
  end
167
167
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-param
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mattt Thompson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-06 00:00:00.000000000 Z
11
+ date: 2014-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -140,6 +140,7 @@ files:
140
140
  - ./LICENSE
141
141
  - ./Rakefile
142
142
  - ./README.md
143
+ - ./sinatra-param-1.1.0.gem
143
144
  - ./sinatra-param.gemspec
144
145
  - spec/dummy/app.rb
145
146
  - spec/parameter_sets_spec.rb