Skip to content

Commit 87dbb7b

Browse files
authored
Merge branch 'master' into update-ruby-versions
2 parents b82098b + 0a81a4d commit 87dbb7b

File tree

4 files changed

+71
-34
lines changed

4 files changed

+71
-34
lines changed

CHANGELOG.md

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

33
#### Features
44

5+
* [#277](https://github.com/ruby-grape/grape-entity/pull/277): Provide grape::entity::options#dig - [@kachick](https://github.com/kachick).
56
* [#265](https://github.com/ruby-grape/grape-entity/pull/265): Adds ability to provide a proc to as: - [@james2m](https://github.com/james2m).
67
* [#264](https://github.com/ruby-grape/grape-entity/pull/264): Adds Rubocop config and todo list - [@james2m](https://github.com/james2m).
78
* [#255](https://github.com/ruby-grape/grape-entity/pull/255): Adds code coverage w/ coveralls - [@LeFnord](https://github.com/LeFnord).

lib/grape_entity/options.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def key?(key)
2525
@opts_hash.key? key
2626
end
2727

28+
def dig(*keys)
29+
@opts_hash.dig(*keys)
30+
end
31+
2832
def merge(new_opts)
2933
if new_opts.empty?
3034
self

spec/grape_entity/entity_spec.rb

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,39 +1767,5 @@ class UserEntity < Grape::Entity
17671767
end
17681768
end
17691769
end
1770-
1771-
describe Grape::Entity::Options do
1772-
module EntitySpec
1773-
class Crystalline
1774-
attr_accessor :prop1, :prop2
1775-
1776-
def initialize
1777-
@prop1 = 'value1'
1778-
@prop2 = 'value2'
1779-
end
1780-
end
1781-
1782-
class CrystallineEntity < Grape::Entity
1783-
expose :prop1, if: ->(_, options) { options.fetch(:signal) }
1784-
expose :prop2, if: ->(_, options) { options.fetch(:beam, 'destructive') == 'destructive' }
1785-
end
1786-
end
1787-
1788-
context '#fetch' do
1789-
it 'without passing in a required option raises KeyError' do
1790-
expect { EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new).as_json }.to raise_error KeyError
1791-
end
1792-
1793-
it 'passing in a required option will expose the values' do
1794-
crystalline_entity = EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new, signal: true)
1795-
expect(crystalline_entity.as_json).to eq(prop1: 'value1', prop2: 'value2')
1796-
end
1797-
1798-
it 'with an option that is not default will not expose that value' do
1799-
crystalline_entity = EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new, signal: true, beam: 'intermittent')
1800-
expect(crystalline_entity.as_json).to eq(prop1: 'value1')
1801-
end
1802-
end
1803-
end
18041770
end
18051771
end

spec/grape_entity/options_spec.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe Grape::Entity::Options do
6+
module EntitySpec
7+
class Crystalline
8+
attr_accessor :prop1, :prop2, :prop3
9+
10+
def initialize
11+
@prop1 = 'value1'
12+
@prop2 = 'value2'
13+
@prop3 = 'value3'
14+
end
15+
end
16+
17+
class CrystallineEntity < Grape::Entity
18+
expose :prop1, if: ->(_, options) { options.fetch(:signal) }
19+
expose :prop2, if: ->(_, options) { options.fetch(:beam, 'destructive') == 'destructive' }
20+
end
21+
end
22+
23+
context '#fetch' do
24+
it 'without passing in a required option raises KeyError' do
25+
expect { EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new).as_json }.to raise_error KeyError
26+
end
27+
28+
it 'passing in a required option will expose the values' do
29+
crystalline_entity = EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new, signal: true)
30+
expect(crystalline_entity.as_json).to eq(prop1: 'value1', prop2: 'value2')
31+
end
32+
33+
it 'with an option that is not default will not expose that value' do
34+
crystalline_entity = EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new, signal: true, beam: 'intermittent')
35+
expect(crystalline_entity.as_json).to eq(prop1: 'value1')
36+
end
37+
end
38+
39+
context '#dig', skip: !{}.respond_to?(:dig) do
40+
let(:model_class) do
41+
Class.new do
42+
attr_accessor :prop1
43+
44+
def initialize
45+
@prop1 = 'value1'
46+
end
47+
end
48+
end
49+
50+
let(:entity_class) do
51+
Class.new(Grape::Entity) do
52+
expose :prop1, if: ->(_, options) { options.dig(:first, :second) == :nested }
53+
end
54+
end
55+
56+
it 'without passing in a expected option hide the value' do
57+
entity = entity_class.represent(model_class.new, first: { invalid: :nested })
58+
expect(entity.as_json).to eq({})
59+
end
60+
61+
it 'passing in a expected option will expose the values' do
62+
entity = entity_class.represent(model_class.new, first: { second: :nested })
63+
expect(entity.as_json).to eq(prop1: 'value1')
64+
end
65+
end
66+
end

0 commit comments

Comments
 (0)