blueprinter 0.13.2 → 0.14.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: d690704c8da95e946025db00e856ad92d4c88366b5d3a86540e1a2f1f12561d4
4
- data.tar.gz: 5e614e3a81094e274517427b5657a95aa8f35e8a479ffd0ef0827ac6558dc90b
3
+ metadata.gz: 1df67b25b05137cc97ddd5e8c0db7430086d3ea9dddd7ab2164944dba191fddc
4
+ data.tar.gz: d1e9a05b61be95ef8a4d627fb6908db38975d6f321cf325a13bb93a7402a03b8
5
5
  SHA512:
6
- metadata.gz: cba3479c4473c7ec3c28b3a03470ad9524ec949f0107bf093abaa2ac79eae16712cf9df9a322fac2f6c333d20a6ed1e3bd4c748b4460f4c7b3f0fc8d11abaf1f
7
- data.tar.gz: 7d1161cac2b2b090545fb6658d3c049b97cbe2fe58a0024890789315565586b38e5d65b4e23b53b17f99ee492529a7aa266d7633848a0c21ab165ec364056e22
6
+ metadata.gz: 4f2c771da50005f7a8d886bf1318f5d00069b76d1c96b78dc3ef14162272278ecddec09e6735d73a8946be7c8229ae2bc96f158a1978e53b1f99d1763b04509b
7
+ data.tar.gz: ed2a05555940425a7847028f85084f0a4fe3a2c063ea480d9e179931f792900ababe0f0d0230050441da87c9bcb3137b1151f9c393838123e80365873ff86dc8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.14.0 - 2019/04/01
2
+ * 🚀 [FEATURE] Added a global `datetime_format` option. [#135](https://github.com/procore/blueprinter/pull/143). Thanks to [@ritikesh](https://github.com/ritikesh).
3
+
1
4
  ## 0.13.2 - 2019/03/14
2
5
  * 🐛 [BUGFIX] Replacing use of rails-specific method `Hash::except` so that Blueprinter continues to work in non-Rails environments. [#140](https://github.com/procore/blueprinter/pull/140). Thanks to [@checkbutton](https://github.com/checkbutton).
3
6
 
@@ -6,6 +6,7 @@ require_relative 'extractors/auto_extractor'
6
6
  require_relative 'extractors/block_extractor'
7
7
  require_relative 'extractors/hash_extractor'
8
8
  require_relative 'extractors/public_send_extractor'
9
+ require_relative 'formatters/date_time_formatter'
9
10
  require_relative 'field'
10
11
  require_relative 'helpers/base_helpers'
11
12
  require_relative 'view'
@@ -18,7 +19,7 @@ module Blueprinter
18
19
  # Specify a field or method name used as an identifier. Usually, this is
19
20
  # something like :id
20
21
  #
21
- # Note: identifiers are always rendered and considerered their own view,
22
+ # Note: identifiers are always rendered and considered their own view,
22
23
  # similar to the :default view.
23
24
  #
24
25
  # @param method [Symbol] the method or field used as an identifier that you
@@ -1,11 +1,12 @@
1
1
  module Blueprinter
2
2
  class Configuration
3
- attr_accessor :association_default, :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
4
4
 
5
5
  VALID_CALLABLES = %i(if unless).freeze
6
6
 
7
7
  def initialize
8
8
  @association_default = nil
9
+ @datetime_format = nil
9
10
  @field_default = nil
10
11
  @generator = JSON
11
12
  @if = nil
@@ -4,11 +4,13 @@ module Blueprinter
4
4
  @hash_extractor = HashExtractor.new
5
5
  @public_send_extractor = PublicSendExtractor.new
6
6
  @block_extractor = BlockExtractor.new
7
+ @datetime_formatter = DateTimeFormatter.new
7
8
  end
8
9
 
9
10
  def extract(field_name, object, local_options, options = {})
10
11
  extraction = extractor(object, options).extract(field_name, object, local_options, options)
11
- value = options.key?(:datetime_format) ? format_datetime(extraction, options[:datetime_format]) : extraction
12
+
13
+ value = @datetime_formatter.format(extraction, options)
12
14
  value.nil? ? default_value(options) : value
13
15
  end
14
16
 
@@ -27,12 +29,5 @@ module Blueprinter
27
29
  @public_send_extractor
28
30
  end
29
31
  end
30
-
31
- def format_datetime(datetime, format)
32
- return nil if datetime.nil?
33
- datetime.strftime(format)
34
- rescue NoMethodError
35
- raise BlueprinterError, 'Cannot format invalid DateTime object'
36
- end
37
32
  end
38
33
  end
@@ -0,0 +1,22 @@
1
+ module Blueprinter
2
+ class DateTimeFormatter
3
+ def format(value, options)
4
+ return value if value.nil?
5
+
6
+ field_format = options[:datetime_format]
7
+ if value.respond_to?(:strftime)
8
+ value = format_datetime(value, field_format)
9
+ elsif field_format
10
+ raise BlueprinterError, 'Cannot format invalid DateTime object'
11
+ end
12
+ value
13
+ end
14
+
15
+ private
16
+
17
+ def format_datetime(value, field_format)
18
+ format = field_format || Blueprinter.configuration.datetime_format
19
+ format.nil? ? value : value.strftime(format)
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module Blueprinter
2
- VERSION = '0.13.2'
2
+ VERSION = '0.14.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.13.2
4
+ version: 0.14.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-03-14 00:00:00.000000000 Z
12
+ date: 2019-04-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: factory_bot
@@ -176,6 +176,7 @@ files:
176
176
  - lib/blueprinter/extractors/hash_extractor.rb
177
177
  - lib/blueprinter/extractors/public_send_extractor.rb
178
178
  - lib/blueprinter/field.rb
179
+ - lib/blueprinter/formatters/date_time_formatter.rb
179
180
  - lib/blueprinter/helpers/base_helpers.rb
180
181
  - lib/blueprinter/version.rb
181
182
  - lib/blueprinter/view.rb