blueprinter 0.11.0 → 0.12.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 90196d2f88f21663e58a83d29f17e086b01ff4b4
4
- data.tar.gz: d5773b0408dd8d11aede3f3970e618d43bc36990
3
+ metadata.gz: 56d342a22edbc9e7bc26d13d955b04a0a810a257
4
+ data.tar.gz: a6866070a40dc666f43bb04bb461a1065b45104b
5
5
  SHA512:
6
- metadata.gz: efec9cf0d62ecd854144535b84cf421cfd34191b30aa8896bbd34753e75a172a056fefe6940ab94791d750f12e48838edb4a8a3e9dfb64fdfa10897877f9c2be
7
- data.tar.gz: bb09550e791977703ec91448bab4da25781852b228f7c97feca16fde2a2c3273731bfac4c4906a83e95a278908347405a9d8f1fb5f346cd0997cffcbbb808d88
6
+ metadata.gz: 7f697e391cfc1ebf5ef3967ca73447b4ad96a2f1f5424289d865d096e7a24de84e97b19116e9c8cfdef16bac05dab71637c90c26c0d679d36b4823d3c855fb35
7
+ data.tar.gz: a2a88c85b099bf56c1eaa97eeb409d4aaec9567f8582cdd2abba34ea157abf21e3a7fb24511c3bb3be87f314d957ab51a43ee39ea9df11ba9091487d93e6bb93
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.12.0 - 2019/1/16
2
+
3
+ * 🚀 [FEATURE] Enables the setting of global `:field_default` and `:association_default` option value in the Blueprinter Configuration that will be used as default values for fields and associations that evaluate to nil. [#128](https://github.com/procore/blueprinter/pull/128). Thanks to [@mcclayton](https://github.com/mcclayton).
4
+
1
5
  ## 0.11.0 - 2019/1/15
2
6
 
3
7
  * 🚀 [FEATURE] Enables the setting of a global `:if`/`:unless` proc in the Blueprinter Configuration that will be used to evaluate the conditional render of all fields. [#127](https://github.com/procore/blueprinter/pull/127). Thanks to [@mcclayton](https://github.com/mcclayton).
data/README.md CHANGED
@@ -180,7 +180,17 @@ Output:
180
180
  ```
181
181
 
182
182
  ### Default Association/Field Option
183
- By default, an association or field that evaluates to `nil` is serialized as `nil`. A default serialized value can be specified as option on the association or field for cases when the association/field could potentially evaluate to `nil`.
183
+ By default, an association or field that evaluates to `nil` is serialized as `nil`. A default serialized value can be specified as an option on the association or field for cases when the association/field could potentially evaluate to `nil`. You can also specify a global `field_default` or `association_default` in the Blueprinter config which will be used for all fields/associations that evaluate to nil.
184
+
185
+ #### Global Config Setting
186
+ ```ruby
187
+ Blueprinter.configure do |config|
188
+ config.field_default = "N/A"
189
+ config.association_default = {}
190
+ end
191
+ ```
192
+
193
+ #### Field-level/Associaion-level Setting
184
194
  ```ruby
185
195
  class UserBlueprint < Blueprinter::Base
186
196
  identifier :uuid
@@ -1,10 +1,12 @@
1
1
  module Blueprinter
2
2
  class Configuration
3
- attr_accessor :generator, :if, :method, :sort_fields_by, :unless
3
+ attr_accessor :association_default, :field_default, :generator, :if, :method, :sort_fields_by, :unless
4
4
 
5
5
  VALID_CALLABLES = %i(if unless).freeze
6
6
 
7
7
  def initialize
8
+ @association_default = nil
9
+ @field_default = nil
8
10
  @generator = JSON
9
11
  @if = nil
10
12
  @method = :generate
@@ -6,14 +6,18 @@ module Blueprinter
6
6
 
7
7
  def extract(association_name, object, local_options, options={})
8
8
  value = @extractor.extract(association_name, object, local_options, options.except(:default))
9
- return options[:default] if value.nil?
9
+ return default_value(options) if value.nil?
10
10
  view = options[:view] || :default
11
11
  blueprint = association_blueprint(options[:blueprint], value)
12
12
  blueprint.prepare(value, view_name: view, local_options: local_options)
13
13
  end
14
-
14
+
15
15
  private
16
16
 
17
+ def default_value(association_options)
18
+ association_options.key?(:default) ? association_options.fetch(:default) : Blueprinter.configuration.association_default
19
+ end
20
+
17
21
  def association_blueprint(blueprint, value)
18
22
  blueprint.is_a?(Proc) ? blueprint.call(value) : blueprint
19
23
  end
@@ -9,11 +9,15 @@ module Blueprinter
9
9
  def extract(field_name, object, local_options, options = {})
10
10
  extraction = extractor(object, options).extract(field_name, object, local_options, options)
11
11
  value = options.key?(:datetime_format) ? format_datetime(extraction, options[:datetime_format]) : extraction
12
- value.nil? ? options[:default] : value
12
+ value.nil? ? default_value(options) : value
13
13
  end
14
14
 
15
15
  private
16
16
 
17
+ def default_value(field_options)
18
+ field_options.key?(:default) ? field_options.fetch(:default) : Blueprinter.configuration.field_default
19
+ end
20
+
17
21
  def extractor(object, options)
18
22
  if options[:block]
19
23
  @block_extractor
@@ -1,3 +1,3 @@
1
1
  module Blueprinter
2
- VERSION = '0.11.0'
2
+ VERSION = '0.12.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blueprinter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Hess
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-01-18 00:00:00.000000000 Z
12
+ date: 2019-01-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: factory_bot