spec/grape_entity/entity_spec.rb in grape-entity-0.4.2 vs spec/grape_entity/entity_spec.rb in grape-entity-0.4.3
- old
+ new
@@ -44,30 +44,30 @@
module EntitySpec
class SomeObject1
attr_accessor :prop1
def initialize
- @prop1 = "value1"
+ @prop1 = 'value1'
end
end
class BogusEntity < Grape::Entity
expose :prop1
end
end
subject.expose(:bogus, using: EntitySpec::BogusEntity) do |entity|
- entity.prop1 = "MODIFIED 2"
+ entity.prop1 = 'MODIFIED 2'
entity
end
object = EntitySpec::SomeObject1.new
value = subject.represent(object).send(:value_for, :bogus)
value.should be_instance_of EntitySpec::BogusEntity
prop1 = value.send(:value_for, :prop1)
- prop1.should == "MODIFIED 2"
+ prop1.should == 'MODIFIED 2'
end
context 'with parameters passed to the block' do
it 'sets the :proc option in the exposure options' do
block = lambda { |_| true }
@@ -98,41 +98,41 @@
}
end
it 'represents the exposure as a hash of its nested exposures' do
subject.expose :awesome do
- subject.expose(:nested) { |_| "value" }
- subject.expose(:another_nested) { |_| "value" }
+ subject.expose(:nested) { |_| 'value' }
+ subject.expose(:another_nested) { |_| 'value' }
end
subject.represent({}).send(:value_for, :awesome).should == {
- nested: "value",
- another_nested: "value"
+ nested: 'value',
+ another_nested: 'value'
}
end
it 'does not represent attributes, declared inside nested exposure, outside of it' do
subject.expose :awesome do
- subject.expose(:nested) { |_| "value" }
- subject.expose(:another_nested) { |_| "value" }
+ subject.expose(:nested) { |_| 'value' }
+ subject.expose(:another_nested) { |_| 'value' }
subject.expose :second_level_nested do
- subject.expose(:deeply_exposed_attr) { |_| "value" }
+ subject.expose(:deeply_exposed_attr) { |_| 'value' }
end
end
subject.represent({}).serializable_hash.should == {
awesome: {
- nested: "value",
- another_nested: "value",
+ nested: 'value',
+ another_nested: 'value',
second_level_nested: {
- deeply_exposed_attr: "value"
+ deeply_exposed_attr: 'value'
}
}
}
end
- it "complex nested attributes" do
+ it 'complex nested attributes' do
class ClassRoom < Grape::Entity
expose(:parents, using: 'Parent') { |_| [{}, {}] }
end
class Person < Grape::Entity
@@ -155,29 +155,29 @@
ClassRoom.represent({}).serializable_hash.should == {
parents: [
{
user: { in_first: 'value' },
children: [
- { user: { in_first: 'value', user_id: "value", display_id: "value" } },
- { user: { in_first: 'value', user_id: "value", display_id: "value" } }
+ { user: { in_first: 'value', user_id: 'value', display_id: 'value' } },
+ { user: { in_first: 'value', user_id: 'value', display_id: 'value' } }
]
},
{
user: { in_first: 'value' },
children: [
- { user: { in_first: 'value', user_id: "value", display_id: "value" } },
- { user: { in_first: 'value', user_id: "value", display_id: "value" } }
+ { user: { in_first: 'value', user_id: 'value', display_id: 'value' } },
+ { user: { in_first: 'value', user_id: 'value', display_id: 'value' } }
]
}
]
}
end
it 'is safe if its nested exposures are safe' do
subject.with_options safe: true do
subject.expose :awesome do
- subject.expose(:nested) { |_| "value" }
+ subject.expose(:nested) { |_| 'value' }
end
subject.expose :not_awesome do
subject.expose :nested
end
end
@@ -250,16 +250,16 @@
end
it 'formats an exposure with a :format_with lambda that returns a value from the entity instance' do
object = Hash.new
- subject.expose(:size, format_with: lambda { |value| self.object.class.to_s })
+ subject.expose(:size, format_with: lambda { |_value| self.object.class.to_s })
subject.represent(object).send(:value_for, :size).should == object.class.to_s
end
it 'formats an exposure with a :format_with symbol that returns a value from the entity instance' do
- subject.format_with :size_formatter do |date|
+ subject.format_with :size_formatter do |_date|
self.object.class.to_s
end
object = Hash.new
@@ -310,12 +310,12 @@
end
subject.exposures[:awesome_thing].should == { as: :extra_smooth }
end
- it "merges nested :if option" do
- match_proc = lambda { |obj, opts| true }
+ it 'merges nested :if option' do
+ match_proc = lambda { |_obj, _opts| true }
subject.class_eval do
# Symbol
with_options(if: :awesome) do
# Hash
@@ -336,11 +336,11 @@
if_extras: [:awesome, match_proc]
}
end
it 'merges nested :unless option' do
- match_proc = lambda { |obj, opts| true }
+ match_proc = lambda { |_, _| true }
subject.class_eval do
# Symbol
with_options(unless: :awesome) do
# Hash
@@ -380,14 +380,14 @@
end
subject.exposures[:awesome_thing].should == { using: 'SomethingElse' }
end
it 'overrides nested :proc option' do
- match_proc = lambda { |obj, opts| 'more awesomer' }
+ match_proc = lambda { |_obj, _opts| 'more awesomer' }
subject.class_eval do
- with_options(proc: lambda { |obj, opts| 'awesome' }) do
+ with_options(proc: lambda { |_obj, _opts| 'awesome' }) do
expose :awesome_thing, proc: match_proc
end
end
subject.exposures[:awesome_thing].should == { proc: match_proc }
@@ -420,11 +420,11 @@
representation.reject { |r| r.kind_of?(subject) }.should be_empty
end
it 'adds the collection: true option if called with a collection' do
representation = subject.represent(4.times.map { Object.new })
- representation.each { |r| r.options[:collection].should be_true }
+ representation.each { |r| r.options[:collection].should be true }
end
it 'returns a serialized hash of a single object if serializable: true' do
subject.expose(:awesome) { |_| true }
representation = subject.represent(Object.new, serializable: true)
@@ -555,12 +555,12 @@
name: 'Bob Bobson',
email: 'bob@example.com',
birthday: Time.gm(2012, 2, 27),
fantasies: ['Unicorns', 'Double Rainbows', 'Nessy'],
friends: [
- double(name: "Friend 1", email: 'friend1@example.com', fantasies: [], birthday: Time.gm(2012, 2, 27), friends: []),
- double(name: "Friend 2", email: 'friend2@example.com', fantasies: [], birthday: Time.gm(2012, 2, 27), friends: [])
+ double(name: 'Friend 1', email: 'friend1@example.com', fantasies: [], birthday: Time.gm(2012, 2, 27), friends: []),
+ double(name: 'Friend 2', email: 'friend2@example.com', fantasies: [], birthday: Time.gm(2012, 2, 27), friends: [])
]
}
}
subject { fresh_class.new(model) }
@@ -573,11 +573,11 @@
it 'does not blow up when the model is nil' do
fresh_class.expose :name
expect { fresh_class.new(nil).serializable_hash }.not_to raise_error
end
- context "with safe option" do
+ context 'with safe option' do
it 'does not throw an exception when an attribute is not found on the object' do
fresh_class.expose :name, :nonexistent_attribute, safe: true
expect { fresh_class.new(model).serializable_hash }.not_to raise_error
end
@@ -600,28 +600,28 @@
res.should_not have_key :nonexistent_attribute
res.should_not have_key :nonexistent_attribute2
end
end
- context "without safe option" do
+ context 'without safe option' do
it 'throws an exception when an attribute is not found on the object' do
fresh_class.expose :name, :nonexistent_attribute
expect { fresh_class.new(model).serializable_hash }.to raise_error
end
it "exposes attributes that don't exist on the object only when they are generated by a block" do
- fresh_class.expose :nonexistent_attribute do |model, _|
- "well, I do exist after all"
+ fresh_class.expose :nonexistent_attribute do |_model, _opts|
+ 'well, I do exist after all'
end
res = fresh_class.new(model).serializable_hash
res.should have_key :nonexistent_attribute
end
- it "does not expose attributes that are generated by a block but have not passed criteria" do
- fresh_class.expose :nonexistent_attribute, proc: lambda { |model, _|
- "I exist, but it is not yet my time to shine"
- }, if: lambda { |model, _| false }
+ it 'does not expose attributes that are generated by a block but have not passed criteria' do
+ fresh_class.expose :nonexistent_attribute, proc: lambda { |_model, _opts|
+ 'I exist, but it is not yet my time to shine'
+ }, if: lambda { |_model, _opts| false }
res = fresh_class.new(model).serializable_hash
res.should_not have_key :nonexistent_attribute
end
end
@@ -629,56 +629,56 @@
module EntitySpec
class TestEntity < Grape::Entity
end
end
- fresh_class.expose :nonexistent_attribute, using: EntitySpec::TestEntity do |model, _|
- "well, I do exist after all"
+ fresh_class.expose :nonexistent_attribute, using: EntitySpec::TestEntity do |_model, _opts|
+ 'well, I do exist after all'
end
res = fresh_class.new(model).serializable_hash
res.should have_key :nonexistent_attribute
end
- it "does not expose attributes that are generated by a block but have not passed criteria" do
- fresh_class.expose :nonexistent_attribute, proc: lambda { |model, _|
- "I exist, but it is not yet my time to shine"
- }, if: lambda { |model, _| false }
+ it 'does not expose attributes that are generated by a block but have not passed criteria' do
+ fresh_class.expose :nonexistent_attribute, proc: lambda { |_, _|
+ 'I exist, but it is not yet my time to shine'
+ }, if: lambda { |_, _| false }
res = fresh_class.new(model).serializable_hash
res.should_not have_key :nonexistent_attribute
end
context '#serializable_hash' do
module EntitySpec
class EmbeddedExample
- def serializable_hash(opts = {})
+ def serializable_hash(_opts = {})
{ abc: 'def' }
end
end
class EmbeddedExampleWithHash
def name
- "abc"
+ 'abc'
end
def embedded
{ a: nil, b: EmbeddedExample.new }
end
end
class EmbeddedExampleWithMany
def name
- "abc"
+ 'abc'
end
def embedded
[EmbeddedExample.new, EmbeddedExample.new]
end
end
class EmbeddedExampleWithOne
def name
- "abc"
+ 'abc'
end
def embedded
EmbeddedExample.new
end
@@ -686,23 +686,23 @@
end
it 'serializes embedded objects which respond to #serializable_hash' do
fresh_class.expose :name, :embedded
presenter = fresh_class.new(EntitySpec::EmbeddedExampleWithOne.new)
- presenter.serializable_hash.should == { name: "abc", embedded: { abc: "def" } }
+ presenter.serializable_hash.should == { name: 'abc', embedded: { abc: 'def' } }
end
it 'serializes embedded arrays of objects which respond to #serializable_hash' do
fresh_class.expose :name, :embedded
presenter = fresh_class.new(EntitySpec::EmbeddedExampleWithMany.new)
- presenter.serializable_hash.should == { name: "abc", embedded: [{ abc: "def" }, { abc: "def" }] }
+ presenter.serializable_hash.should == { name: 'abc', embedded: [{ abc: 'def' }, { abc: 'def' }] }
end
it 'serializes embedded hashes of objects which respond to #serializable_hash' do
fresh_class.expose :name, :embedded
presenter = fresh_class.new(EntitySpec::EmbeddedExampleWithHash.new)
- presenter.serializable_hash.should == { name: "abc", embedded: { a: nil, b: { abc: "def" } } }
+ presenter.serializable_hash.should == { name: 'abc', embedded: { a: nil, b: { abc: 'def' } } }
end
end
end
describe '#value_for' do
@@ -753,20 +753,20 @@
rep.reject { |r| r.is_a?(EntitySpec::FriendEntity) }.should be_empty
rep.first.serializable_hash[:name].should == 'Friend 1'
rep.last.serializable_hash[:name].should == 'Friend 2'
end
- it "passes through the proc which returns an array of objects with custom options(:using)" do
+ it 'passes through the proc which returns an array of objects with custom options(:using)' do
module EntitySpec
class FriendEntity < Grape::Entity
root 'friends', 'friend'
expose :name, :email
end
end
fresh_class.class_eval do
- expose :custom_friends, using: EntitySpec::FriendEntity do |user, options|
+ expose :custom_friends, using: EntitySpec::FriendEntity do |user, _opts|
user.friends
end
end
rep = subject.send(:value_for, :custom_friends)
@@ -774,40 +774,39 @@
rep.reject { |r| r.is_a?(EntitySpec::FriendEntity) }.should be_empty
rep.first.serializable_hash.should == { name: 'Friend 1', email: 'friend1@example.com' }
rep.last.serializable_hash.should == { name: 'Friend 2', email: 'friend2@example.com' }
end
- it "passes through the proc which returns single object with custom options(:using)" do
+ it 'passes through the proc which returns single object with custom options(:using)' do
module EntitySpec
class FriendEntity < Grape::Entity
root 'friends', 'friend'
expose :name, :email
end
end
fresh_class.class_eval do
- expose :first_friend, using: EntitySpec::FriendEntity do |user, options|
+ expose :first_friend, using: EntitySpec::FriendEntity do |user, _opts|
user.friends.first
end
end
rep = subject.send(:value_for, :first_friend)
rep.should be_kind_of EntitySpec::FriendEntity
rep.serializable_hash.should == { name: 'Friend 1', email: 'friend1@example.com' }
end
- it "passes through the proc which returns empty with custom options(:using)" do
+ it 'passes through the proc which returns empty with custom options(:using)' do
module EntitySpec
class FriendEntity < Grape::Entity
root 'friends', 'friend'
expose :name, :email
end
end
fresh_class.class_eval do
- expose :first_friend, using: EntitySpec::FriendEntity do |user, options|
-
+ expose :first_friend, using: EntitySpec::FriendEntity do |_user, _opts|
end
end
rep = subject.send(:value_for, :first_friend)
rep.should be_kind_of EntitySpec::FriendEntity
@@ -871,59 +870,59 @@
it 'returns a formatted value if format_with is passed a lambda' do
subject.send(:value_for, :fantasies).should == ['Nessy', 'Double Rainbows', 'Unicorns']
end
- it "tries instance methods on the entity first" do
+ it 'tries instance methods on the entity first' do
module EntitySpec
class DelegatingEntity < Grape::Entity
root 'friends', 'friend'
expose :name
expose :email
private
def name
- "cooler name"
+ 'cooler name'
end
end
end
- friend = double("Friend", name: "joe", email: "joe@example.com")
+ friend = double('Friend', name: 'joe', email: 'joe@example.com')
rep = EntitySpec::DelegatingEntity.new(friend)
- rep.send(:value_for, :name).should == "cooler name"
- rep.send(:value_for, :email).should == "joe@example.com"
+ rep.send(:value_for, :name).should == 'cooler name'
+ rep.send(:value_for, :email).should == 'joe@example.com'
end
- context "using" do
+ context 'using' do
before do
module EntitySpec
class UserEntity < Grape::Entity
expose :name, :email
end
end
end
- it "string" do
+ it 'string' do
fresh_class.class_eval do
- expose :friends, using: "EntitySpec::UserEntity"
+ expose :friends, using: 'EntitySpec::UserEntity'
end
rep = subject.send(:value_for, :friends)
rep.should be_kind_of Array
rep.size.should == 2
- rep.all? { |r| r.is_a?(EntitySpec::UserEntity) }.should be_true
+ rep.all? { |r| r.is_a?(EntitySpec::UserEntity) }.should be true
end
it 'class' do
fresh_class.class_eval do
expose :friends, using: EntitySpec::UserEntity
end
rep = subject.send(:value_for, :friends)
rep.should be_kind_of Array
rep.size.should == 2
- rep.all? { |r| r.is_a?(EntitySpec::UserEntity) }.should be_true
+ rep.all? { |r| r.is_a?(EntitySpec::UserEntity) }.should be true
end
end
end
describe '#documentation' do
@@ -932,20 +931,20 @@
subject.documentation.should == {}
end
it 'returns each defined documentation hash' do
- doc = { type: "foo", desc: "bar" }
+ doc = { type: 'foo', desc: 'bar' }
fresh_class.expose :name, documentation: doc
fresh_class.expose :email, documentation: doc
fresh_class.expose :birthday
subject.documentation.should == { name: doc, email: doc }
end
it 'returns each defined documentation hash with :as param considering' do
- doc = { type: "foo", desc: "bar" }
+ doc = { type: 'foo', desc: 'bar' }
fresh_class.expose :name, documentation: doc, as: :label
fresh_class.expose :email, documentation: doc
fresh_class.expose :birthday
subject.documentation.should == { label: doc, email: doc }
@@ -971,57 +970,57 @@
describe '#conditions_met?' do
it 'only passes through hash :if exposure if all attributes match' do
exposure_options = { if: { condition1: true, condition2: true } }
- subject.send(:conditions_met?, exposure_options, {}).should be_false
- subject.send(:conditions_met?, exposure_options, condition1: true).should be_false
- subject.send(:conditions_met?, exposure_options, condition1: true, condition2: true).should be_true
- subject.send(:conditions_met?, exposure_options, condition1: false, condition2: true).should be_false
- subject.send(:conditions_met?, exposure_options, condition1: true, condition2: true, other: true).should be_true
+ subject.send(:conditions_met?, exposure_options, {}).should be false
+ subject.send(:conditions_met?, exposure_options, condition1: true).should be false
+ subject.send(:conditions_met?, exposure_options, condition1: true, condition2: true).should be true
+ subject.send(:conditions_met?, exposure_options, condition1: false, condition2: true).should be false
+ subject.send(:conditions_met?, exposure_options, condition1: true, condition2: true, other: true).should be true
end
it 'looks for presence/truthiness if a symbol is passed' do
exposure_options = { if: :condition1 }
- subject.send(:conditions_met?, exposure_options, {}).should be_false
- subject.send(:conditions_met?, exposure_options, condition1: true).should be_true
- subject.send(:conditions_met?, exposure_options, condition1: false).should be_false
- subject.send(:conditions_met?, exposure_options, condition1: nil).should be_false
+ subject.send(:conditions_met?, exposure_options, {}).should be false
+ subject.send(:conditions_met?, exposure_options, condition1: true).should be true
+ subject.send(:conditions_met?, exposure_options, condition1: false).should be false
+ subject.send(:conditions_met?, exposure_options, condition1: nil).should be false
end
it 'looks for absence/falsiness if a symbol is passed' do
exposure_options = { unless: :condition1 }
- subject.send(:conditions_met?, exposure_options, {}).should be_true
- subject.send(:conditions_met?, exposure_options, condition1: true).should be_false
- subject.send(:conditions_met?, exposure_options, condition1: false).should be_true
- subject.send(:conditions_met?, exposure_options, condition1: nil).should be_true
+ subject.send(:conditions_met?, exposure_options, {}).should be true
+ subject.send(:conditions_met?, exposure_options, condition1: true).should be false
+ subject.send(:conditions_met?, exposure_options, condition1: false).should be true
+ subject.send(:conditions_met?, exposure_options, condition1: nil).should be true
end
it 'only passes through proc :if exposure if it returns truthy value' do
exposure_options = { if: lambda { |_, opts| opts[:true] } }
- subject.send(:conditions_met?, exposure_options, true: false).should be_false
- subject.send(:conditions_met?, exposure_options, true: true).should be_true
+ subject.send(:conditions_met?, exposure_options, true: false).should be false
+ subject.send(:conditions_met?, exposure_options, true: true).should be true
end
it 'only passes through hash :unless exposure if any attributes do not match' do
exposure_options = { unless: { condition1: true, condition2: true } }
- subject.send(:conditions_met?, exposure_options, {}).should be_true
- subject.send(:conditions_met?, exposure_options, condition1: true).should be_false
- subject.send(:conditions_met?, exposure_options, condition1: true, condition2: true).should be_false
- subject.send(:conditions_met?, exposure_options, condition1: false, condition2: true).should be_false
- subject.send(:conditions_met?, exposure_options, condition1: true, condition2: true, other: true).should be_false
- subject.send(:conditions_met?, exposure_options, condition1: false, condition2: false).should be_true
+ subject.send(:conditions_met?, exposure_options, {}).should be true
+ subject.send(:conditions_met?, exposure_options, condition1: true).should be false
+ subject.send(:conditions_met?, exposure_options, condition1: true, condition2: true).should be false
+ subject.send(:conditions_met?, exposure_options, condition1: false, condition2: true).should be false
+ subject.send(:conditions_met?, exposure_options, condition1: true, condition2: true, other: true).should be false
+ subject.send(:conditions_met?, exposure_options, condition1: false, condition2: false).should be true
end
it 'only passes through proc :unless exposure if it returns falsy value' do
exposure_options = { unless: lambda { |_, options| options[:true] == true } }
- subject.send(:conditions_met?, exposure_options, true: false).should be_true
- subject.send(:conditions_met?, exposure_options, true: true).should be_false
+ subject.send(:conditions_met?, exposure_options, true: false).should be true
+ subject.send(:conditions_met?, exposure_options, true: true).should be false
end
end
describe '::DSL' do
subject { Class.new }