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 6, 2015
1 parent 471dc4c commit f089f16
Show file tree
Hide file tree
Showing 5 changed files with 73 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Next Release
============

* Your contribution here.
* [#109](https://github.com/intridea/grape-entity/pull/109): Add unexpose method - [@jonmchan](https://github.com/jonmchan).
* [#98](https://github.com/intridea/grape-entity/pull/98): Add nested conditionals - [@zbelzer](https://github.com/zbelzer).
* [#91](https://github.com/intridea/grape-entity/pull/91): Fix OpenStruct serializing - [@etehtsea](https://github.com/etehtsea).
* [#105](https://github.com/intridea/grape-entity/pull/105): Specify which attribute is missing in which Entity - [@jhollinger](https://github.com/jhollinger).
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,31 @@ private
end
```

#### Unexpose

To undefine an exposed field, use the ```.unexpose``` method. Useful for modifying inherited entities.

```ruby
class UserData < Grape::Entity
expose :name
expose :address1
expose :address2
expose :address_state
expose :address_city
expose :email
expose :phone
end

class MailingAddress < UserData
unexpose :email
unexpose :phone
end
```





#### Aliases

Expose under a different name with `:as`.
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 f089f16

Please sign in to comment.