Skip to content

Allow global settings on Grape::Entity (0.4.x) #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2015
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
20 changes: 10 additions & 10 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
# This configuration was generated by `rubocop --auto-gen-config`
# on 2015-05-21 22:47:03 +0700 using RuboCop version 0.31.0.
# on 2015-08-10 12:30:52 +0300 using RuboCop version 0.31.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 8
# Offense count: 7
Metrics/AbcSize:
Max: 51
Max: 45

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 328
Max: 333

# Offense count: 5
# Offense count: 4
Metrics/CyclomaticComplexity:
Max: 17

# Offense count: 176
# Offense count: 193
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 146

# Offense count: 7
# Offense count: 8
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 32
Max: 26

# Offense count: 5
# Offense count: 7
Metrics/PerceivedComplexity:
Max: 15

# Offense count: 31
# Offense count: 37
Style/Documentation:
Enabled: false

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.4.8 (2015-08-10)
==================
* [#167](https://github.com/ruby-grape/grape-entity/pull/167): Regression: global settings (exposures, formatters) on `Grape::Entity` should work: [#166](https://github.com/ruby-grape/grape-entity/issues/166) - [@marshall-lee](http://github.com/marshall-lee).

0.4.7 (2015-08-03)
==================
* [#164](https://github.com/ruby-grape/grape-entity/pull/164): Regression: entity instance methods were exposed with `NoMethodError`: [#163](https://github.com/ruby-grape/grape-entity/issues/163) - [@marshall-lee](http://github.com/marshall-lee).
Expand Down
19 changes: 14 additions & 5 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,18 @@ class << self
attr_accessor :nested_exposures
end

@exposures = {}
@root_exposures = {}
@nested_exposures = {}
@nested_attribute_names = {}
@formatters = {}

def self.inherited(subclass)
subclass.exposures = exposures.try(:dup) || {}
subclass.root_exposures = root_exposures.try(:dup) || {}
subclass.nested_exposures = nested_exposures.try(:dup) || {}
subclass.nested_attribute_names = nested_attribute_names.try(:dup) || {}
subclass.formatters = formatters.try(:dup) || {}
subclass.exposures = exposures.dup
subclass.root_exposures = root_exposures.dup
subclass.nested_exposures = nested_exposures.dup
subclass.nested_attribute_names = nested_attribute_names.dup
subclass.formatters = formatters.dup
end

# This method is the primary means by which you will declare what attributes
Expand Down Expand Up @@ -183,7 +189,10 @@ def self.expose(*args, &block)
end

def self.unexpose(attribute)
root_exposures.delete(attribute)
exposures.delete(attribute)
nested_exposures.delete(attribute)
nested_attribute_names.delete(attribute)
end

# Set options that will be applied to any exposures declared inside the block.
Expand Down
2 changes: 1 addition & 1 deletion lib/grape_entity/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GrapeEntity
VERSION = '0.4.7'
VERSION = '0.4.8'
end
27 changes: 27 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,26 @@ class Parent < Person
subject.expose(:size, format_with: :size_formatter)
expect(subject.represent(object).send(:value_for, :size)).to eq object.class.to_s
end

it 'works global on Grape::Entity' do
Grape::Entity.format_with :size_formatter do |_date|
self.object.class.to_s
end
object = {}

subject.expose(:size, format_with: :size_formatter)
expect(subject.represent(object).send(:value_for, :size)).to eq object.class.to_s
end
end

it 'works global on Grape::Entity' do
Grape::Entity.expose :a
object = { a: 11, b: 22 }
expect(Grape::Entity.represent(object).send(:value_for, :a)).to eq 11
subject.expose :b
expect(subject.represent(object).send(:value_for, :a)).to eq 11
expect(subject.represent(object).send(:value_for, :b)).to eq 22
Grape::Entity.unexpose :a
end
end

Expand Down Expand Up @@ -310,6 +330,13 @@ class Parent < Person
end
end
end

it 'works global on Grape::Entity' do
Grape::Entity.expose :a
expect(Grape::Entity.exposures).to eq(a: {})
Grape::Entity.unexpose :a
expect(Grape::Entity.exposures).to eq({})
end
end

describe '.with_options' do
Expand Down