Skip to content
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### Features

* [#277](https://github.com/ruby-grape/grape-entity/pull/277): Provide grape::entity::options#dig - [@kachick](https://github.com/kachick).
* [#265](https://github.com/ruby-grape/grape-entity/pull/265): Adds ability to provide a proc to as: - [@james2m](https://github.com/james2m).
* [#264](https://github.com/ruby-grape/grape-entity/pull/264): Adds Rubocop config and todo list - [@james2m](https://github.com/james2m).
* [#255](https://github.com/ruby-grape/grape-entity/pull/255): Adds code coverage w/ coveralls - [@LeFnord](https://github.com/LeFnord).
Expand Down
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ source 'http://rubygems.org'

gemspec

group :development, :test do
gem 'rubocop', '~> 0.48.0', require: false
end

group :test do
gem 'coveralls', require: false
gem 'growl'
Expand Down
1 change: 0 additions & 1 deletion grape-entity.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Gem::Specification.new do |s|

s.add_development_dependency 'bundler'
s.add_development_dependency 'rake'
s.add_development_dependency 'rubocop', '~> 0.48'
s.add_development_dependency 'rspec', '~> 3.0'
s.add_development_dependency 'rack-test'
s.add_development_dependency 'maruku'
Expand Down
4 changes: 4 additions & 0 deletions lib/grape_entity/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def key?(key)
@opts_hash.key? key
end

def dig(*keys)
@opts_hash.dig(*keys)
end

def merge(new_opts)
if new_opts.empty?
self
Expand Down
16 changes: 15 additions & 1 deletion spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1771,17 +1771,19 @@ class UserEntity < Grape::Entity
describe Grape::Entity::Options do
module EntitySpec
class Crystalline
attr_accessor :prop1, :prop2
attr_accessor :prop1, :prop2, :prop3

def initialize
@prop1 = 'value1'
@prop2 = 'value2'
@prop3 = 'value3'
end
end

class CrystallineEntity < Grape::Entity
expose :prop1, if: ->(_, options) { options.fetch(:signal) }
expose :prop2, if: ->(_, options) { options.fetch(:beam, 'destructive') == 'destructive' }
expose :prop3, if: ->(_, options) { options.dig(:first, :second) == :nested }
end
end

Expand All @@ -1800,6 +1802,18 @@ class CrystallineEntity < Grape::Entity
expect(crystalline_entity.as_json).to eq(prop1: 'value1')
end
end

context '#dig' do
it 'without passing in a expected option hide the value' do
crystalline_entity = EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new, signal: true, first: { invalid: :nested })
expect(crystalline_entity.as_json).to eq(prop1: 'value1', prop2: 'value2')
end

it 'passing in a expected option will expose the values' do
crystalline_entity = EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new, signal: true, first: { second: :nested })
expect(crystalline_entity.as_json).to eq(prop1: 'value1', prop2: 'value2', prop3: 'value3')
end
end
end
end
end