grape-entity 0.7.0 → 0.7.1

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: ecd89442adb3e014be540761d005b20920e8aa847691a69a6c4e166d1ed63f21
4
- data.tar.gz: c556531fdea49f03dfea0ce4d1388016736c7d3987db0957be30b27a82e21e40
3
+ metadata.gz: 31f997094eb9145e0e34541cc1083562afbc7724f68c343d9bb8f897b1af8c5f
4
+ data.tar.gz: 271397df6ac51ffdd5e6380a1e4ab47affb3fbb14d805f6d00cc6715084d0f13
5
5
  SHA512:
6
- metadata.gz: 5a48af786a0d681229aecd0765eccfafb9f28f6178dc5ade649d54b255df13e82f24f4f5243de7cdff9d45a423d101b851137ffb7920e3853529d88fbe23f67b
7
- data.tar.gz: b9b89ee602c3804470c57e6519244e59b78fa5b10470fb2a5d0f1581e821847ab8a6f66145fcf57445090d654c442d33cfc6fa94c6eaea9e36f15ce1038eb362
6
+ metadata.gz: '0916c3b71d39463de13549202659f8e2c60634e6e6951b9999d45a045307c716f7bcada5a86c100066029a8e4f7da7aea5bf296bc8659f9a871162118c196d3d'
7
+ data.tar.gz: ed8956de1f403808e504a60b3759e19a0012f9d91db9200fc63891f7d5f5bc29116084619bd47e41d8afce3ef8a98180c620c1f902b3f0dbd4eb42f37d4bb596
@@ -18,7 +18,7 @@ Metrics/AbcSize:
18
18
  # Offense count: 35
19
19
  # Configuration parameters: CountComments, ExcludedMethods.
20
20
  Metrics/BlockLength:
21
- Max: 1625
21
+ Max: 1632
22
22
 
23
23
  # Offense count: 2
24
24
  # Configuration parameters: CountComments.
@@ -8,6 +8,12 @@
8
8
 
9
9
  * Your contribution here.
10
10
 
11
+ ### 0.7.1 (2018-01-30)
12
+
13
+ #### Features
14
+
15
+ * [#292](https://github.com/ruby-grape/grape-entity/pull/297): Introduce `override` option for expose (fixes [#286](https://github.com/ruby-grape/grape-entity/issues/296)) - [@DmitryTsepelev](https://github.com/DmitryTsepelev).
16
+
11
17
  ### 0.7.0 (2018-01-25)
12
18
 
13
19
  #### Features
data/README.md CHANGED
@@ -256,6 +256,22 @@ class MailingAddress < UserData
256
256
  end
257
257
  ```
258
258
 
259
+ #### Overriding exposures
260
+
261
+ If you want to add one more exposure for the field but don't want the first one to be fired (for instance, when using inheritance), you can use the `override` flag. For instance:
262
+
263
+ ```ruby
264
+ class User < Grape::Entity
265
+ expose :name
266
+ end
267
+
268
+ class Employee < UserData
269
+ expose :name, as: :employee_name, override: true
270
+ end
271
+ ```
272
+
273
+ `User` will return something like this `{ "name" : "John" }` while `Employee` will present the same data as `{ "employee_name" : "John" }` instead of `{ "name" : "John", "employee_name" : "John" }`.
274
+
259
275
  #### Returning only the fields you want
260
276
 
261
277
  After exposing the desired attributes, you can choose which one you need when representing some object or collection by using the only: and except: options. See the example:
@@ -197,7 +197,7 @@ module Grape
197
197
 
198
198
  exposure = Exposure.new(attribute, options)
199
199
 
200
- exposure_list.delete_by(attribute) if exposure_list.select_by(attribute).all? { |exp| exp.replaceable_by?(exposure) }
200
+ exposure_list.delete_by(attribute) if exposure.override?
201
201
 
202
202
  exposure_list << exposure
203
203
 
@@ -527,7 +527,7 @@ module Grape
527
527
 
528
528
  # All supported options.
529
529
  OPTIONS = %i[
530
- rewrite as if unless using with proc documentation format_with safe attr_path if_extras unless_extras merge expose_nil
530
+ rewrite as if unless using with proc documentation format_with safe attr_path if_extras unless_extras merge expose_nil override
531
531
  ].to_set.freeze
532
532
 
533
533
  # Merges the given options with current block options.
@@ -4,7 +4,7 @@ module Grape
4
4
  class Entity
5
5
  module Exposure
6
6
  class Base
7
- attr_reader :attribute, :is_safe, :documentation, :conditions, :for_merge
7
+ attr_reader :attribute, :is_safe, :documentation, :override, :conditions, :for_merge
8
8
 
9
9
  def self.new(attribute, options, conditions, *args, &block)
10
10
  super(attribute, options, conditions).tap { |e| e.setup(*args, &block) }
@@ -19,6 +19,7 @@ module Grape
19
19
  @for_merge = options[:merge]
20
20
  @attr_path_proc = options[:attr_path]
21
21
  @documentation = options[:documentation]
22
+ @override = options[:override]
22
23
  @conditions = conditions
23
24
  end
24
25
 
@@ -116,8 +117,8 @@ module Grape
116
117
  end
117
118
  end
118
119
 
119
- def replaceable_by?(other)
120
- !nesting? && !conditional? && !other.nesting? && !other.conditional?
120
+ def override?
121
+ @override
121
122
  end
122
123
 
123
124
  protected
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GrapeEntity
4
- VERSION = '0.7.0'
4
+ VERSION = '0.7.1'
5
5
  end
@@ -477,11 +477,20 @@ describe Grape::Entity do
477
477
  expect(child_class.represent({ name: 'bar' }, serializable: true)).to eq(email: nil, name: 'foo')
478
478
  end
479
479
 
480
- it 'overrides parent class exposure' do
480
+ it 'not overrides exposure by default' do
481
481
  subject.expose :name
482
482
  child_class = Class.new(subject)
483
483
  child_class.expose :name, as: :child_name
484
484
 
485
+ expect(subject.represent({ name: 'bar' }, serializable: true)).to eq(name: 'bar')
486
+ expect(child_class.represent({ name: 'bar' }, serializable: true)).to eq(name: 'bar', child_name: 'bar')
487
+ end
488
+
489
+ it 'overrides parent class exposure when option is specified' do
490
+ subject.expose :name
491
+ child_class = Class.new(subject)
492
+ child_class.expose :name, as: :child_name, override: true
493
+
485
494
  expect(subject.represent({ name: 'bar' }, serializable: true)).to eq(name: 'bar')
486
495
  expect(child_class.represent({ name: 'bar' }, serializable: true)).to eq(child_name: 'bar')
487
496
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-entity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bleigh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-25 00:00:00.000000000 Z
11
+ date: 2018-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport