decanter 0.8.2 → 0.9.0

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: 99d5bfae4e2f4ed374e9851fd07d53d524963349
4
- data.tar.gz: ad634273515df9853b3566948a1dcf61884d5b80
3
+ metadata.gz: 59c4f720f9e7e8d34a163d6b06314adb05aad75e
4
+ data.tar.gz: 57fb16ed8e5d10a7b265dfe3a1b201867a938017
5
5
  SHA512:
6
- metadata.gz: 09420cff51f63c0971a7dc1651b27078d209588f65373608c3be654dbea9bfad1d4ef0727b7170c8aa9bf65d271220c396e7d0f0ed01c028ccb64d2e3e1ec9b0
7
- data.tar.gz: 03dd595cc9e0f01ca5963d8fdb88763300990a018ec639c7d093bd6098d1fb18c6e7ffde8a07c663adc911d43ab7a54cddb1f5b66c371b0aaaf0fccaf97e00b1
6
+ metadata.gz: 950d5a5e1a6110a19b7acb242fb8baaacd526c969caff7966fa6122c3f3ca53c96e3a6fe7dc8ad74c202ff6986384a8a2f52649282a2c1fce26bd60e72bf634b
7
+ data.tar.gz: 10666cd624edba38e980e4e5d90684e62bf8b452905e4a8b882fe0bbfc32e1e5aae237a0e88a3988358a9987195fdd8a0fe419fb07e497e7011fa74d11a15fc9
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- decanter (0.8.1)
4
+ decanter (0.9.0)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -97,4 +97,4 @@ DEPENDENCIES
97
97
  rspec-rails
98
98
 
99
99
  BUNDLED WITH
100
- 1.10.6
100
+ 1.14.4
data/README.md CHANGED
@@ -422,27 +422,53 @@ No Need for Strong Params
422
422
 
423
423
  Since you are already defining your expected inputs in Decanter, you really don't need strong params anymore.
424
424
 
425
- In order to tell Decanter to ignore the params not defined in your Decanter, just add the ```strict``` flag to your Decanters:
425
+ Note: starting with version 0.7.2, the default strict mode is ```:with_exception```. You can modify your default strict mode in your configuration file (see the "Configuration" section below).
426
+
427
+ #### Mode: with_exception (default mode)
428
+
429
+ To raise exceptions when parameters arrive in your Decanter that you didn't expect:
426
430
 
427
431
  ```ruby
428
432
  class TripDecanter < Decanter::Base
429
- strict true
433
+ strict :with_exception
430
434
 
431
435
  input :name
432
436
  end
433
437
  ```
434
438
 
435
- Or to raise exceptions when parameters arrive in your Decanter that you didn't expect:
439
+ #### Mode: strict
440
+
441
+ In order to tell Decanter to ignore the params not defined in your Decanter, just add the ```strict``` flag to your Decanters:
436
442
 
437
443
  ```ruby
438
444
  class TripDecanter < Decanter::Base
439
- strict :with_exception
445
+ strict true
440
446
 
441
447
  input :name
442
448
  end
443
449
  ```
444
450
 
445
- In addition, if you provide the option `:required` for an input in your decanter, an exception will be thrown if the parameters is nil or an empty string.
451
+ #### Requiring Params
452
+
453
+ If you provide the option `:required` for an input in your decanter, an exception will be thrown if the parameters is nil or an empty string.
454
+
455
+ ```ruby
456
+ class TripDecanter < Decanter::Base
457
+ input :name, :string, required: true
458
+ end
459
+ ```
460
+
461
+ #### Ignoring Params
462
+
463
+ If you anticipate your decanter will receive certain params that you simply want to ignore and therefore do not want Decanter to raise an exception, you can do so by calling the `ignore` method:
464
+
465
+ ```ruby
466
+ class TripDecanter < Decanter::Base
467
+ ignore :created_at, :updated_at
468
+
469
+ input :name, :string
470
+ end
471
+ ```
446
472
 
447
473
  Configuration
448
474
  ---
@@ -5,14 +5,20 @@ module Decanter
5
5
  class << self
6
6
 
7
7
  def decanter_for(klass_or_sym)
8
- case klass_or_sym
9
- when Class
10
- klass_or_sym.name
11
- when Symbol
12
- klass_or_sym.to_s.singularize.camelize
13
- else
14
- raise ArgumentError.new("cannot lookup decanter for #{klass_or_sym} with class #{klass_or_sym.class}")
15
- end.concat('Decanter').constantize
8
+ decanter_name =
9
+ case klass_or_sym
10
+ when Class
11
+ klass_or_sym.name
12
+ when Symbol
13
+ klass_or_sym.to_s.singularize.camelize
14
+ else
15
+ raise ArgumentError.new("cannot lookup decanter for #{klass_or_sym} with class #{klass_or_sym.class}")
16
+ end.concat('Decanter')
17
+ begin
18
+ decanter_name.constantize
19
+ rescue
20
+ raise NameError.new("uninitialized constant #{decanter_name}")
21
+ end
16
22
  end
17
23
 
18
24
  def decanter_from(klass_or_string)
@@ -21,7 +27,11 @@ module Decanter
21
27
  when Class
22
28
  klass_or_string
23
29
  when String
24
- klass_or_string.constantize
30
+ begin
31
+ klass_or_string.constantize
32
+ rescue
33
+ raise NameError.new("uninitialized constant #{klass_or_string}")
34
+ end
25
35
  else
26
36
  raise ArgumentError.new("cannot find decanter from #{klass_or_string} with class #{klass_or_string.class}")
27
37
  end
@@ -51,4 +61,4 @@ require 'decanter/core'
51
61
  require 'decanter/base'
52
62
  require 'decanter/extensions'
53
63
  require 'decanter/parser'
54
- require 'decanter/railtie' if defined?(::Rails)
64
+ require 'decanter/railtie' if defined?(::Rails)
@@ -44,6 +44,10 @@ module Decanter
44
44
  }
45
45
  end
46
46
 
47
+ def ignore(*args)
48
+ keys_to_ignore.push(*args)
49
+ end
50
+
47
51
  def strict(mode)
48
52
  raise( ArgumentError.new("#{self.name}: Unknown strict value #{mode}")) unless [:with_exception, true, false].include? mode
49
53
  @strict_mode = mode
@@ -61,16 +65,17 @@ module Decanter
61
65
  def unhandled_keys(args)
62
66
  unhandled_keys = args.keys.map(&:to_sym) -
63
67
  handlers.keys.flatten.uniq -
68
+ keys_to_ignore -
64
69
  handlers.values
65
70
  .select { |handler| handler[:type] != :input }
66
- .map { |handler| "#{handler[:name]}_attributes".to_sym }
71
+ .map { |handler| "#{handler[:name]}_attributes".to_sym }
67
72
 
68
73
  if unhandled_keys.any?
69
74
  case strict_mode
70
75
  when true
71
76
  p "#{self.name} ignoring unhandled keys: #{unhandled_keys.join(', ')}."
72
77
  {}
73
- when :with_exception
78
+ when :with_exception
74
79
  raise ArgumentError.new("#{self.name} received unhandled keys: #{unhandled_keys.join(', ')}.")
75
80
  else
76
81
  args.select { |key| unhandled_keys.include? key }
@@ -173,6 +178,10 @@ module Decanter
173
178
  @handlers ||= {}
174
179
  end
175
180
 
181
+ def keys_to_ignore
182
+ @keys_to_ignore ||= []
183
+ end
184
+
176
185
  def strict_mode
177
186
  @strict_mode.nil? ? Decanter.configuration.strict : @strict_mode
178
187
  end
@@ -1,3 +1,3 @@
1
1
  module Decanter
2
- VERSION = '0.8.2'
2
+ VERSION = '0.9.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decanter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Francis
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-08-09 00:00:00.000000000 Z
12
+ date: 2017-06-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -164,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
164
  version: '0'
165
165
  requirements: []
166
166
  rubyforge_project:
167
- rubygems_version: 2.4.5.1
167
+ rubygems_version: 2.5.1
168
168
  signing_key:
169
169
  specification_version: 4
170
170
  summary: Form Parser for Rails