Skip to content

Commit ae847ca

Browse files
committed
Allow global settings on Grape::Entity.
This fixes a regression introduced after merging a #134. It's something undocumented before but it worked before and I don't see anything harmful in the presence of this feature. Fixes #166. Conflicts: .rubocop_todo.yml CHANGELOG.md lib/grape_entity/entity.rb lib/grape_entity/version.rb spec/grape_entity/entity_spec.rb
1 parent a7d0396 commit ae847ca

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

.rubocop_todo.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This configuration was generated by `rubocop --auto-gen-config`
2-
# on 2015-08-02 19:30:25 +0300 using RuboCop version 0.31.0.
2+
# on 2015-08-10 13:14:22 +0300 using RuboCop version 0.31.0.
33
# The point is for the user to remove these configuration records
44
# one by one as the offenses are removed from the code base.
55
# Note that changes in the inspected code, or installation of new
@@ -12,13 +12,13 @@ Metrics/AbcSize:
1212
# Offense count: 2
1313
# Configuration parameters: CountComments.
1414
Metrics/ClassLength:
15-
Max: 198
15+
Max: 202
1616

1717
# Offense count: 3
1818
Metrics/CyclomaticComplexity:
1919
Max: 11
2020

21-
# Offense count: 208
21+
# Offense count: 210
2222
# Configuration parameters: AllowURI, URISchemes.
2323
Metrics/LineLength:
2424
Max: 146

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
* [#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).
1515
* Your contribution here.
1616

17+
0.4.8 (2015-08-10)
18+
==================
19+
* [#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).
20+
1721
0.4.7 (2015-08-03)
1822
==================
1923
* [#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).

lib/grape_entity/entity.rb

+15-8
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,26 @@ def entity(options = {})
9999
end
100100

101101
class << self
102-
attr_accessor :root_exposure
102+
def root_exposure
103+
@root_exposure ||= Exposure.new(nil, nesting: true)
104+
end
105+
106+
attr_writer :root_exposure
107+
103108
# Returns all formatters that are registered for this and it's ancestors
104109
# @return [Hash] of formatters
105-
attr_accessor :formatters
110+
def formatters
111+
@formatters ||= {}
112+
end
113+
114+
attr_writer :formatters
106115
end
107116

117+
@formatters = {}
118+
108119
def self.inherited(subclass)
109-
subclass.root_exposure = root_exposure.try(:dup) || build_root_exposure
110-
subclass.formatters = formatters.try(:dup) || {}
120+
subclass.root_exposure = root_exposure.dup
121+
subclass.formatters = formatters.dup
111122
end
112123

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

178-
def self.build_root_exposure
179-
Exposure.new(nil, nesting: true)
180-
end
181-
182189
# Returns exposures that have been declared for this Entity on the top level.
183190
# @return [Array] of exposures
184191
def self.root_exposures

spec/grape_entity/entity_spec.rb

+27
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,26 @@ class Parent < Person
322322
subject.expose(:size, format_with: :size_formatter)
323323
expect(subject.represent(object).value_for(:size)).to eq object.class.to_s
324324
end
325+
326+
it 'works global on Grape::Entity' do
327+
Grape::Entity.format_with :size_formatter do |_date|
328+
self.object.class.to_s
329+
end
330+
object = {}
331+
332+
subject.expose(:size, format_with: :size_formatter)
333+
expect(subject.represent(object).value_for(:size)).to eq object.class.to_s
334+
end
335+
end
336+
337+
it 'works global on Grape::Entity' do
338+
Grape::Entity.expose :x
339+
object = { x: 11, y: 22 }
340+
expect(Grape::Entity.represent(object).value_for(:x)).to eq 11
341+
subject.expose :y
342+
expect(subject.represent(object).value_for(:x)).to eq 11
343+
expect(subject.represent(object).value_for(:y)).to eq 22
344+
Grape::Entity.unexpose :x
325345
end
326346
end
327347

@@ -370,6 +390,13 @@ class Parent < Person
370390
end
371391
end.to raise_error(/You cannot call 'unexpose`/)
372392
end
393+
394+
it 'works global on Grape::Entity' do
395+
Grape::Entity.expose :x
396+
expect(Grape::Entity.root_exposures[0].attribute).to eq(:x)
397+
Grape::Entity.unexpose :x
398+
expect(Grape::Entity.root_exposures).to eq([])
399+
end
373400
end
374401

375402
describe '.with_options' do

0 commit comments

Comments
 (0)