Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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
34 changes: 0 additions & 34 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1767,39 +1767,5 @@ class UserEntity < Grape::Entity
end
end
end

describe Grape::Entity::Options do
module EntitySpec
class Crystalline
attr_accessor :prop1, :prop2

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

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

context '#fetch' do
it 'without passing in a required option raises KeyError' do
expect { EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new).as_json }.to raise_error KeyError
end

it 'passing in a required option will expose the values' do
crystalline_entity = EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new, signal: true)
expect(crystalline_entity.as_json).to eq(prop1: 'value1', prop2: 'value2')
end

it 'with an option that is not default will not expose that value' do
crystalline_entity = EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new, signal: true, beam: 'intermittent')
expect(crystalline_entity.as_json).to eq(prop1: 'value1')
end
end
end
end
end
66 changes: 66 additions & 0 deletions spec/grape_entity/options_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

require 'spec_helper'

describe Grape::Entity::Options do
module EntitySpec
class Crystalline
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' }
end
end

context '#fetch' do
it 'without passing in a required option raises KeyError' do
expect { EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new).as_json }.to raise_error KeyError
end

it 'passing in a required option will expose the values' do
crystalline_entity = EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new, signal: true)
expect(crystalline_entity.as_json).to eq(prop1: 'value1', prop2: 'value2')
end

it 'with an option that is not default will not expose that value' do
crystalline_entity = EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new, signal: true, beam: 'intermittent')
expect(crystalline_entity.as_json).to eq(prop1: 'value1')
end
end

context '#dig', skip: !{}.respond_to?(:dig) do
let(:model_class) do
Class.new do
attr_accessor :prop1

def initialize
@prop1 = 'value1'
end
end
end

let(:entity_class) do
Class.new(Grape::Entity) do
expose :prop1, if: ->(_, options) { options.dig(:first, :second) == :nested }
end
end

it 'without passing in a expected option hide the value' do
entity = entity_class.represent(model_class.new, first: { invalid: :nested })
expect(entity.as_json).to eq({})
end

it 'passing in a expected option will expose the values' do
entity = entity_class.represent(model_class.new, first: { second: :nested })
expect(entity.as_json).to eq(prop1: 'value1')
end
end
end