blueprinter 0.25.0 → 0.25.1

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: c47449bb382533a5c9dab64108c3eec2078f4f1ceba16a9cde3c46b23cb1dcc9
4
- data.tar.gz: ea6d7fb604c37ad0bceb6db63a300da7c7946bb49ef0696ed416a674fe95f01a
3
+ metadata.gz: 1ea32b799d411874c19f6fcd40b522e12ac8eca461e3ed518c2ec21d1b2a4980
4
+ data.tar.gz: 1a9f5d9ce56d46821047bb693975a310abc044ff7a30c3e3c7ca908bcd81b3c2
5
5
  SHA512:
6
- metadata.gz: 48c8207d8f6a721feb45df8c15c128a73166478d708725a561861704eec7d041d92c08b8a5032bda2bba67cab138c0988976026732340086320b723e0ecab465
7
- data.tar.gz: 1c818d65fb75800ff01ebdb15470626a2e83af578e16ef0537d67005dbb014366da34d504970fc7501e4d2fc9031601710ee4943f79fd4145ce965da06cd6e1d
6
+ metadata.gz: bf07f48a3baaea65428a36c4751a0014f2178c3b391a69f8ad1f6081737454d62caa642b726ca16a8f6b53e83c5ccb61422dc2e470754d37f4c46bb257fea228
7
+ data.tar.gz: 5602227efea583382dc8e297151b473a859071cf293908feafdb00d7f50f4753cd53ae17a37e0697a47889543c883a3f0f668b45813ebc4d1bfa23f7c5a29a04
@@ -1,3 +1,6 @@
1
+ ## 0.25.1 - 2020/8/18
2
+ * 🚀 [BUGFIX] Raise Blueprinter::BlueprinterError if Blueprint given is not of class Blueprinter::Base. Before it just raised a generic `undefined method 'prepare'`. See [#233](https://github.com/procore/blueprinter/pull/233) thanks to [@caws](https://github.com/caws)
3
+
1
4
  ## 0.25.0 - 2020/7/06
2
5
  * 🚀 [FEATURE] Enable default `Blueprinter::Transformer`s to be set in the global configuration. [#222](https://github.com/procore/blueprinter/pull/222). Thanks to [@supremebeing7](https://github.com/supremebeing7).
3
6
 
data/README.md CHANGED
@@ -370,7 +370,7 @@ class DriverBlueprint < Blueprinter::Base
370
370
 
371
371
  view :normal do
372
372
  fields :first_name, :last_name
373
- association :vehicles, blueprint: vehicle_blueprint, options: { trim: 'LX' }
373
+ association :vehicles, blueprint: VehicleBlueprint, options: { trim: 'LX' }
374
374
  end
375
375
  end
376
376
  ```
@@ -156,7 +156,7 @@ module Blueprinter
156
156
  #
157
157
  # @return [Field] A Field object
158
158
  def self.association(method, options = {}, &block)
159
- raise BlueprinterError, 'blueprint required' unless options[:blueprint]
159
+ validate_blueprint!(options[:blueprint], method)
160
160
 
161
161
  field(
162
162
  method,
@@ -214,7 +214,7 @@ module Blueprinter
214
214
  # # => [{id:1, title: Hello},{id:2, title: My Day}]
215
215
  #
216
216
  # @return [Hash]
217
- def self.render_as_hash(object, options= {})
217
+ def self.render_as_hash(object, options = {})
218
218
  prepare_for_render(object, options)
219
219
  end
220
220
 
@@ -239,7 +239,7 @@ module Blueprinter
239
239
  # # => [{"id" => "1", "title" => "Hello"},{"id" => "2", "title" => "My Day"}]
240
240
  #
241
241
  # @return [Hash]
242
- def self.render_as_json(object, options= {})
242
+ def self.render_as_json(object, options = {})
243
243
  prepare_for_render(object, options).as_json
244
244
  end
245
245
 
@@ -279,7 +279,6 @@ module Blueprinter
279
279
  end
280
280
 
281
281
 
282
-
283
282
  # Specify one transformer to be included for serialization.
284
283
  # Takes a class which extends Blueprinter::Transformer
285
284
  #
@@ -8,11 +8,12 @@ module Blueprinter
8
8
  include TypeHelpers
9
9
 
10
10
  private
11
+
11
12
  def prepare_for_render(object, options)
12
13
  view_name = options.delete(:view) || :default
13
14
  root = options.delete(:root)
14
15
  meta = options.delete(:meta)
15
- validate_root_and_meta(root, meta)
16
+ validate_root_and_meta!(root, meta)
16
17
  prepare(object, view_name: view_name, local_options: options, root: root, meta: meta)
17
18
  end
18
19
 
@@ -20,19 +21,19 @@ module Blueprinter
20
21
  if array_like?(object)
21
22
  object.map do |obj|
22
23
  object_to_hash(obj,
23
- view_name: view_name,
24
- local_options: local_options)
24
+ view_name: view_name,
25
+ local_options: local_options)
25
26
  end
26
27
  else
27
28
  object_to_hash(object,
28
- view_name: view_name,
29
- local_options: local_options)
29
+ view_name: view_name,
30
+ local_options: local_options)
30
31
  end
31
32
  end
32
33
 
33
34
  def prepend_root_and_meta(data, root, meta)
34
35
  return data unless root
35
- ret = { root => data }
36
+ ret = {root => data}
36
37
  meta ? ret.merge!(meta: meta) : ret
37
38
  end
38
39
 
@@ -43,15 +44,15 @@ module Blueprinter
43
44
  def object_to_hash(object, view_name:, local_options:)
44
45
  result_hash = view_collection.fields_for(view_name).each_with_object({}) do |field, hash|
45
46
  next if field.skip?(field.name, object, local_options)
46
- hash[field.name] = field.extract(object, local_options)
47
+ hash[field.name] = field.extract(object, local_options)
47
48
  end
48
49
  view_collection.transformers(view_name).each do |transformer|
49
- transformer.transform(result_hash,object,local_options)
50
+ transformer.transform(result_hash, object, local_options)
50
51
  end
51
52
  result_hash
52
53
  end
53
54
 
54
- def validate_root_and_meta(root, meta)
55
+ def validate_root_and_meta!(root, meta)
55
56
  case root
56
57
  when String, Symbol
57
58
  # no-op
@@ -62,6 +63,44 @@ module Blueprinter
62
63
  end
63
64
  end
64
65
 
66
+ def dynamic_blueprint?(blueprint)
67
+ blueprint.is_a?(Proc)
68
+ end
69
+
70
+ def validate_blueprint!(blueprint, method)
71
+ validate_presence_of_blueprint!(blueprint)
72
+ unless dynamic_blueprint?(blueprint)
73
+ validate_blueprint_has_ancestors!(blueprint, method)
74
+ validate_blueprint_has_blueprinter_base_ancestor!(blueprint, method)
75
+ end
76
+ end
77
+
78
+ def validate_presence_of_blueprint!(blueprint)
79
+ raise BlueprinterError, 'Blueprint required' unless blueprint
80
+ end
81
+
82
+ def validate_blueprint_has_ancestors!(blueprint, association_name)
83
+ # If the class passed as a blueprint does not respond to ancestors
84
+ # it means it, at the very least, does not have Blueprinter::Base as
85
+ # one of its ancestor classes (e.g: Hash) and thus an error should
86
+ # be raised.
87
+ unless blueprint.respond_to?(:ancestors)
88
+ raise BlueprinterError, "Blueprint provided for #{association_name} "\
89
+ 'association is not valid.'
90
+ end
91
+ end
92
+
93
+ def validate_blueprint_has_blueprinter_base_ancestor!(blueprint, association_name)
94
+ # Guard clause in case Blueprinter::Base is present in the ancestor list
95
+ # for the blueprint class provided.
96
+ return if blueprint.ancestors.include? Blueprinter::Base
97
+
98
+ # Raise error describing what's wrong.
99
+ raise BlueprinterError, "Class #{blueprint.name} does not inherit from "\
100
+ 'Blueprinter::Base and is not a valid Blueprinter '\
101
+ "for #{association_name} association."
102
+ end
103
+
65
104
  def jsonify(blob)
66
105
  Blueprinter.configuration.jsonify(blob)
67
106
  end
@@ -1,3 +1,3 @@
1
1
  module Blueprinter
2
- VERSION = '0.25.0'.freeze
2
+ VERSION = '0.25.1'.freeze
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.25.0
4
+ version: 0.25.1
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: 2020-07-06 00:00:00.000000000 Z
12
+ date: 2020-08-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: factory_bot