decanter 3.0.1 → 3.2.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
  SHA256:
3
- metadata.gz: d88913e453daf9b338ba361c9d0bcf8bd460c6cbf8f9ba9c8690f8bb72072350
4
- data.tar.gz: 4831a0a79c97caaf4ebe48123e2ae77baa5bf15a95b911e639ead8c43c428186
3
+ metadata.gz: 8bcc235910fd86ed830c20f5c07f53b3154911cb26aec56d45fece483207d4bd
4
+ data.tar.gz: a9db5721c34fdd0c9857ce8647ad84b2a4bbfdb6b1b76f67fe4e5a8b1650d9d2
5
5
  SHA512:
6
- metadata.gz: 5b16bca4f276b1aceefb952104e38675ba0bea4792ef4d9b30e61d1b0501ed1dff9d832f97cb17cc2db62711c644e12183900ded11d6382dea0830a91f38c28a
7
- data.tar.gz: 19b9c671278b61dc7a903f2df4861c606914585165bfa3bfd81f82d950db6fa02328ee3d25ead2dd9048690b92238856d7fd8b833def2bc40b045d405c3a4832
6
+ metadata.gz: 170e5256eda3f2b36f259f6f475420218c1a04aa360f553a0fb0f74479c28469a521e32fbfb852521d8c84e5dc22d754f7455e66e18702493b303b285cb1f7a6
7
+ data.tar.gz: 35a01b51460f2f831a40e52fc5f11a8e800b5e50eeb08d0623f8241d72f00b5f20e1543235904083e793b0be76a02c1378c4f31edbb4b3934fa155b01f409b92
@@ -0,0 +1,2 @@
1
+ # These owners will be the default owners for everything in the repo.
2
+ * @chawes13
@@ -4,5 +4,5 @@
4
4
  ## Author Checklist
5
5
  - [ ] Add unit test(s)
6
6
  - [ ] Update documentation (if necessary)
7
- - [ ] Update version in `version.rb` following [versioning guidelines](https://github.com/LaunchPadLab/opex/blob/master/gists/gem-guidelines.md#pull-requests-and-deployments)
7
+ - [ ] Update version in `version.rb` following [versioning guidelines](https://github.com/LaunchPadLab/opex-public/blob/master/gists/gem-guidelines.md#pull-requests-and-deployments)
8
8
 
@@ -1,6 +1,6 @@
1
1
  # Contributing
2
2
 
3
- Thanks for your interest in contributing! This project follows our [Gem guidelines](https://github.com/LaunchPadLab/opex/blob/master/gists/gem-guidelines.md).
3
+ Thanks for your interest in contributing! This project follows our [Gem guidelines](https://github.com/LaunchPadLab/opex-public/blob/master/gists/gem-guidelines.md).
4
4
 
5
5
  ## Code of Conduct
6
6
 
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in goaltender.gemspec
3
+ # Specify your gem's dependencies in decanter.gemspec
4
4
  gemspec
@@ -1,90 +1,90 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- decanter (3.0.1)
4
+ decanter (3.2.1)
5
5
  actionpack (>= 4.2.10)
6
- activesupport
6
+ activesupport (~> 5.2)
7
7
  rails-html-sanitizer (>= 1.0.4)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- actionpack (5.1.4)
13
- actionview (= 5.1.4)
14
- activesupport (= 5.1.4)
15
- rack (~> 2.0)
12
+ actionpack (5.2.4.4)
13
+ actionview (= 5.2.4.4)
14
+ activesupport (= 5.2.4.4)
15
+ rack (~> 2.0, >= 2.0.8)
16
16
  rack-test (>= 0.6.3)
17
17
  rails-dom-testing (~> 2.0)
18
18
  rails-html-sanitizer (~> 1.0, >= 1.0.2)
19
- actionview (5.1.4)
20
- activesupport (= 5.1.4)
19
+ actionview (5.2.4.4)
20
+ activesupport (= 5.2.4.4)
21
21
  builder (~> 3.1)
22
22
  erubi (~> 1.4)
23
23
  rails-dom-testing (~> 2.0)
24
24
  rails-html-sanitizer (~> 1.0, >= 1.0.3)
25
- activesupport (5.1.4)
25
+ activesupport (5.2.4.4)
26
26
  concurrent-ruby (~> 1.0, >= 1.0.2)
27
- i18n (~> 0.7)
27
+ i18n (>= 0.7, < 2)
28
28
  minitest (~> 5.1)
29
29
  tzinfo (~> 1.1)
30
- builder (3.2.3)
31
- concurrent-ruby (1.0.5)
32
- crass (1.0.4)
33
- diff-lcs (1.3)
30
+ builder (3.2.4)
31
+ concurrent-ruby (1.1.7)
32
+ crass (1.0.6)
33
+ diff-lcs (1.4.4)
34
34
  docile (1.1.5)
35
35
  dotenv (2.2.1)
36
- erubi (1.7.0)
37
- i18n (0.9.3)
36
+ erubi (1.9.0)
37
+ i18n (1.8.5)
38
38
  concurrent-ruby (~> 1.0)
39
- json (2.1.0)
40
- loofah (2.2.2)
39
+ json (2.3.0)
40
+ loofah (2.7.0)
41
41
  crass (~> 1.0.2)
42
42
  nokogiri (>= 1.5.9)
43
- method_source (0.9.0)
44
- mini_portile2 (2.3.0)
45
- minitest (5.11.3)
46
- nokogiri (1.8.5)
47
- mini_portile2 (~> 2.3.0)
48
- rack (2.0.4)
49
- rack-test (0.8.2)
43
+ method_source (1.0.0)
44
+ mini_portile2 (2.4.0)
45
+ minitest (5.14.2)
46
+ nokogiri (1.10.10)
47
+ mini_portile2 (~> 2.4.0)
48
+ rack (2.2.3)
49
+ rack-test (1.1.0)
50
50
  rack (>= 1.0, < 3)
51
51
  rails-dom-testing (2.0.3)
52
52
  activesupport (>= 4.2.0)
53
53
  nokogiri (>= 1.6)
54
- rails-html-sanitizer (1.0.4)
55
- loofah (~> 2.2, >= 2.2.2)
56
- railties (5.1.4)
57
- actionpack (= 5.1.4)
58
- activesupport (= 5.1.4)
54
+ rails-html-sanitizer (1.3.0)
55
+ loofah (~> 2.3)
56
+ railties (5.2.4.4)
57
+ actionpack (= 5.2.4.4)
58
+ activesupport (= 5.2.4.4)
59
59
  method_source
60
60
  rake (>= 0.8.7)
61
- thor (>= 0.18.1, < 2.0)
62
- rake (10.5.0)
63
- rspec-core (3.7.1)
64
- rspec-support (~> 3.7.0)
65
- rspec-expectations (3.7.0)
61
+ thor (>= 0.19.0, < 2.0)
62
+ rake (12.3.3)
63
+ rspec-core (3.9.3)
64
+ rspec-support (~> 3.9.3)
65
+ rspec-expectations (3.9.3)
66
66
  diff-lcs (>= 1.2.0, < 2.0)
67
- rspec-support (~> 3.7.0)
68
- rspec-mocks (3.7.0)
67
+ rspec-support (~> 3.9.0)
68
+ rspec-mocks (3.9.1)
69
69
  diff-lcs (>= 1.2.0, < 2.0)
70
- rspec-support (~> 3.7.0)
71
- rspec-rails (3.7.2)
70
+ rspec-support (~> 3.9.0)
71
+ rspec-rails (3.9.1)
72
72
  actionpack (>= 3.0)
73
73
  activesupport (>= 3.0)
74
74
  railties (>= 3.0)
75
- rspec-core (~> 3.7.0)
76
- rspec-expectations (~> 3.7.0)
77
- rspec-mocks (~> 3.7.0)
78
- rspec-support (~> 3.7.0)
79
- rspec-support (3.7.1)
75
+ rspec-core (~> 3.9.0)
76
+ rspec-expectations (~> 3.9.0)
77
+ rspec-mocks (~> 3.9.0)
78
+ rspec-support (~> 3.9.0)
79
+ rspec-support (3.9.4)
80
80
  simplecov (0.15.1)
81
81
  docile (~> 1.1.0)
82
82
  json (>= 1.8, < 3)
83
83
  simplecov-html (~> 0.10.0)
84
84
  simplecov-html (0.10.2)
85
- thor (0.20.0)
85
+ thor (1.0.1)
86
86
  thread_safe (0.3.6)
87
- tzinfo (1.2.5)
87
+ tzinfo (1.2.7)
88
88
  thread_safe (~> 0.1)
89
89
 
90
90
  PLATFORMS
@@ -94,8 +94,8 @@ DEPENDENCIES
94
94
  bundler (~> 1.9)
95
95
  decanter!
96
96
  dotenv
97
- rake (~> 10.0)
98
- rspec-rails
97
+ rake (~> 12.0)
98
+ rspec-rails (~> 3.9)
99
99
  simplecov (~> 0.15.1)
100
100
 
101
101
  BUNDLED WITH
data/README.md CHANGED
@@ -220,6 +220,25 @@ end
220
220
 
221
221
  _Note: we recommend using [Active Record validations](https://guides.rubyonrails.org/active_record_validations.html) to check for presence of an attribute, rather than using the `required` option. This method is intended for use in non-RESTful routes or cases where Active Record validations are not available._
222
222
 
223
+
224
+ ### Default values
225
+
226
+ If you provide the option `:default_value` for an input in your decanter, the input key will be initialized with the given default value. Input keys not found in the incoming data parameters will be set to the provided default rather than ignoring the missing key. Note: `nil` and empty keys will not be overridden.
227
+
228
+ ```ruby
229
+ class TripDecanter < Decanter::Base
230
+ input :name, :string
231
+ input :destination, :string, default_value: 'Chicago'
232
+ end
233
+
234
+ ```
235
+
236
+ ```
237
+ TripDecanter.decant({ name: 'Vacation 2020' })
238
+ => { name: 'Vacation 2020', destination: 'Chicago' }
239
+
240
+ ```
241
+
223
242
  ### Global configuration
224
243
 
225
244
  You can generate a local copy of the default configuration with `rails generate decanter:install`. This will create an initializer where you can do global configuration:
@@ -28,12 +28,12 @@ Gem::Specification.new do |spec|
28
28
  spec.require_paths = ['lib']
29
29
 
30
30
  spec.add_dependency 'actionpack', '>= 4.2.10'
31
- spec.add_dependency 'activesupport'
31
+ spec.add_dependency 'activesupport', '~> 5.2'
32
32
  spec.add_dependency 'rails-html-sanitizer', '>= 1.0.4'
33
33
 
34
34
  spec.add_development_dependency 'bundler', '~> 1.9'
35
35
  spec.add_development_dependency 'dotenv'
36
- spec.add_development_dependency 'rake', '~> 10.0'
37
- spec.add_development_dependency 'rspec-rails'
36
+ spec.add_development_dependency 'rake', '~> 12.0'
37
+ spec.add_development_dependency 'rspec-rails', '~> 3.9'
38
38
  spec.add_development_dependency 'simplecov', '~> 0.15.1'
39
39
  end
@@ -1,12 +1,13 @@
1
1
  module Decanter
2
2
  module Core
3
-
3
+ DEFAULT_VALUE_KEY = :default_value
4
+
4
5
  def self.included(base)
5
6
  base.extend(ClassMethods)
6
7
  end
7
8
 
8
9
  module ClassMethods
9
-
10
+
10
11
  def input(name, parsers=nil, **options)
11
12
 
12
13
  _name = [name].flatten
@@ -57,11 +58,26 @@ module Decanter
57
58
  return handle_empty_args if args.blank?
58
59
  return empty_required_input_error unless required_input_keys_present?(args)
59
60
  args = args.to_unsafe_h.with_indifferent_access if args.class.name == 'ActionController::Parameters'
60
- {}.merge( unhandled_keys(args) )
61
+ {}.merge( default_keys )
62
+ .merge( unhandled_keys(args) )
61
63
  .merge( handled_keys(args) )
62
64
  end
63
65
 
64
- def handle_empty_args
66
+ def default_keys
67
+ # return keys with provided default value when key is not defined within incoming args
68
+ default_result = default_value_inputs
69
+ .map { |input| [input[:key], input[:options][DEFAULT_VALUE_KEY]] }
70
+ .to_h
71
+
72
+ # parse default values
73
+ handled_keys(default_result)
74
+ end
75
+
76
+ def default_value_inputs
77
+ handlers.values.select { |input| input[:options].key?(DEFAULT_VALUE_KEY) }
78
+ end
79
+
80
+ def handle_empty_args
65
81
  any_inputs_required? ? empty_args_error : {}
66
82
  end
67
83
 
@@ -73,7 +89,7 @@ module Decanter
73
89
  handlers.map do |h|
74
90
  options = h.last[:options]
75
91
  h.first.first if options && options[:required]
76
- end
92
+ end
77
93
  end
78
94
 
79
95
  def required_input_keys_present?(args={})
@@ -100,7 +116,7 @@ module Decanter
100
116
  keys_to_ignore -
101
117
  handlers.values
102
118
  .select { |handler| handler[:type] != :input }
103
- .map { |handler| "#{handler[:name]}_attributes".to_sym }
119
+ .map { |handler| "#{handler[:name]}_attributes".to_sym }
104
120
 
105
121
  return {} unless unhandled_keys.any?
106
122
  raise(UnhandledKeysError, "#{self.name} received unhandled keys: #{unhandled_keys.join(', ')}.") if strict_mode
@@ -185,13 +201,13 @@ module Decanter
185
201
  def parse(key, parsers, value, options)
186
202
  return { key => value } unless parsers
187
203
  if options[:required] && value_missing?(value)
188
- raise ArgumentError.new("No value for required argument: #{key}")
204
+ raise ArgumentError.new("No value for required argument: #{key}")
189
205
  end
190
206
  parser_classes = Parser.parsers_for(parsers)
191
207
  Parser.compose_parsers(parser_classes).parse(key, value, options)
192
208
  end
193
209
 
194
- def handlers
210
+ def handlers
195
211
  @handlers ||= {}
196
212
  end
197
213
 
@@ -13,17 +13,16 @@ module Decanter
13
13
  unless item_parsers.all? { |parser| parser <= ValueParser }
14
14
  raise Decanter::ParseError.new 'parser(s) for array items must subclass ValueParser'
15
15
  end
16
- # Compose supplied parsers
16
+ # Compose supplied parsers
17
17
  item_parser = Parser.compose_parsers(item_parsers)
18
18
  # Parse all values
19
- val.map do |item|
20
- # Value parsers will expect a "key" for the value they're parsing,
19
+ val.map do |item|
20
+ # Value parsers will expect a "key" for the value they're parsing,
21
21
  # so we provide a dummy one.
22
22
  result = item_parser.parse(DUMMY_VALUE_KEY, item, options)
23
23
  result[DUMMY_VALUE_KEY]
24
- end
24
+ end.compact
25
25
  end
26
-
27
26
  end
28
27
  end
29
28
  end
@@ -1,3 +1,3 @@
1
1
  module Decanter
2
- VERSION = '3.0.1'.freeze
2
+ VERSION = '3.2.1'.freeze
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: 3.0.1
4
+ version: 3.2.1
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: 2020-03-12 00:00:00.000000000 Z
12
+ date: 2020-11-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionpack
@@ -29,16 +29,16 @@ dependencies:
29
29
  name: activesupport
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ">="
32
+ - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: '0'
34
+ version: '5.2'
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ">="
39
+ - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '0'
41
+ version: '5.2'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: rails-html-sanitizer
44
44
  requirement: !ruby/object:Gem::Requirement
@@ -87,28 +87,28 @@ dependencies:
87
87
  requirements:
88
88
  - - "~>"
89
89
  - !ruby/object:Gem::Version
90
- version: '10.0'
90
+ version: '12.0'
91
91
  type: :development
92
92
  prerelease: false
93
93
  version_requirements: !ruby/object:Gem::Requirement
94
94
  requirements:
95
95
  - - "~>"
96
96
  - !ruby/object:Gem::Version
97
- version: '10.0'
97
+ version: '12.0'
98
98
  - !ruby/object:Gem::Dependency
99
99
  name: rspec-rails
100
100
  requirement: !ruby/object:Gem::Requirement
101
101
  requirements:
102
- - - ">="
102
+ - - "~>"
103
103
  - !ruby/object:Gem::Version
104
- version: '0'
104
+ version: '3.9'
105
105
  type: :development
106
106
  prerelease: false
107
107
  version_requirements: !ruby/object:Gem::Requirement
108
108
  requirements:
109
- - - ">="
109
+ - - "~>"
110
110
  - !ruby/object:Gem::Version
111
- version: '0'
111
+ version: '3.9'
112
112
  - !ruby/object:Gem::Dependency
113
113
  name: simplecov
114
114
  requirement: !ruby/object:Gem::Requirement
@@ -132,6 +132,7 @@ extensions: []
132
132
  extra_rdoc_files: []
133
133
  files:
134
134
  - ".codeclimate.yml"
135
+ - ".github/CODEOWNERS"
135
136
  - ".github/ISSUE_TEMPLATE/BUG_REPORT.md"
136
137
  - ".github/ISSUE_TEMPLATE/FEATURE_REQUEST.md"
137
138
  - ".github/PULL_REQUEST_TEMPLATE.md"