Skip to content

Commit 05d1e22

Browse files
committed
added unexpose method
1 parent 471dc4c commit 05d1e22

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-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:

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)