blueprinter 0.22.0 → 0.23.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
  SHA256:
3
- metadata.gz: 335ed3cf4448c0972904b81679e8e95304ab075064c1022555062b8eb07eaf12
4
- data.tar.gz: fd442388bb18481015b4ae8ed79ca9b79b9a34f6eef6f5b31eb9ef473c458287
3
+ metadata.gz: 2a1440e4adce199d743371035d3061ae69aea0ab8030bcbdabe59b23cd995c34
4
+ data.tar.gz: b1a58870b18665b6bf1a7fe07ac3993cea0d25450c73a00f241f981ab4b9ad95
5
5
  SHA512:
6
- metadata.gz: 6855988c4693065d2620a19995f326718fd14796b0ec856b1ace0d5e91068959c72ada4788881e4b7bc2fbe587f56cf146dba06817431feaa6fb7d487d2d1eba
7
- data.tar.gz: 4aef0b54eea76568616e860863d782caced131399cdd36bdec2874f5b701b139517367556fe880f5041b7acd4fe6619eba79f8c7180c8fe8b31f09bd279cdc5d
6
+ metadata.gz: b20e40f673a754d4d20ccacb2acc8caceeb79d0cced6f69bf4003fa6df6bde5c883baf40954d9fcc9dcde5394f489f54f8a9e1f3f4c3db8eb8494ea88d860e50
7
+ data.tar.gz: 6192b46a62e77bb267c1b03f15686412efebbddf30dc10931941f2966ddc0bad4f97ec8823d3f4c939d9902c3cac89a3a5ebd209c61798314e2f94c94a537249
@@ -1,3 +1,11 @@
1
+ ## 0.23.0 - 2019/1/31
2
+ * 🚀 [FEATURE] Configurable default extractor introduced in [#198](https://github.com/procore/blueprinter/pull/198) by [@wlkrw](https://github.com/wlkrw). You can now set a default extractor like so:
3
+ ```
4
+ Blueprinter.configure do |config|
5
+ config.extractor_default = MyAutoExtractor
6
+ end
7
+ ```
8
+
1
9
  ## 0.22.0 - 2019/12/26
2
10
  * 🚀 [FEATURE] Add rails generators. See `rails g blueprinter:blueprint --help` for usage. Introduced in [#176](https://github.com/procore/blueprinter/pull/176) by [@wlkrw](https://github.com/wlkrw).
3
11
 
data/README.md CHANGED
@@ -742,6 +742,57 @@ end
742
742
  ---
743
743
  </details>
744
744
 
745
+ <details>
746
+ <summary>Configurable Extractors</summary>
747
+
748
+ ---
749
+
750
+ Blueprinter gets a given objects' values from the fields definitions using extractor classes. You can substitute your own extractor class globally or per-field.
751
+
752
+ #### Examples
753
+
754
+ For a specific kind of field, create an extractor class extending from `Blueprinter::Extractor`
755
+ ```ruby
756
+ class MyFieldExtractor < Blueprinter::Extractor
757
+ def extract(_field_name, _object, _local_options, _options={})
758
+ # process your obscure_object
759
+ _object.clarified
760
+ end
761
+ end
762
+ ```
763
+
764
+ ```ruby
765
+ class MysteryBlueprint < Blueprinter::Base
766
+ field :obscure_object, extractor: MyFieldExtractor
767
+ end
768
+ ```
769
+
770
+ For a global default, create an extractor class extending from `Blueprinter::AutoExtractor` and set the `extractor_default` configuration
771
+ ```ruby
772
+ class MyAutoExtractor < Blueprinter::AutoExtractor
773
+ def initialize
774
+ super
775
+ @my_field_extractor = MyFieldExtractor.new
776
+ end
777
+ def extractor(object, options)
778
+ # dispatch to any class AutoExtractor can, plus more
779
+ if detect_obscurity(object)
780
+ @my_field_extractor
781
+ else
782
+ super
783
+ end
784
+ end
785
+ end
786
+ ```
787
+
788
+ ```ruby
789
+ Blueprinter.configure do |config|
790
+ config.extractor_default = MyAutoExtractor
791
+ end
792
+ ```
793
+
794
+ ---
795
+ </details>
745
796
 
746
797
  <details>
747
798
  <summary>Sorting Fields</summary>
@@ -51,7 +51,7 @@ module Blueprinter
51
51
  # end
52
52
  #
53
53
  # @return [Field] A Field object
54
- def self.identifier(method, name: method, extractor: AutoExtractor.new, &block)
54
+ def self.identifier(method, name: method, extractor: Blueprinter.configuration.extractor_default.new, &block)
55
55
  view_collection[:identifier] << Field.new(
56
56
  method,
57
57
  name,
@@ -120,7 +120,7 @@ module Blueprinter
120
120
  current_view << Field.new(
121
121
  method,
122
122
  options.fetch(:name) { method },
123
- options.fetch(:extractor) { AutoExtractor.new },
123
+ options.fetch(:extractor) { Blueprinter.configuration.extractor_default.new },
124
124
  self,
125
125
  options.merge(block: block),
126
126
  )
@@ -1,6 +1,6 @@
1
1
  module Blueprinter
2
2
  class Configuration
3
- attr_accessor :association_default, :datetime_format, :field_default, :generator, :if, :method, :sort_fields_by, :unless
3
+ attr_accessor :association_default, :datetime_format, :field_default, :generator, :if, :method, :sort_fields_by, :unless, :extractor_default
4
4
 
5
5
  VALID_CALLABLES = %i(if unless).freeze
6
6
 
@@ -13,6 +13,7 @@ module Blueprinter
13
13
  @method = :generate
14
14
  @sort_fields_by = :name_asc
15
15
  @unless = nil
16
+ @extractor_default = AutoExtractor
16
17
  end
17
18
 
18
19
  def jsonify(blob)
@@ -4,7 +4,7 @@ module Blueprinter
4
4
  include EmptyTypes
5
5
 
6
6
  def initialize
7
- @extractor = AutoExtractor.new
7
+ @extractor = Blueprinter.configuration.extractor_default.new
8
8
  end
9
9
 
10
10
  def extract(association_name, object, local_options, options={})
@@ -1,3 +1,3 @@
1
1
  module Blueprinter
2
- VERSION = '0.22.0'.freeze
2
+ VERSION = '0.23.0'.freeze
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.22.0
4
+ version: 0.23.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-12-26 00:00:00.000000000 Z
12
+ date: 2020-01-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: factory_bot