Skip to content

Commit dce9d9a

Browse files
committed
Merge pull request #109 from jonmchan/unexpose
Add Unexpose Method
2 parents 471dc4c + f089f16 commit dce9d9a

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Metrics/AbcSize:
1212
# Offense count: 1
1313
# Configuration parameters: CountComments.
1414
Metrics/ClassLength:
15-
Max: 296
15+
Max: 300
1616

1717
# Offense count: 4
1818
Metrics/CyclomaticComplexity:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Next Release
22
============
33

44
* Your contribution here.
5+
* [#109](https://github.com/intridea/grape-entity/pull/109): Add unexpose method - [@jonmchan](https://github.com/jonmchan).
56
* [#98](https://github.com/intridea/grape-entity/pull/98): Add nested conditionals - [@zbelzer](https://github.com/zbelzer).
67
* [#91](https://github.com/intridea/grape-entity/pull/91): Fix OpenStruct serializing - [@etehtsea](https://github.com/etehtsea).
78
* [#105](https://github.com/intridea/grape-entity/pull/105): Specify which attribute is missing in which Entity - [@jhollinger](https://github.com/jhollinger).

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,31 @@ private
199199
end
200200
```
201201

202+
#### Unexpose
203+
204+
To undefine an exposed field, use the ```.unexpose``` method. Useful for modifying inherited entities.
205+
206+
```ruby
207+
class UserData < Grape::Entity
208+
expose :name
209+
expose :address1
210+
expose :address2
211+
expose :address_state
212+
expose :address_city
213+
expose :email
214+
expose :phone
215+
end
216+
217+
class MailingAddress < UserData
218+
unexpose :email
219+
unexpose :phone
220+
end
221+
```
222+
223+
224+
225+
226+
202227
#### Aliases
203228

204229
Expose under a different name with `:as`.

lib/grape_entity/entity.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ def self.expose(*args, &block)
158158
end
159159
end
160160

161+
def self.unexpose(attribute)
162+
exposures.delete(attribute)
163+
end
164+
161165
# Set options that will be applied to any exposures declared inside the block.
162166
#
163167
# @example Multi-exposure if

spec/grape_entity/entity_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,48 @@ class Parent < Person
278278
end
279279
end
280280

281+
describe '.unexpose' do
282+
it 'is able to remove exposed attributes' do
283+
subject.expose :name, :email
284+
subject.unexpose :email
285+
286+
expect(subject.exposures).to eq(name: {})
287+
end
288+
289+
context 'inherited exposures' do
290+
it 'when called from child class, only removes from the attribute from child' do
291+
subject.expose :name, :email
292+
child_class = Class.new(subject)
293+
child_class.unexpose :email
294+
295+
expect(child_class.exposures).to eq(name: {})
296+
expect(subject.exposures).to eq(name: {}, email: {})
297+
end
298+
299+
# the following 2 behaviors are testing because it is not most intuitive and could be confusing
300+
context 'when called from the parent class' do
301+
it 'remove from parent and all child classes that have not locked down their attributes with an .exposures call' do
302+
subject.expose :name, :email
303+
child_class = Class.new(subject)
304+
subject.unexpose :email
305+
306+
expect(subject.exposures).to eq(name: {})
307+
expect(child_class.exposures).to eq(name: {})
308+
end
309+
310+
it 'remove from parent and do not remove from child classes that have locked down their attributes with an .exposures call' do
311+
subject.expose :name, :email
312+
child_class = Class.new(subject)
313+
child_class.exposures
314+
subject.unexpose :email
315+
316+
expect(subject.exposures).to eq(name: {})
317+
expect(child_class.exposures).to eq(name: {}, email: {})
318+
end
319+
end
320+
end
321+
end
322+
281323
describe '.with_options' do
282324
it 'raises an error for unknown options' do
283325
block = proc do

0 commit comments

Comments
 (0)