Skip to content

Allow global settings on Grape::Entity. #168

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
6 changes: 3 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This configuration was generated by `rubocop --auto-gen-config`
# on 2015-08-02 19:30:25 +0300 using RuboCop version 0.31.0.
# on 2015-08-10 13:14:22 +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
Expand All @@ -12,13 +12,13 @@ Metrics/AbcSize:
# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 198
Max: 202

# Offense count: 3
Metrics/CyclomaticComplexity:
Max: 11

# Offense count: 208
# Offense count: 210
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 146
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): Fix: deep projections (`:only`, `:except`) were unaware of nesting: [#156](https://github.com/ruby-grape/grape-entity/issues/156) - [@marshall-lee](https://github.com/marshall-lee).
* Your contribution here.

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
23 changes: 15 additions & 8 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,26 @@ def entity(options = {})
end

class << self
attr_accessor :root_exposure
def root_exposure
@root_exposure ||= Exposure.new(nil, nesting: true)
end

attr_writer :root_exposure

# Returns all formatters that are registered for this and it's ancestors
# @return [Hash] of formatters
attr_accessor :formatters
def formatters
@formatters ||= {}
end

attr_writer :formatters
end

@formatters = {}

def self.inherited(subclass)
subclass.root_exposure = root_exposure.try(:dup) || build_root_exposure
subclass.formatters = formatters.try(:dup) || {}
subclass.root_exposure = root_exposure.dup
subclass.formatters = formatters.dup
end

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

def self.build_root_exposure
Exposure.new(nil, nesting: true)
end

# Returns exposures that have been declared for this Entity on the top level.
# @return [Array] of exposures
def self.root_exposures
Expand Down
27 changes: 27 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,26 @@ class Parent < Person
subject.expose(:size, format_with: :size_formatter)
expect(subject.represent(object).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).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).value_for(:a)).to eq 11
subject.expose :b
expect(subject.represent(object).value_for(:a)).to eq 11
expect(subject.represent(object).value_for(:b)).to eq 22
Grape::Entity.unexpose :a
end
end

Expand Down Expand Up @@ -370,6 +390,13 @@ class Parent < Person
end
end.to raise_error(/You cannot call 'unexpose`/)
end

it 'works global on Grape::Entity' do
Grape::Entity.expose :x
expect(Grape::Entity.root_exposures[0].attribute).to eq(:x)
Grape::Entity.unexpose :x
expect(Grape::Entity.root_exposures).to eq([])
end
end

describe '.with_options' do
Expand Down