blueprinter 0.8.0 → 0.9.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
  SHA1:
3
- metadata.gz: 4f911f54c6fe83cbebf48847eee6b7b596931689
4
- data.tar.gz: afe0b665b414c0b6fa9d86e853a2c94030318bed
3
+ metadata.gz: bc5da9a0f99343970675961e3a4774db44fc587b
4
+ data.tar.gz: 3a70a6c6a544b3b71c50616ef69ddc1f81b62761
5
5
  SHA512:
6
- metadata.gz: 8e41f7b0dccca6fd0640b2468edff7606d3afe23a08de4968f88e90ec6315eb8d99a89d756da5228330d2dc9f44e6c840adebf825d853fa94828e21e0521b345
7
- data.tar.gz: 60e2e16868012f1e8956baf46ae7d9f5aba743c94fcde224d6d3d985f6ae355fca4a5512ebedf48e2226724a8ef0ee7338cfeb43c5715772b801e2b371c78a99
6
+ metadata.gz: 0cb410a384a642e7111175d2ae7252c43b37b444272c14fdea416acc59c8958bef1507bee3e13c8de1fb21fc65281f91e18b7509ba55fa512ebb871b173fdc81
7
+ data.tar.gz: 0fa13303416d908c42894e70ac94106fb6e205a37301373644c29c89aebc2cf60fbe5ba6c716002a760091c31010f55889724316522e79dc18112baf414884de
@@ -1,3 +1,9 @@
1
+ ## 0.9.0 - 2018/11/29
2
+
3
+ * [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).
4
+ * [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).
5
+ * [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).
6
+
1
7
  ## 0.8.0 - 2018/11/19
2
8
 
3
9
  * [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).
data/README.md CHANGED
@@ -100,6 +100,40 @@ Output:
100
100
  }
101
101
  ```
102
102
 
103
+ ### Exclude fields
104
+ You can specifically choose to exclude certain fields for specific views
105
+ ```ruby
106
+ class UserBlueprint < Blueprinter::Base
107
+ identifier :uuid
108
+ field :email, name: :login
109
+
110
+ view :normal do
111
+ fields :first_name, :last_name
112
+ end
113
+
114
+ view :extended do
115
+ include_view :normal
116
+ field :address
117
+ exclude :last_name
118
+ end
119
+ end
120
+ ```
121
+
122
+ Usage:
123
+ ```ruby
124
+ puts UserBlueprint.render(user, view: :extended)
125
+ ```
126
+
127
+ Output:
128
+ ```json
129
+ {
130
+ "uuid": "733f0758-8f21-4719-875f-262c3ec743af",
131
+ "address": "123 Fake St.",
132
+ "first_name": "John",
133
+ "login": "john.doe@some.fake.email.domain"
134
+ }
135
+ ```
136
+
103
137
  ### Associations
104
138
  You may include associated objects. Say for example, a user has projects:
105
139
  ```ruby
@@ -278,6 +312,42 @@ Output:
278
312
  }
279
313
  ```
280
314
 
315
+ ### render_as_hash
316
+ Same as `render`, returns a Ruby Hash.
317
+
318
+ Usage:
319
+
320
+ ```ruby
321
+ puts UserBlueprint.render_as_hash(user, company: company)
322
+ ```
323
+
324
+ Output:
325
+
326
+ ```ruby
327
+ {
328
+ uuid: "733f0758-8f21-4719-875f-262c3ec743af",
329
+ company_name: "My Company LLC"
330
+ }
331
+ ```
332
+
333
+ ### render_as_json
334
+ Same as `render`, returns a Ruby Hash JSONified. This will call JSONify all keys and values.
335
+
336
+ Usage:
337
+
338
+ ```ruby
339
+ puts UserBlueprint.render_as_json(user, company: company)
340
+ ```
341
+
342
+ Output:
343
+
344
+ ```ruby
345
+ {
346
+ "uuid" => "733f0758-8f21-4719-875f-262c3ec743af",
347
+ "company_name" => "My Company LLC"
348
+ }
349
+ ```
350
+
281
351
  ### Conditional field
282
352
 
283
353
  `field` supports `:if` and `:unless` options argument that can be used to serialize the field conditionally.
@@ -328,6 +398,34 @@ $ gem install blueprinter
328
398
 
329
399
  You should also have `require 'json'` already in your project if you are not using Rails or if you are not using Oj.
330
400
 
401
+ ## Sorting
402
+ By default the response sorts the keys by name. If you want the fields to be sorted in the order of definition, use the below configuration option.
403
+
404
+ Usage:
405
+
406
+ ```ruby
407
+ Blueprinter.configure do |config|
408
+ config.sort_fields_by = :definition
409
+ end
410
+ ```
411
+
412
+ ```ruby
413
+ class UserBlueprint < Blueprinter::Base
414
+ identifier :name
415
+ field :email
416
+ field :birthday, datetime_format: "%m/%d/%Y"
417
+ end
418
+ ```
419
+
420
+ Output:
421
+ ```json
422
+ {
423
+ "name": "John Doe",
424
+ "email": "john.doe@some.fake.email.domain",
425
+ "birthday": "03/04/1994"
426
+ }
427
+ ```
428
+
331
429
  ## OJ
332
430
 
333
431
  By default, Blueprinter will be calling `JSON.generate(object)` internally and it expects that you have `require 'json'` already in your project's code. You may use `Oj` to generate in place of `JSON` like so:
@@ -352,7 +450,7 @@ gem 'oj'
352
450
  [yajl-ruby](https://github.com/brianmario/yajl-ruby) is a fast and powerful JSON generator/parser. To use `yajl-ruby` in place of `JSON / OJ`, use:
353
451
 
354
452
  ```ruby
355
- require 'yajl' # you can skip this if OJ has already been required.
453
+ require 'yajl' # you can skip this if yajl has already been required.
356
454
 
357
455
  Blueprinter.configure do |config|
358
456
  config.generator = Yajl::Encoder # default is JSON
@@ -360,7 +458,7 @@ Blueprinter.configure do |config|
360
458
  end
361
459
  ```
362
460
 
363
- ##### 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.
461
+ 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.
364
462
 
365
463
  ## How to Document
366
464
 
@@ -175,8 +175,7 @@ module Blueprinter
175
175
  #
176
176
  # @return [String] JSON formatted String
177
177
  def self.render(object, options = {})
178
- view_name = options.delete(:view) || :default
179
- jsonify(prepare(object, view_name: view_name, local_options: options))
178
+ jsonify(prepare_for_render(object, options))
180
179
  end
181
180
 
182
181
  # Generates a hash.
@@ -196,8 +195,27 @@ module Blueprinter
196
195
  #
197
196
  # @return [Hash]
198
197
  def self.render_as_hash(object, options= {})
199
- view_name = options.delete(:view) || :default
200
- prepare(object, view_name: view_name, local_options: options)
198
+ prepare_for_render(object, options)
199
+ end
200
+
201
+ # Generates a JSONified hash.
202
+ # Takes a required object and an optional view.
203
+ #
204
+ # @param object [Object] the Object to serialize upon.
205
+ # @param options [Hash] the options hash which requires a :view. Any
206
+ # additional key value pairs will be exposed during serialization.
207
+ # @option options [Symbol] :view Defaults to :default.
208
+ # The view name that corresponds to the group of
209
+ # fields to be serialized.
210
+ #
211
+ # @example Generating a hash with an extended view
212
+ # post = Post.all
213
+ # Blueprinter::Base.render_as_json post, view: :extended
214
+ # # => [{"id" => "1", "title" => "Hello"},{"id" => "2", "title" => "My Day"}]
215
+ #
216
+ # @return [Hash]
217
+ def self.render_as_json(object, options= {})
218
+ prepare_for_render(object, options).as_json
201
219
  end
202
220
 
203
221
  # This is the magic method that converts complex objects into a simple hash
@@ -308,7 +326,12 @@ module Blueprinter
308
326
  @current_view = view_collection[:default]
309
327
  end
310
328
 
311
- private
329
+ # Begin private class methods
330
+ def self.prepare_for_render(object, options)
331
+ view_name = options.delete(:view) || :default
332
+ prepare(object, view_name: view_name, local_options: options)
333
+ end
334
+ private_class_method :prepare_for_render
312
335
 
313
336
  def self.inherited(subclass)
314
337
  subclass.send(:view_collection).inherit(view_collection)
@@ -1,10 +1,11 @@
1
1
  module Blueprinter
2
2
  class Configuration
3
- attr_accessor :generator, :method
3
+ attr_accessor :generator, :method, :sort_fields_by
4
4
 
5
5
  def initialize
6
6
  @generator = JSON
7
7
  @method = :generate
8
+ @sort_fields_by = :name_asc
8
9
  end
9
10
 
10
11
  def jsonify(blob)
@@ -1,3 +1,3 @@
1
1
  module Blueprinter
2
- VERSION = '0.8.0'
2
+ VERSION = '0.9.0'
3
3
  end
@@ -1,12 +1,13 @@
1
1
  module Blueprinter
2
2
  # @api private
3
3
  class ViewCollection
4
- attr_reader :views
4
+ attr_reader :views, :sort_by_definition
5
5
  def initialize
6
6
  @views = {
7
7
  identifier: View.new(:identifier),
8
8
  default: View.new(:default)
9
9
  }
10
+ @sort_by_definition = Blueprinter.configuration.sort_fields_by.eql?(:definition)
10
11
  end
11
12
 
12
13
  def inherit(view_collection)
@@ -20,7 +21,9 @@ module Blueprinter
20
21
  end
21
22
 
22
23
  def fields_for(view_name)
23
- identifier_fields + sortable_fields(view_name).values.sort_by(&:name)
24
+ fields = sortable_fields(view_name).values
25
+ sorted_fields = sort_by_definition ? fields : fields.sort_by(&:name)
26
+ identifier_fields + sorted_fields
24
27
  end
25
28
 
26
29
  def [](view_name)
@@ -35,10 +38,10 @@ module Blueprinter
35
38
 
36
39
  def sortable_fields(view_name)
37
40
  fields = views[:default].fields
38
- fields = fields.merge(views[view_name].fields)
41
+ fields = merge_fields(fields, views[view_name].fields)
39
42
  views[view_name].included_view_names.each do |included_view_name|
40
43
  if view_name != included_view_name
41
- fields = fields.merge(sortable_fields(included_view_name))
44
+ fields = merge_fields(fields, sortable_fields(included_view_name))
42
45
  end
43
46
  end
44
47
 
@@ -48,5 +51,13 @@ module Blueprinter
48
51
 
49
52
  fields
50
53
  end
54
+
55
+ def merge_fields(source_fields, included_fields)
56
+ if sort_by_definition
57
+ included_fields.merge(source_fields)
58
+ else
59
+ source_fields.merge(included_fields)
60
+ end
61
+ end
51
62
  end
52
63
  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.8.0
4
+ version: 0.9.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: 2018-11-20 00:00:00.000000000 Z
12
+ date: 2018-12-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: factory_bot