blueprinter 0.15.0 → 0.16.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: a3d1968ce7648f29d301d61dd92a016b0a6ca65a515ad7ee196a4b6958634d3d
4
- data.tar.gz: b8abfa3db3801439ff6405ec695863913081cdb0c339f983279cfad75812d867
3
+ metadata.gz: e0063edea4645668b9ed423eca8e3ebd926227084216a795f34e4d7c1c4aaf65
4
+ data.tar.gz: d0cb833cbc3cae7fd7290560e170dc21825179f2a268a81de216cc8ad2bb42d2
5
5
  SHA512:
6
- metadata.gz: 41f969a27e181dd1a6e7534223eb5419e7a5c279c97a4f2da4e775c0ec05b4cfac30aa2a102891331b29ef4b644424fefece493abf90b743cca9b9ebbaff1390
7
- data.tar.gz: 187e6c5af1f43aab7b5043cb86dc3bea9dd705397eaae0888747a429ea7a4d903a6631eb35940a43960c7070b9a8297ecf236795ddaad4ecacae09b59c1735d4
6
+ metadata.gz: 72eca578378f6c66306e48e790332321141b31c07c66d40e3faac6da513ea6e43a19c8b56a062c3b92c320903956ba7211bb97d2fd32fa18c2dd1cd2b302cfef
7
+ data.tar.gz: 2946724076faa04dd6a0aabd85c1b2e92ff5f9bba4466c3c06de419ad7c11b50c48591d336e6e5aa19a66c23e9fd791b53aa48fe9aa6c243da6b0402845e0728
@@ -1,5 +1,8 @@
1
+ ## 0.16.0 - 2019/04/3
2
+ * 🚀 [FEATURE] Add ability to exclude multiple fields inline using `excludes`. [#141](https://github.com/procore/blueprinter/pull/141). Thanks to [@pabhinaya](https://github.com/pabhinaya).
3
+
1
4
  ## 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).
5
+ * 🚀 [FEATURE] Add ability to pass in `datetime_format` field option as either a string representing the strftime 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
6
 
4
7
  ## 0.14.0 - 2019/04/01
5
8
  * 🚀 [FEATURE] Added a global `datetime_format` option. [#135](https://github.com/procore/blueprinter/pull/143). Thanks to [@ritikesh](https://github.com/ritikesh).
data/README.md CHANGED
@@ -13,7 +13,11 @@ It heavily relies on the idea of `views` which, similar to Rails views, are ways
13
13
  Docs can be found [here](http://www.rubydoc.info/gems/blueprinter).
14
14
 
15
15
  ## Usage
16
- ### Basic
16
+ <details open>
17
+ <summary>Basic</summary>
18
+
19
+ ---
20
+
17
21
  If you have an object you would like serialized, simply create a blueprint. Say, for example, you have a User record with the following attributes `[:uuid, :email, :first_name, :last_name, :password, :address]`.
18
22
 
19
23
  You may define a simple blueprint like so:
@@ -42,7 +46,14 @@ And the output would look like:
42
46
  }
43
47
  ```
44
48
 
45
- ### Collections
49
+ ---
50
+ </details>
51
+
52
+
53
+ <details>
54
+ <summary>Collections</summary>
55
+
56
+ ---
46
57
 
47
58
  You can also pass a collection object or an array to the render method.
48
59
 
@@ -69,7 +80,14 @@ This will result in JSON that looks something like this:
69
80
  ]
70
81
  ```
71
82
 
72
- ### Renaming
83
+ ---
84
+ </details>
85
+
86
+
87
+ <details>
88
+ <summary>Renaming</summary>
89
+
90
+ ---
73
91
 
74
92
  You can rename the resulting JSON keys in both fields and associations by using the `name` option.
75
93
 
@@ -93,7 +111,15 @@ This will result in JSON that looks something like this:
93
111
  }
94
112
  ```
95
113
 
96
- ### Views
114
+ ---
115
+ </details>
116
+
117
+
118
+ <details>
119
+ <summary>Views</summary>
120
+
121
+ ---
122
+
97
123
  You may define different outputs by utilizing views:
98
124
  ```ruby
99
125
  class UserBlueprint < Blueprinter::Base
@@ -128,7 +154,15 @@ Output:
128
154
  }
129
155
  ```
130
156
 
131
- ### Root
157
+ ---
158
+ </details>
159
+
160
+
161
+ <details>
162
+ <summary>Root</summary>
163
+
164
+ ---
165
+
132
166
  You can also optionally pass in a root key to wrap your resulting json in:
133
167
  ```ruby
134
168
  class UserBlueprint < Blueprinter::Base
@@ -158,7 +192,15 @@ Output:
158
192
  }
159
193
  ```
160
194
 
161
- ### Meta attributes
195
+ ---
196
+ </details>
197
+
198
+
199
+ <details>
200
+ <summary>Meta Attributes</summary>
201
+
202
+ ---
203
+
162
204
  You can additionally add meta-data to the json as well:
163
205
  ```ruby
164
206
  class UserBlueprint < Blueprinter::Base
@@ -199,7 +241,15 @@ Output:
199
241
  ```
200
242
  _NOTE:_ For meta attributes, a [root](#root) is mandatory.
201
243
 
202
- ### Exclude fields
244
+ ---
245
+ </details>
246
+
247
+
248
+ <details>
249
+ <summary>Exclude Fields</summary>
250
+
251
+ ---
252
+
203
253
  You can specifically choose to exclude certain fields for specific views
204
254
  ```ruby
205
255
  class UserBlueprint < Blueprinter::Base
@@ -233,7 +283,34 @@ Output:
233
283
  }
234
284
  ```
235
285
 
236
- ### Associations
286
+ Use `excludes` to exclude multiple fields at once inline.
287
+
288
+ ```ruby
289
+ class UserBlueprint < Blueprinter::Base
290
+ identifier :uuid
291
+ field :email, name: :login
292
+
293
+ view :normal do
294
+ fields :age, :first_name, :last_name,
295
+ end
296
+
297
+ view :extended do
298
+ include_view :normal
299
+ field :address
300
+ excludes :age, :last_name
301
+ end
302
+ end
303
+ ```
304
+
305
+ ---
306
+ </details>
307
+
308
+
309
+ <details>
310
+ <summary>Associations</summary>
311
+
312
+ ---
313
+
237
314
  You may include associated objects. Say for example, a user has projects:
238
315
  ```ruby
239
316
  class ProjectBlueprint < Blueprinter::Base
@@ -277,7 +354,15 @@ Output:
277
354
  }
278
355
  ```
279
356
 
280
- ### Default Association/Field Option
357
+ ---
358
+ </details>
359
+
360
+
361
+ <details>
362
+ <summary>Default Association/Field Option</summary>
363
+
364
+ ---
365
+
281
366
  By default, an association or field that evaluates to `nil` is serialized as `nil`. A default serialized value can be specified as an option on the association or field for cases when the association/field could potentially evaluate to `nil`. You can also specify a global `field_default` or `association_default` in the Blueprinter config which will be used for all fields/associations that evaluate to nil.
282
367
 
283
368
  #### Global Config Setting
@@ -300,7 +385,15 @@ class UserBlueprint < Blueprinter::Base
300
385
  end
301
386
  ```
302
387
 
303
- ### Supporting Dynamic Blueprints for associations
388
+ ---
389
+ </details>
390
+
391
+
392
+ <details>
393
+ <summary>Supporting Dynamic Blueprints For Associations</summary>
394
+
395
+ ---
396
+
304
397
  When defining an association, we can dynamically evaluate the blueprint. This comes in handy when adding polymorphic associations, by allowing reuse of existing blueprints.
305
398
  ```ruby
306
399
  class Task < ActiveRecord::Base
@@ -326,7 +419,14 @@ end
326
419
  ```
327
420
  _NOTE:_ `taskable.blueprint` should return a valid Blueprint class. Currently, `has_many` is not supported because of the very nature of polymorphic associations.
328
421
 
329
- ### Defining a field directly in the Blueprint
422
+ ---
423
+ </details>
424
+
425
+
426
+ <details>
427
+ <summary>Defining A Field Directly In The Blueprint</summary>
428
+
429
+ ---
330
430
 
331
431
  You can define a field directly in the Blueprint by passing it a block. This is especially useful if the object does not already have such an attribute or method defined, and you want to define it specifically for use with the Blueprint. This is done by passing `field` a block. The block also yields the object and any options that were passed from `render`. For example:
332
432
 
@@ -354,7 +454,14 @@ Output:
354
454
  }
355
455
  ```
356
456
 
357
- #### Defining an identifier directly in the Blueprint
457
+ ---
458
+ </details>
459
+
460
+
461
+ <details>
462
+ <summary>Defining An Identifier Directly In The Blueprint</summary>
463
+
464
+ ---
358
465
 
359
466
  You can also pass a block to an identifier:
360
467
 
@@ -380,7 +487,14 @@ Output:
380
487
  }
381
488
  ```
382
489
 
383
- #### Defining an association directly in the Blueprint
490
+ ---
491
+ </details>
492
+
493
+
494
+ <details>
495
+ <summary>Defining An Association Directly In The Blueprint</summary>
496
+
497
+ ---
384
498
 
385
499
  You can also pass a block to an association:
386
500
 
@@ -418,7 +532,14 @@ Output:
418
532
  }
419
533
  ```
420
534
 
421
- ### Passing additional properties to `render`
535
+ ---
536
+ </details>
537
+
538
+
539
+ <details>
540
+ <summary>Passing Additional Properties To #render</summary>
541
+
542
+ ---
422
543
 
423
544
  `render` takes an options hash which you can pass additional properties, allowing you to utilize those additional properties in the `field` block. For example:
424
545
 
@@ -446,43 +567,14 @@ Output:
446
567
  }
447
568
  ```
448
569
 
449
- ### render_as_hash
450
- Same as `render`, returns a Ruby Hash.
451
-
452
- Usage:
453
-
454
- ```ruby
455
- puts UserBlueprint.render_as_hash(user, company: company)
456
- ```
457
-
458
- Output:
459
-
460
- ```ruby
461
- {
462
- uuid: "733f0758-8f21-4719-875f-262c3ec743af",
463
- company_name: "My Company LLC"
464
- }
465
- ```
466
-
467
- ### render_as_json
468
- Same as `render`, returns a Ruby Hash JSONified. This will call JSONify all keys and values.
469
-
470
- Usage:
471
-
472
- ```ruby
473
- puts UserBlueprint.render_as_json(user, company: company)
474
- ```
570
+ ---
571
+ </details>
475
572
 
476
- Output:
477
573
 
478
- ```ruby
479
- {
480
- "uuid" => "733f0758-8f21-4719-875f-262c3ec743af",
481
- "company_name" => "My Company LLC"
482
- }
483
- ```
574
+ <details>
575
+ <summary>Conditional Fields</summary>
484
576
 
485
- ### Conditional fields
577
+ ---
486
578
 
487
579
  Both the `field` and the global Blueprinter Configuration supports `:if` and `:unless` options that can be used to serialize fields conditionally.
488
580
 
@@ -505,16 +597,24 @@ end
505
597
 
506
598
  _NOTE:_ The field-level setting overrides the global config setting (for the field) if both are set.
507
599
 
508
- ### Custom formatting for dates and times
600
+ ---
601
+ </details>
602
+
603
+
604
+ <details>
605
+ <summary>Custom Formatting for Dates and Times</summary>
606
+
607
+ ---
608
+
509
609
  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,
610
+ This global or field-level option can be either a string representing the associated `strftime` format,
511
611
  or a Proc which receives the original Date/DateTime object and returns the formatted value.
512
612
  When using a Proc, it is the Proc's responsibility to handle any errors in formatting.
513
613
 
514
614
 
515
615
  #### Global Config Setting
516
616
  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`.
617
+ invoked and used to format all fields that respond to `strftime`.
518
618
  ```ruby
519
619
  Blueprinter.configure do |config|
520
620
  config.datetime_format = ->(datetime) { datetime.nil? ? datetime : datetime.strftime("%s").to_i }
@@ -556,26 +656,15 @@ Output:
556
656
 
557
657
  _NOTE:_ The field-level setting overrides the global config setting (for the field) if both are set.
558
658
 
559
- ## Installation
560
- Add this line to your application's Gemfile:
561
-
562
- ```ruby
563
- gem 'blueprinter'
564
- ```
659
+ ---
660
+ </details>
565
661
 
566
- And then execute:
567
- ```bash
568
- $ bundle
569
- ```
570
662
 
571
- Or install it yourself as:
572
- ```bash
573
- $ gem install blueprinter
574
- ```
663
+ <details>
664
+ <summary>Sorting Fields</summary>
575
665
 
576
- You should also have `require 'json'` already in your project if you are not using Rails or if you are not using Oj.
666
+ ---
577
667
 
578
- ## Sorting
579
668
  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.
580
669
 
581
670
  Usage:
@@ -603,6 +692,81 @@ Output:
603
692
  }
604
693
  ```
605
694
 
695
+ ---
696
+ </details>
697
+
698
+
699
+ <details>
700
+ <summary>render_as_hash</summary>
701
+
702
+ ---
703
+
704
+ Same as `render`, returns a Ruby Hash.
705
+
706
+ Usage:
707
+
708
+ ```ruby
709
+ puts UserBlueprint.render_as_hash(user, company: company)
710
+ ```
711
+
712
+ Output:
713
+
714
+ ```ruby
715
+ {
716
+ uuid: "733f0758-8f21-4719-875f-262c3ec743af",
717
+ company_name: "My Company LLC"
718
+ }
719
+ ```
720
+
721
+ ---
722
+ </details>
723
+
724
+
725
+ <details>
726
+ <summary>render_as_json</summary>
727
+
728
+ ---
729
+
730
+ Same as `render`, returns a Ruby Hash JSONified. This will call JSONify all keys and values.
731
+
732
+ Usage:
733
+
734
+ ```ruby
735
+ puts UserBlueprint.render_as_json(user, company: company)
736
+ ```
737
+
738
+ Output:
739
+
740
+ ```ruby
741
+ {
742
+ "uuid" => "733f0758-8f21-4719-875f-262c3ec743af",
743
+ "company_name" => "My Company LLC"
744
+ }
745
+ ```
746
+
747
+ ---
748
+ </details>
749
+
750
+
751
+ ## Installation
752
+ Add this line to your application's Gemfile:
753
+
754
+ ```ruby
755
+ gem 'blueprinter'
756
+ ```
757
+
758
+ And then execute:
759
+ ```bash
760
+ $ bundle
761
+ ```
762
+
763
+ Or install it yourself as:
764
+ ```bash
765
+ $ gem install blueprinter
766
+ ```
767
+
768
+ You should also have `require 'json'` already in your project if you are not using Rails or if you are not using Oj.
769
+
606
770
  ## OJ
607
771
 
608
772
  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:
@@ -637,14 +801,6 @@ end
637
801
 
638
802
  _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.
639
803
 
640
- ## How to Document
641
-
642
- We use [Yard](https://yardoc.org/) for documentation. Here are the following
643
- documentation rules:
644
-
645
- - Document all public methods we expect to be utilized by the end developers.
646
- - Methods that are not set to private due to ruby visibility rule limitations should be marked with `@api private`.
647
-
648
804
  ## Contributing
649
805
  Feel free to browse the issues, converse, and make pull requests. If you need help, first please see if there is already an issue for your problem. Otherwise, go ahead and make a new issue.
650
806
 
@@ -657,6 +813,14 @@ We use Yard for documentation. Here are the following documentation rules:
657
813
  - Document all public methods we expect to be utilized by the end developers.
658
814
  - Methods that are not set to private due to ruby visibility rule limitations should be marked with `@api private`.
659
815
 
816
+ ## How to Document
817
+
818
+ We use [Yard](https://yardoc.org/) for documentation. Here are the following
819
+ documentation rules:
820
+
821
+ - Document all public methods we expect to be utilized by the end developers.
822
+ - Methods that are not set to private due to ruby visibility rule limitations should be marked with `@api private`.
823
+
660
824
  ### Releasing a New Version
661
825
  To release a new version, change the version number in `version.rb`, and update the `CHANGELOG.md`. Finally, maintainers need to run `bundle exec rake release`, which will automatically create a git tag for the version, push git commits and tags to Github, and push the `.gem` file to rubygems.org.
662
826
 
@@ -313,6 +313,29 @@ module Blueprinter
313
313
  def self.exclude(field_name)
314
314
  current_view.exclude_field(field_name)
315
315
  end
316
+
317
+ # When mixing multiple views under a single view, some fields may required to be excluded from
318
+ # current view
319
+ #
320
+ # @param [Array<Symbol>] the fields to exclude from the current view.
321
+ #
322
+ # @example Excluding mutiple fields from being included into the current view.
323
+ # view :normal do
324
+ # fields :name,:address,:position,
325
+ # :company, :contact
326
+ # end
327
+ # view :special do
328
+ # include_view :normal
329
+ # fields :birthday,:joining_anniversary
330
+ # excludes :position,:address
331
+ # end
332
+ # => [:name, :company, :contact, :birthday, :joining_anniversary]
333
+ #
334
+ # @return [Array<Symbol>] an array of field names
335
+
336
+ def self.excludes(*field_names)
337
+ current_view.exclude_fields(field_names)
338
+ end
316
339
 
317
340
  # Specify a view and the fields it should have.
318
341
  # It accepts a view name and a block. The block should specify the fields.
@@ -1,3 +1,3 @@
1
1
  module Blueprinter
2
- VERSION = '0.15.0'
2
+ VERSION = '0.16.0'
3
3
  end
@@ -31,6 +31,12 @@ module Blueprinter
31
31
  def exclude_field(field_name)
32
32
  excluded_field_names << field_name
33
33
  end
34
+
35
+ def exclude_fields(field_names)
36
+ field_names.each do |field_name|
37
+ excluded_field_names << field_name
38
+ end
39
+ end
34
40
 
35
41
  def <<(field)
36
42
  fields[field.name] = field
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.15.0
4
+ version: 0.16.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-03 00:00:00.000000000 Z
12
+ date: 2019-04-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: factory_bot