Skip to content

Commit

Permalink
added unexpose method
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmchan committed Mar 5, 2015
1 parent 471dc4c commit 05d1e22
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Metrics/AbcSize:
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 296
Max: 300

# Offense count: 4
Metrics/CyclomaticComplexity:
Expand Down
4 changes: 4 additions & 0 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ def self.expose(*args, &block)
end
end

def self.unexpose(attribute)
exposures.delete(attribute)
end

# Set options that will be applied to any exposures declared inside the block.
#
# @example Multi-exposure if
Expand Down
42 changes: 42 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,48 @@ class Parent < Person
end
end

describe '.unexpose' do
it 'is able to remove exposed attributes' do
subject.expose :name, :email
subject.unexpose :email

expect(subject.exposures).to eq(name: {})
end

context 'inherited exposures' do
it 'when called from child class, only removes from the attribute from child' do
subject.expose :name, :email
child_class = Class.new(subject)
child_class.unexpose :email

expect(child_class.exposures).to eq(name: {})
expect(subject.exposures).to eq(name: {}, email: {})
end

# the following 2 behaviors are testing because it is not most intuitive and could be confusing
context 'when called from the parent class' do
it 'remove from parent and all child classes that have not locked down their attributes with an .exposures call' do
subject.expose :name, :email
child_class = Class.new(subject)
subject.unexpose :email

expect(subject.exposures).to eq(name: {})
expect(child_class.exposures).to eq(name: {})
end

it 'remove from parent and do not remove from child classes that have locked down their attributes with an .exposures call' do
subject.expose :name, :email
child_class = Class.new(subject)
child_class.exposures
subject.unexpose :email

expect(subject.exposures).to eq(name: {})
expect(child_class.exposures).to eq(name: {}, email: {})
end
end
end
end

describe '.with_options' do
it 'raises an error for unknown options' do
block = proc do
Expand Down

0 comments on commit 05d1e22

Please sign in to comment.