blueprinter 0.18.0 → 0.19.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 58305ea0b747a19614cf18bacb3a531de6c64e6ccf4f01abc22dc47c3a378ecc
4
- data.tar.gz: b48d6d62205280be48ec415bc5b67af3ae5a5ee7afe3581a707064fa46f5986f
3
+ metadata.gz: 580e993c5873c53b1324841946e603d64cc53549fa171467683b0d4777bb3375
4
+ data.tar.gz: 297c9ff83b8b2a726970e099034631e9f9200ff4598f896c465885a982a86e04
5
5
  SHA512:
6
- metadata.gz: f101686b41f0c201d3bf0414f7aa1529450ca79ffed19e272875c48ba617bf0a9933876510715f3a65d016af3f4ed7ebeb179a932489dfe7c074a0a3465fba3a
7
- data.tar.gz: 3b4dcc7ce851136b62cb3fe534fa38746da07c834d25b19a56927dfabe01f0be4a1e3e5ba95434fce80cc43c566244854b08eb0b6fbb7f14cb1dce33d8a2b18f
6
+ metadata.gz: b7ba1f23e5cd9f50a7c1c97269d6830ed832d4018ba29c24ee208e8105be53f0034ce52c7f851f12d2c4a876f8093202fa7c99306bd50c74f1663edca0a82cbe
7
+ data.tar.gz: a66d70cf431cdd6ada7633cf1ee067a8701a5eaef4ba2d9d9cea0344a81565603357ba0dafa548cac4db870bc131b7aca263ead7c389b4b0d15264ec4dff2315
@@ -1,3 +1,6 @@
1
+ ## 0.19.0 - 2019/07/24
2
+ * 🚀 [FEATURE] Added ability to specify transformers for Blueprinter views to further process the resulting hash before serialization. [#164](https://github.com/procore/blueprinter/pull/164). Thanks to [@amalarayfreshworks](https://github.com/amalarayfreshworks).
3
+
1
4
  ## 0.18.0 - 2019/05/29
2
5
 
3
6
  * ⚠️ [DEPRECATION] :if/:unless procs with two arguments are now deprecated. *These procs now take in three arguments (field_name, obj, options) instead of just (obj, options).*
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  [![CircleCI](https://circleci.com/gh/procore/blueprinter.svg?style=svg)](https://circleci.com/gh/procore/blueprinter)
2
2
  [![Gem Version](https://badge.fury.io/rb/blueprinter.svg)](https://badge.fury.io/rb/blueprinter)
3
- [![Gitter chat](https://badges.gitter.im/procore/blueprinter.png)](https://gitter.im/blueprinter-gem/community)
3
+ [![Gitter chat](https://badges.gitter.im/procore/blueprinter.svg)](https://gitter.im/blueprinter-gem/community)
4
4
 
5
5
  <img src="blueprinter_logo.svg" width="25%">
6
6
 
@@ -660,6 +660,53 @@ _NOTE:_ The field-level setting overrides the global config setting (for the fie
660
660
  </details>
661
661
 
662
662
 
663
+ <details>
664
+ <summary>Transform Classes</summary>
665
+
666
+ ---
667
+
668
+ Blueprinter provides the ability to specify `transform`s on views, which enable further
669
+ processing and transforming of resulting view field hashes prior to serialization.
670
+
671
+ Use `transform` to specify one transformer to be included for serialization.
672
+ A transformer is a class, extending `Blueprinter::Transformer` and implementing the `transform` method.
673
+ Whatever is returned from this `transform` method will end up being the resulting hash passed to serialization.
674
+
675
+ #### Example
676
+
677
+ Create a Transform class extending from `Blueprinter::Transformer`
678
+ ```ruby
679
+ class DynamicFieldTransformer < Blueprinter::Transformer
680
+ def transform(hash, object, options)
681
+ hash.merge!(object.dynamic_fields)
682
+ end
683
+ end
684
+ ```
685
+
686
+ ```ruby
687
+ class User
688
+ def custom_columns
689
+ self.dynamic_fields #which is an array of some columns
690
+ end
691
+
692
+ def custom_fields
693
+ custom_columns.each_with_object({}){|col,result| result[col] = self.send(col)}
694
+ end
695
+ end
696
+ ```
697
+
698
+ Then specify the transform to use for the view.
699
+ ```ruby
700
+ class UserBlueprint < Blueprinter::Base
701
+ fields :first_name, :last_name
702
+ transform DynamicTransformer
703
+ end
704
+ ```
705
+
706
+ ---
707
+ </details>
708
+
709
+
663
710
  <details>
664
711
  <summary>Sorting Fields</summary>
665
712
 
@@ -11,6 +11,7 @@ require_relative 'field'
11
11
  require_relative 'helpers/base_helpers'
12
12
  require_relative 'view'
13
13
  require_relative 'view_collection'
14
+ require_relative 'transformer'
14
15
 
15
16
  module Blueprinter
16
17
  class Base
@@ -274,6 +275,43 @@ module Blueprinter
274
275
  end
275
276
  end
276
277
 
278
+
279
+
280
+ # Specify one transformer to be included for serialization.
281
+ # Takes a class which extends Blueprinter::Transformer
282
+ #
283
+ # @param class name [Class] which implements the method transform to include for
284
+ # serialization.
285
+ #
286
+ #
287
+ # @example Specifying a DynamicFieldTransformer transformer for including dynamic fields to be serialized.
288
+ # class User
289
+ # def custom_columns
290
+ # self.dynamic_fields #which is an array of some columns
291
+ # end
292
+ # def custom_fields
293
+ # custom_columns.each_with_object({}){|col,result| result[col] = self.send(col)}
294
+ # end
295
+ # end
296
+ #
297
+ # class UserBlueprint < Blueprinter::Base
298
+ # fields :first_name, :last_name
299
+ # transform DynamicTransformer
300
+ # # other code
301
+ # end
302
+ #
303
+ # class DynamicFieldTransformer < Blueprinter::Transformer
304
+ # def transform(hash, object, options)
305
+ # hash.merge!(object.dynamic_fields)
306
+ # end
307
+ # end
308
+ #
309
+ # @return [Array<Class>] an array of transformers
310
+ def self.transform(transformer)
311
+ current_view.add_transformer(transformer)
312
+ end
313
+
314
+
277
315
  # Specify another view that should be mixed into the current view.
278
316
  #
279
317
  # @param view_name [Symbol] the view to mix into the current view.
@@ -44,10 +44,14 @@ module Blueprinter
44
44
  end
45
45
 
46
46
  def object_to_hash(object, view_name:, local_options:)
47
- view_collection.fields_for(view_name).each_with_object({}) do |field, hash|
47
+ result_hash = view_collection.fields_for(view_name).each_with_object({}) do |field, hash|
48
48
  next if field.skip?(field.name, object, local_options)
49
- hash[field.name] = field.extract(object, local_options)
49
+ hash[field.name] = field.extract(object, local_options)
50
50
  end
51
+ view_collection.transformers(view_name).each do |transformer|
52
+ transformer.transform(result_hash,object,local_options)
53
+ end
54
+ result_hash
51
55
  end
52
56
 
53
57
  def validate_root_and_meta(root, meta)
@@ -0,0 +1,12 @@
1
+ module Blueprinter
2
+ # @api private
3
+ class Transformer
4
+ def transform(result_hash,primary_obj, options={})
5
+ fail NotImplementedError, "A Transformer must implement #transform"
6
+ end
7
+
8
+ def self.transform(result_hash,primary_obj, options={})
9
+ self.new.transform(result_hash,primary_obj, options)
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module Blueprinter
2
- VERSION = '0.18.0'
2
+ VERSION = '0.19.0'
3
3
  end
@@ -1,13 +1,14 @@
1
1
  module Blueprinter
2
2
  # @api private
3
3
  class View
4
- attr_reader :excluded_field_names, :fields, :included_view_names, :name
4
+ attr_reader :excluded_field_names, :fields, :included_view_names, :name, :transformers
5
5
 
6
- def initialize(name, fields: {}, included_view_names: [], excluded_view_names: [])
6
+ def initialize(name, fields: {}, included_view_names: [], excluded_view_names: [],transformers: [])
7
7
  @name = name
8
8
  @fields = fields
9
9
  @included_view_names = included_view_names
10
10
  @excluded_field_names = excluded_view_names
11
+ @transformers = transformers
11
12
  end
12
13
 
13
14
  def inherit(view)
@@ -22,6 +23,10 @@ module Blueprinter
22
23
  view.excluded_field_names.each do |field_name|
23
24
  exclude_field(field_name)
24
25
  end
26
+
27
+ view.transformers.each do |transformer|
28
+ self.add_transformer(transformer)
29
+ end
25
30
  end
26
31
 
27
32
  def include_view(view_name)
@@ -38,6 +43,10 @@ module Blueprinter
38
43
  end
39
44
  end
40
45
 
46
+ def add_transformer(custom_transformer)
47
+ transformers << custom_transformer
48
+ end
49
+
41
50
  def <<(field)
42
51
  fields[field.name] = field
43
52
  end
@@ -28,6 +28,10 @@ module Blueprinter
28
28
  identifier_fields + sorted_fields
29
29
  end
30
30
 
31
+ def transformers(view_name)
32
+ views[view_name].transformers
33
+ end
34
+
31
35
  def [](view_name)
32
36
  @views[view_name] ||= View.new(view_name)
33
37
  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.18.0
4
+ version: 0.19.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-06-11 00:00:00.000000000 Z
12
+ date: 2019-07-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: factory_bot
@@ -178,6 +178,7 @@ files:
178
178
  - lib/blueprinter/field.rb
179
179
  - lib/blueprinter/formatters/date_time_formatter.rb
180
180
  - lib/blueprinter/helpers/base_helpers.rb
181
+ - lib/blueprinter/transformer.rb
181
182
  - lib/blueprinter/version.rb
182
183
  - lib/blueprinter/view.rb
183
184
  - lib/blueprinter/view_collection.rb