blueprinter 0.10.0 → 0.11.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 +4 -4
- data/CHANGELOG.md +28 -24
- data/README.md +14 -3
- data/lib/blueprinter/configuration.rb +9 -1
- data/lib/blueprinter/field.rb +12 -4
- data/lib/blueprinter/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 90196d2f88f21663e58a83d29f17e086b01ff4b4
|
|
4
|
+
data.tar.gz: d5773b0408dd8d11aede3f3970e618d43bc36990
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: efec9cf0d62ecd854144535b84cf421cfd34191b30aa8896bbd34753e75a172a056fefe6940ab94791d750f12e48838edb4a8a3e9dfb64fdfa10897877f9c2be
|
|
7
|
+
data.tar.gz: bb09550e791977703ec91448bab4da25781852b228f7c97feca16fde2a2c3273731bfac4c4906a83e95a278908347405a9d8f1fb5f346cd0997cffcbbb808d88
|
data/CHANGELOG.md
CHANGED
|
@@ -1,23 +1,27 @@
|
|
|
1
|
+
## 0.11.0 - 2019/1/15
|
|
2
|
+
|
|
3
|
+
* 🚀 [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).
|
|
4
|
+
|
|
1
5
|
## 0.10.0 - 2018/12/20
|
|
2
6
|
|
|
3
|
-
* [FEATURE] Association Blueprints can be dynamically evaluated using a proc. [#122](https://github.com/procore/blueprinter/pull/122). Thanks to [@ritikesh](https://github.com/ritikesh).
|
|
7
|
+
* 🚀 [FEATURE] Association Blueprints can be dynamically evaluated using a proc. [#122](https://github.com/procore/blueprinter/pull/122). Thanks to [@ritikesh](https://github.com/ritikesh).
|
|
4
8
|
|
|
5
9
|
## 0.9.0 - 2018/11/29
|
|
6
10
|
|
|
7
|
-
* [FEATURE] Added a `render_as_json` API. Similar to `render_as_hash` but returns a JSONified hash. Please see pr [#119](https://github.com/procore/blueprinter/pull/119). Thanks to [@ritikesh](https://github.com/ritikesh).
|
|
8
|
-
* [FEATURE] Sorting of fields in the response is now configurable to sort by definition or by name(asc only). Please see pr [#119](https://github.com/procore/blueprinter/pull/119). Thanks to [@ritikesh](https://github.com/ritikesh).
|
|
9
|
-
* [ENHANCEMENT] Updated readme for above features and some existing undocumented features like `exclude fields`, `render_as_hash`. Please see pr [#119](https://github.com/procore/blueprinter/pull/119). Thanks to [@ritikesh](https://github.com/ritikesh).
|
|
11
|
+
* 🚀 [FEATURE] Added a `render_as_json` API. Similar to `render_as_hash` but returns a JSONified hash. Please see pr [#119](https://github.com/procore/blueprinter/pull/119). Thanks to [@ritikesh](https://github.com/ritikesh).
|
|
12
|
+
* 🚀 [FEATURE] Sorting of fields in the response is now configurable to sort by definition or by name(asc only). Please see pr [#119](https://github.com/procore/blueprinter/pull/119). Thanks to [@ritikesh](https://github.com/ritikesh).
|
|
13
|
+
* 💅 [ENHANCEMENT] Updated readme for above features and some existing undocumented features like `exclude fields`, `render_as_hash`. Please see pr [#119](https://github.com/procore/blueprinter/pull/119). Thanks to [@ritikesh](https://github.com/ritikesh).
|
|
10
14
|
|
|
11
15
|
## 0.8.0 - 2018/11/19
|
|
12
16
|
|
|
13
|
-
* [FEATURE] Extend Support for other JSON encoders like yajl-ruby. Please see pr [#118](https://github.com/procore/blueprinter/pull/118). Thanks to [@ritikesh](https://github.com/ritikesh).
|
|
14
|
-
* [BUGFIX] Do not raise error on null date with `date_format` option. Please see pr [#117](https://github.com/procore/blueprinter/pull/117). Thanks to [@tpltn](https://github.com/tpltn).
|
|
15
|
-
* [FEATURE] Add `default` option to `field`s which will be used as the serialized value instead of `null` when the field evaluates to null. Please see pr [#115](https://github.com/procore/blueprinter/pull/115). Thanks to [@mcclayton](https://github.com/mcclayton).
|
|
16
|
-
* [BUGFIX] Made Base.associations completely private since they are not used outside of the Blueprinter base. Please see pr [#112](https://github.com/procore/blueprinter/pull/112). Thanks to [@philipqnguyen](https://github.com/philipqnguyen).
|
|
17
|
-
* [BUGFIX] Fix issue where entire Blueprinter module was marked api private. Please see pr [#111](https://github.com/procore/blueprinter/pull/111). Thanks to [@philipqnguyen](https://github.com/philipqnguyen).
|
|
18
|
-
* [FEATURE] Allow identifiers to be defined with a block. Please see pr [#110](https://github.com/procore/blueprinter/pull/110). Thanks to [@hugopeixoto](https://github.com/hugopeixoto).
|
|
19
|
-
* [ENHANCEMENT] Update docs regarding the args yielded to blocks. Please see pr [#108](https://github.com/procore/blueprinter/pull/108). Thanks to [@philipqnguyen](https://github.com/philipqnguyen).
|
|
20
|
-
* [ENHANCEMENT] Use `field` method in fields. Please see pr [#107](https://github.com/procore/blueprinter/pull/107). Thanks to [@hugopeixoto](https://github.com/hugopeixoto).
|
|
17
|
+
* 🚀 [FEATURE] Extend Support for other JSON encoders like yajl-ruby. Please see pr [#118](https://github.com/procore/blueprinter/pull/118). Thanks to [@ritikesh](https://github.com/ritikesh).
|
|
18
|
+
* 🐛 [BUGFIX] Do not raise error on null date with `date_format` option. Please see pr [#117](https://github.com/procore/blueprinter/pull/117). Thanks to [@tpltn](https://github.com/tpltn).
|
|
19
|
+
* 🚀 [FEATURE] Add `default` option to `field`s which will be used as the serialized value instead of `null` when the field evaluates to null. Please see pr [#115](https://github.com/procore/blueprinter/pull/115). Thanks to [@mcclayton](https://github.com/mcclayton).
|
|
20
|
+
* 🐛 [BUGFIX] Made Base.associations completely private since they are not used outside of the Blueprinter base. Please see pr [#112](https://github.com/procore/blueprinter/pull/112). Thanks to [@philipqnguyen](https://github.com/philipqnguyen).
|
|
21
|
+
* 🐛 [BUGFIX] Fix issue where entire Blueprinter module was marked api private. Please see pr [#111](https://github.com/procore/blueprinter/pull/111). Thanks to [@philipqnguyen](https://github.com/philipqnguyen).
|
|
22
|
+
* 🚀 [FEATURE] Allow identifiers to be defined with a block. Please see pr [#110](https://github.com/procore/blueprinter/pull/110). Thanks to [@hugopeixoto](https://github.com/hugopeixoto).
|
|
23
|
+
* 💅 [ENHANCEMENT] Update docs regarding the args yielded to blocks. Please see pr [#108](https://github.com/procore/blueprinter/pull/108). Thanks to [@philipqnguyen](https://github.com/philipqnguyen).
|
|
24
|
+
* 💅 [ENHANCEMENT] Use `field` method in fields. Please see pr [#107](https://github.com/procore/blueprinter/pull/107). Thanks to [@hugopeixoto](https://github.com/hugopeixoto).
|
|
21
25
|
|
|
22
26
|
## 0.7.0 - 2018/10/17
|
|
23
27
|
|
|
@@ -26,23 +30,23 @@
|
|
|
26
30
|
|
|
27
31
|
## 0.6.0 - 2018/06/05
|
|
28
32
|
|
|
29
|
-
* [FEATURE] Add `date_time` format as an option to `field`. Please see pr #68. Thanks to [@njbbaer](https://github.com/njbbaer).
|
|
30
|
-
* [FEATURE] Add conditional field support `:unless` and `:if` as an option to `field`. Please see pr [#86](https://github.com/procore/blueprinter/pull/86). Thanks to [@ojab](https://github.com/ojab).
|
|
31
|
-
* [BUGFIX] Fix case where miscellaneous options were not being passed through the `AutoExtractor`. See pr [#83](https://github.com/procore/blueprinter/pull/83).
|
|
33
|
+
* 🚀 [FEATURE] Add `date_time` format as an option to `field`. Please see pr #68. Thanks to [@njbbaer](https://github.com/njbbaer).
|
|
34
|
+
* 🚀 [FEATURE] Add conditional field support `:unless` and `:if` as an option to `field`. Please see pr [#86](https://github.com/procore/blueprinter/pull/86). Thanks to [@ojab](https://github.com/ojab).
|
|
35
|
+
* 🐛 [BUGFIX] Fix case where miscellaneous options were not being passed through the `AutoExtractor`. See pr [#83](https://github.com/procore/blueprinter/pull/83).
|
|
32
36
|
|
|
33
37
|
## 0.5.0 - 2018/05/15
|
|
34
38
|
|
|
35
|
-
* [FEATURE] Add `default` option to `association` which will be used as the serialized value instead of `null` when the association evaluates to null.
|
|
39
|
+
* 🚀 [FEATURE] Add `default` option to `association` which will be used as the serialized value instead of `null` when the association evaluates to null.
|
|
36
40
|
See PR [#78](https://github.com/procore/blueprinter/pull/78) by [@vinaya-procore](https://github.com/vinaya-procore).
|
|
37
41
|
|
|
38
42
|
## 0.4.0 - 2018/05/02
|
|
39
43
|
|
|
40
|
-
* [FEATURE] Add `render_as_hash` which will output a hash instead of
|
|
44
|
+
* 🚀 [FEATURE] Add `render_as_hash` which will output a hash instead of
|
|
41
45
|
a JSON String. See PR [#76](https://github.com/procore/blueprinter/pull/76) by [@amayer171](https://github.com/amayer171) and Issue [#73](https://github.com/procore/blueprinter/issues/73).
|
|
42
46
|
|
|
43
47
|
## 0.3.0 - 2018/04/05
|
|
44
48
|
|
|
45
|
-
Sort of a breaking Change. Serializer classes has been renamed to Extractor. To upgrade, if you passed in a specific serializer to `field` or `identifier` such as:
|
|
49
|
+
💥 [BREAKING] Sort of a breaking Change. Serializer classes has been renamed to Extractor. To upgrade, if you passed in a specific serializer to `field` or `identifier` such as:
|
|
46
50
|
|
|
47
51
|
```
|
|
48
52
|
field(:first_name, serializer: CustomSerializer)
|
|
@@ -54,19 +58,19 @@ Please rename that to:
|
|
|
54
58
|
field(:first_name, extractor: CustomExtractor)
|
|
55
59
|
```
|
|
56
60
|
|
|
57
|
-
* [ENHANCEMENT] Renamed Serializer classes to Extractor. See #72.
|
|
58
|
-
* [ENHANCEMENT] Updated README. See pr [#66](https://github.com/procore/blueprinter/pull/66), [#65](https://github.com/procore/blueprinter/pull/65)
|
|
61
|
+
* 💅 [ENHANCEMENT] Renamed Serializer classes to Extractor. See #72.
|
|
62
|
+
* 💅 [ENHANCEMENT] Updated README. See pr [#66](https://github.com/procore/blueprinter/pull/66), [#65](https://github.com/procore/blueprinter/pull/65)
|
|
59
63
|
|
|
60
64
|
## 0.2.0 - 2018/01/22
|
|
61
65
|
|
|
62
|
-
Breaking Changes. To upgrade, ensure that any associated objects have a blueprint. For example:
|
|
66
|
+
💥 [BREAKING] Breaking Changes. To upgrade, ensure that any associated objects have a blueprint. For example:
|
|
63
67
|
```
|
|
64
68
|
association :comments, blueprint: CommentsBlueprint
|
|
65
69
|
```
|
|
66
70
|
|
|
67
|
-
* [BUGFIX] Remove Optimizer class. See [#61](https://github.com/procore/blueprinter/pull/61).
|
|
68
|
-
* [BUGFIX] Require associated objects to have a Blueprint, so that objects will always serialize properly. See [#60](https://github.com/procore/blueprinter/pull/60).
|
|
71
|
+
* 🐛 [BUGFIX] Remove Optimizer class. See [#61](https://github.com/procore/blueprinter/pull/61).
|
|
72
|
+
* 🐛 [BUGFIX] Require associated objects to have a Blueprint, so that objects will always serialize properly. See [#60](https://github.com/procore/blueprinter/pull/60).
|
|
69
73
|
|
|
70
74
|
## 0.1.0 - 2018/01/17
|
|
71
75
|
|
|
72
|
-
* [FEATURE] Initial release of Blueprinter
|
|
76
|
+
* 🚀 [FEATURE] Initial release of Blueprinter
|
data/README.md
CHANGED
|
@@ -201,7 +201,7 @@ end
|
|
|
201
201
|
|
|
202
202
|
class Project < ActiveRecord::Base
|
|
203
203
|
has_many :tasks, as: :taskable
|
|
204
|
-
|
|
204
|
+
|
|
205
205
|
def blueprint
|
|
206
206
|
ProjectBlueprint
|
|
207
207
|
end
|
|
@@ -374,10 +374,19 @@ Output:
|
|
|
374
374
|
}
|
|
375
375
|
```
|
|
376
376
|
|
|
377
|
-
### Conditional
|
|
377
|
+
### Conditional fields
|
|
378
378
|
|
|
379
|
-
`field` supports `:if` and `:unless` options
|
|
379
|
+
Both the `field` and the global Blueprinter Configuration supports `:if` and `:unless` options that can be used to serialize fields conditionally.
|
|
380
380
|
|
|
381
|
+
#### Global Config Setting
|
|
382
|
+
```ruby
|
|
383
|
+
Blueprinter.configure do |config|
|
|
384
|
+
config.if = ->(obj, _options) { obj.is_a?(Foo) }
|
|
385
|
+
config.unless = ->(obj, _options) { obj.is_a?(Bar) }
|
|
386
|
+
end
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
#### Field-level Setting
|
|
381
390
|
```ruby
|
|
382
391
|
class UserBlueprint < Blueprinter::Base
|
|
383
392
|
identifier :uuid
|
|
@@ -386,6 +395,8 @@ class UserBlueprint < Blueprinter::Base
|
|
|
386
395
|
end
|
|
387
396
|
```
|
|
388
397
|
|
|
398
|
+
The field-level setting overrides the global config setting (for the field) if both are set.
|
|
399
|
+
|
|
389
400
|
### Custom formatting for dates and times
|
|
390
401
|
To define a custom format for a Date or DateTime field, include the option `datetime_format` with the associated `strptime` format.
|
|
391
402
|
|
|
@@ -1,16 +1,24 @@
|
|
|
1
1
|
module Blueprinter
|
|
2
2
|
class Configuration
|
|
3
|
-
attr_accessor :generator, :method, :sort_fields_by
|
|
3
|
+
attr_accessor :generator, :if, :method, :sort_fields_by, :unless
|
|
4
|
+
|
|
5
|
+
VALID_CALLABLES = %i(if unless).freeze
|
|
4
6
|
|
|
5
7
|
def initialize
|
|
6
8
|
@generator = JSON
|
|
9
|
+
@if = nil
|
|
7
10
|
@method = :generate
|
|
8
11
|
@sort_fields_by = :name_asc
|
|
12
|
+
@unless = nil
|
|
9
13
|
end
|
|
10
14
|
|
|
11
15
|
def jsonify(blob)
|
|
12
16
|
generator.public_send(method, blob)
|
|
13
17
|
end
|
|
18
|
+
|
|
19
|
+
def valid_callable?(callable_name)
|
|
20
|
+
VALID_CALLABLES.include?(callable_name)
|
|
21
|
+
end
|
|
14
22
|
end
|
|
15
23
|
|
|
16
24
|
def self.configuration
|
data/lib/blueprinter/field.rb
CHANGED
|
@@ -30,15 +30,23 @@ class Blueprinter::Field
|
|
|
30
30
|
@unless_callable = callable_from(:unless)
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
-
def callable_from(
|
|
34
|
-
|
|
33
|
+
def callable_from(condition)
|
|
34
|
+
config = Blueprinter.configuration
|
|
35
|
+
|
|
36
|
+
# Use field-level callable, or when not defined, try global callable
|
|
37
|
+
tmp = if options.key?(condition)
|
|
38
|
+
options.fetch(condition)
|
|
39
|
+
elsif config.valid_callable?(condition)
|
|
40
|
+
config.public_send(condition)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
return false unless tmp
|
|
35
44
|
|
|
36
|
-
tmp = options.fetch(option_name)
|
|
37
45
|
case tmp
|
|
38
46
|
when Proc then tmp
|
|
39
47
|
when Symbol then blueprint.method(tmp)
|
|
40
48
|
else
|
|
41
|
-
raise ArgumentError, "#{tmp.class} is passed to :#{
|
|
49
|
+
raise ArgumentError, "#{tmp.class} is passed to :#{condition}"
|
|
42
50
|
end
|
|
43
51
|
end
|
|
44
52
|
end
|
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.11.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-
|
|
12
|
+
date: 2019-01-18 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: factory_bot
|