blueprinter 0.22.0 → 0.23.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +51 -0
- data/lib/blueprinter/base.rb +2 -2
- data/lib/blueprinter/configuration.rb +2 -1
- data/lib/blueprinter/extractors/association_extractor.rb +1 -1
- data/lib/blueprinter/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a1440e4adce199d743371035d3061ae69aea0ab8030bcbdabe59b23cd995c34
|
4
|
+
data.tar.gz: b1a58870b18665b6bf1a7fe07ac3993cea0d25450c73a00f241f981ab4b9ad95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b20e40f673a754d4d20ccacb2acc8caceeb79d0cced6f69bf4003fa6df6bde5c883baf40954d9fcc9dcde5394f489f54f8a9e1f3f4c3db8eb8494ea88d860e50
|
7
|
+
data.tar.gz: 6192b46a62e77bb267c1b03f15686412efebbddf30dc10931941f2966ddc0bad4f97ec8823d3f4c939d9902c3cac89a3a5ebd209c61798314e2f94c94a537249
|
data/CHANGELOG.md
CHANGED
@@ -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>
|
data/lib/blueprinter/base.rb
CHANGED
@@ -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:
|
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) {
|
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)
|
data/lib/blueprinter/version.rb
CHANGED
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.
|
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:
|
12
|
+
date: 2020-01-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: factory_bot
|