blueprinter 0.14.0 → 0.15.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 +3 -0
- data/README.md +40 -8
- data/lib/blueprinter/base.rb +8 -5
- data/lib/blueprinter/extractors/auto_extractor.rb +0 -1
- data/lib/blueprinter/formatters/date_time_formatter.rb +12 -3
- 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: a3d1968ce7648f29d301d61dd92a016b0a6ca65a515ad7ee196a4b6958634d3d
|
4
|
+
data.tar.gz: b8abfa3db3801439ff6405ec695863913081cdb0c339f983279cfad75812d867
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41f969a27e181dd1a6e7534223eb5419e7a5c279c97a4f2da4e775c0ec05b4cfac30aa2a102891331b29ef4b644424fefece493abf90b743cca9b9ebbaff1390
|
7
|
+
data.tar.gz: 187e6c5af1f43aab7b5043cb86dc3bea9dd705397eaae0888747a429ea7a4d903a6631eb35940a43960c7070b9a8297ecf236795ddaad4ecacae09b59c1735d4
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
## 0.15.0 - 2019/04/1
|
2
|
+
* 🚀 [FEATURE] Add ability to pass in `datetime_format` field option as either a string representing the strptime format, or a Proc which takes in the Date or DateTime object and returns the formatted date. [#145](https://github.com/procore/blueprinter/pull/145). Thanks to [@mcclayton](https://github.com/mcclayton).
|
3
|
+
|
1
4
|
## 0.14.0 - 2019/04/01
|
2
5
|
* 🚀 [FEATURE] Added a global `datetime_format` option. [#135](https://github.com/procore/blueprinter/pull/143). Thanks to [@ritikesh](https://github.com/ritikesh).
|
3
6
|
|
data/README.md
CHANGED
@@ -148,7 +148,7 @@ puts UserBlueprint.render(user, view: :normal, root: :user)
|
|
148
148
|
|
149
149
|
Output:
|
150
150
|
```json
|
151
|
-
{
|
151
|
+
{
|
152
152
|
"user": {
|
153
153
|
"uuid": "733f0758-8f21-4719-875f-262c3ec743af",
|
154
154
|
"first_name": "John",
|
@@ -182,7 +182,7 @@ puts json
|
|
182
182
|
|
183
183
|
Output:
|
184
184
|
```json
|
185
|
-
{
|
185
|
+
{
|
186
186
|
"user": {
|
187
187
|
"uuid": "733f0758-8f21-4719-875f-262c3ec743af",
|
188
188
|
"first_name": "John",
|
@@ -197,7 +197,7 @@ Output:
|
|
197
197
|
}
|
198
198
|
}
|
199
199
|
```
|
200
|
-
|
200
|
+
_NOTE:_ For meta attributes, a [root](#root) is mandatory.
|
201
201
|
|
202
202
|
### Exclude fields
|
203
203
|
You can specifically choose to exclude certain fields for specific views
|
@@ -324,7 +324,7 @@ class TaskBlueprint < Blueprinter::Base
|
|
324
324
|
end
|
325
325
|
end
|
326
326
|
```
|
327
|
-
|
327
|
+
_NOTE:_ `taskable.blueprint` should return a valid Blueprint class. Currently, `has_many` is not supported because of the very nature of polymorphic associations.
|
328
328
|
|
329
329
|
### Defining a field directly in the Blueprint
|
330
330
|
|
@@ -503,12 +503,26 @@ class UserBlueprint < Blueprinter::Base
|
|
503
503
|
end
|
504
504
|
```
|
505
505
|
|
506
|
-
The field-level setting overrides the global config setting (for the field) if both are set.
|
506
|
+
_NOTE:_ The field-level setting overrides the global config setting (for the field) if both are set.
|
507
507
|
|
508
508
|
### Custom formatting for dates and times
|
509
|
-
To define a custom format for a Date or DateTime field, include the option `datetime_format
|
509
|
+
To define a custom format for a Date or DateTime field, include the option `datetime_format`.
|
510
|
+
This global or field-level option can be either a string representing the associated `strptime` format,
|
511
|
+
or a Proc which receives the original Date/DateTime object and returns the formatted value.
|
512
|
+
When using a Proc, it is the Proc's responsibility to handle any errors in formatting.
|
510
513
|
|
511
|
-
|
514
|
+
|
515
|
+
#### Global Config Setting
|
516
|
+
If a global datetime_format is set (either as a string format or a Proc), this option will be
|
517
|
+
invoked and used to format all fields that respond to `strptime`.
|
518
|
+
```ruby
|
519
|
+
Blueprinter.configure do |config|
|
520
|
+
config.datetime_format = ->(datetime) { datetime.nil? ? datetime : datetime.strftime("%s").to_i }
|
521
|
+
end
|
522
|
+
```
|
523
|
+
|
524
|
+
#### Field-level Setting
|
525
|
+
Usage (String Option):
|
512
526
|
```ruby
|
513
527
|
class UserBlueprint < Blueprinter::Base
|
514
528
|
identifier :name
|
@@ -524,6 +538,24 @@ Output:
|
|
524
538
|
}
|
525
539
|
```
|
526
540
|
|
541
|
+
Usage (Proc Option):
|
542
|
+
```ruby
|
543
|
+
class UserBlueprint < Blueprinter::Base
|
544
|
+
identifier :name
|
545
|
+
field :birthday, datetime_format: ->(datetime) { datetime.nil? ? datetime : datetime.strftime("%s").to_i }
|
546
|
+
end
|
547
|
+
```
|
548
|
+
|
549
|
+
Output:
|
550
|
+
```json
|
551
|
+
{
|
552
|
+
"name": "John Doe",
|
553
|
+
"birthday": 762739200
|
554
|
+
}
|
555
|
+
```
|
556
|
+
|
557
|
+
_NOTE:_ The field-level setting overrides the global config setting (for the field) if both are set.
|
558
|
+
|
527
559
|
## Installation
|
528
560
|
Add this line to your application's Gemfile:
|
529
561
|
|
@@ -603,7 +635,7 @@ Blueprinter.configure do |config|
|
|
603
635
|
end
|
604
636
|
```
|
605
637
|
|
606
|
-
|
638
|
+
_NOTE:_ You should be doing this only if you aren't using `yajl-ruby` through the JSON API by requiring `yajl/json_gem`. More details [here](https://github.com/brianmario/yajl-ruby#json-gem-compatibility-api). In this case, `JSON.generate` is patched to use `Yajl::Encoder.encode` internally.
|
607
639
|
|
608
640
|
## How to Document
|
609
641
|
|
data/lib/blueprinter/base.rb
CHANGED
@@ -71,8 +71,11 @@ module Blueprinter
|
|
71
71
|
# @option options [Symbol] :name Use this to rename the method. Useful if
|
72
72
|
# if you want your JSON key named differently in the output than your
|
73
73
|
# object's field or method name.
|
74
|
-
# @option options [String] :datetime_format Format Date or DateTime object
|
75
|
-
# with given strftime
|
74
|
+
# @option options [String,Proc] :datetime_format Format Date or DateTime object
|
75
|
+
# If the option provided is a String, the object will be formatted with given strftime
|
76
|
+
# formatting.
|
77
|
+
# If this option is a Proc, the object will be formatted by calling the provided Proc
|
78
|
+
# on the Date/DateTime object.
|
76
79
|
# @option options [Symbol,Proc] :if Specifies a method, proc or string to
|
77
80
|
# call to determine if the field should be included (e.g.
|
78
81
|
# `if: :include_first_name?, or if: Proc.new { |user, options| options[:current_user] == user }).
|
@@ -171,7 +174,7 @@ module Blueprinter
|
|
171
174
|
# @option options [Symbol|String] :root Defaults to nil.
|
172
175
|
# Render the json/hash with a root key if provided.
|
173
176
|
# @option options [Any] :meta Defaults to nil.
|
174
|
-
# Render the json/hash with a meta attribute with provided value
|
177
|
+
# Render the json/hash with a meta attribute with provided value
|
175
178
|
# if both root and meta keys are provided in the options hash.
|
176
179
|
#
|
177
180
|
# @example Generating JSON with an extended view
|
@@ -196,7 +199,7 @@ module Blueprinter
|
|
196
199
|
# @option options [Symbol|String] :root Defaults to nil.
|
197
200
|
# Render the json/hash with a root key if provided.
|
198
201
|
# @option options [Any] :meta Defaults to nil.
|
199
|
-
# Render the json/hash with a meta attribute with provided value
|
202
|
+
# Render the json/hash with a meta attribute with provided value
|
200
203
|
# if both root and meta keys are provided in the options hash.
|
201
204
|
#
|
202
205
|
# @example Generating a hash with an extended view
|
@@ -221,7 +224,7 @@ module Blueprinter
|
|
221
224
|
# @option options [Symbol|String] :root Defaults to nil.
|
222
225
|
# Render the json/hash with a root key if provided.
|
223
226
|
# @option options [Any] :meta Defaults to nil.
|
224
|
-
# Render the json/hash with a meta attribute with provided value
|
227
|
+
# Render the json/hash with a meta attribute with provided value
|
225
228
|
# if both root and meta keys are provided in the options hash.
|
226
229
|
#
|
227
230
|
# @example Generating a hash with an extended view
|
@@ -9,7 +9,6 @@ module Blueprinter
|
|
9
9
|
|
10
10
|
def extract(field_name, object, local_options, options = {})
|
11
11
|
extraction = extractor(object, options).extract(field_name, object, local_options, options)
|
12
|
-
|
13
12
|
value = @datetime_formatter.format(extraction, options)
|
14
13
|
value.nil? ? default_value(options) : value
|
15
14
|
end
|
@@ -1,13 +1,15 @@
|
|
1
1
|
module Blueprinter
|
2
2
|
class DateTimeFormatter
|
3
|
+
InvalidDateTimeFormatterError = Class.new(BlueprinterError)
|
4
|
+
|
3
5
|
def format(value, options)
|
4
6
|
return value if value.nil?
|
5
|
-
|
7
|
+
|
6
8
|
field_format = options[:datetime_format]
|
7
9
|
if value.respond_to?(:strftime)
|
8
10
|
value = format_datetime(value, field_format)
|
9
11
|
elsif field_format
|
10
|
-
raise
|
12
|
+
raise InvalidDateTimeFormatterError, 'Cannot format invalid DateTime object'
|
11
13
|
end
|
12
14
|
value
|
13
15
|
end
|
@@ -16,7 +18,14 @@ module Blueprinter
|
|
16
18
|
|
17
19
|
def format_datetime(value, field_format)
|
18
20
|
format = field_format || Blueprinter.configuration.datetime_format
|
19
|
-
|
21
|
+
|
22
|
+
case format
|
23
|
+
when NilClass then value
|
24
|
+
when Proc then format.call(value)
|
25
|
+
when String then value.strftime(format)
|
26
|
+
else
|
27
|
+
raise InvalidDateTimeFormatterError, 'Cannot format DateTime object with invalid formatter: #{format.class}'
|
28
|
+
end
|
20
29
|
end
|
21
30
|
end
|
22
31
|
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.15.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-04-
|
12
|
+
date: 2019-04-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: factory_bot
|