Skip to content

Commit f6e0796

Browse files
committed
Allow global settings on Grape::Entity.
This fixes a regression introduced after merging a ruby-grape#134. It's something undocumented before but it worked before and I don't see anything harmful in the presence of this feature. Fixes ruby-grape#166.
1 parent 7235364 commit f6e0796

File tree

5 files changed

+56
-16
lines changed

5 files changed

+56
-16
lines changed

.rubocop_todo.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
# This configuration was generated by `rubocop --auto-gen-config`
2-
# on 2015-05-21 22:47:03 +0700 using RuboCop version 0.31.0.
2+
# on 2015-08-10 12:30:52 +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
66
# versions of RuboCop, may require this file to be generated again.
77

8-
# Offense count: 8
8+
# Offense count: 7
99
Metrics/AbcSize:
10-
Max: 51
10+
Max: 45
1111

1212
# Offense count: 1
1313
# Configuration parameters: CountComments.
1414
Metrics/ClassLength:
15-
Max: 328
15+
Max: 333
1616

17-
# Offense count: 5
17+
# Offense count: 4
1818
Metrics/CyclomaticComplexity:
1919
Max: 17
2020

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

26-
# Offense count: 7
26+
# Offense count: 8
2727
# Configuration parameters: CountComments.
2828
Metrics/MethodLength:
29-
Max: 32
29+
Max: 26
3030

31-
# Offense count: 5
31+
# Offense count: 7
3232
Metrics/PerceivedComplexity:
3333
Max: 15
3434

35-
# Offense count: 31
35+
# Offense count: 37
3636
Style/Documentation:
3737
Enabled: false
3838

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
0.4.8 (2015-08-10)
2+
==================
3+
* [#167](https://github.com/intridea/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).
4+
15
0.4.7 (2015-08-03)
26
==================
37
* [#164](https://github.com/intridea/grape-entity/pull/164): Regression: entity instance methods were exposed with `NoMethodError`: [#163](https://github.com/intridea/grape-entity/issues/163) - [@marshall-lee](http://github.com/marshall-lee).

lib/grape_entity/entity.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,18 @@ class << self
112112
attr_accessor :nested_exposures
113113
end
114114

115+
@exposures = {}
116+
@root_exposures = {}
117+
@nested_exposures = {}
118+
@nested_attribute_names = {}
119+
@formatters = {}
120+
115121
def self.inherited(subclass)
116-
subclass.exposures = exposures.try(:dup) || {}
117-
subclass.root_exposures = root_exposures.try(:dup) || {}
118-
subclass.nested_exposures = nested_exposures.try(:dup) || {}
119-
subclass.nested_attribute_names = nested_attribute_names.try(:dup) || {}
120-
subclass.formatters = formatters.try(:dup) || {}
122+
subclass.exposures = exposures.try(:dup)
123+
subclass.root_exposures = root_exposures.try(:dup)
124+
subclass.nested_exposures = nested_exposures.try(:dup)
125+
subclass.nested_attribute_names = nested_attribute_names.try(:dup)
126+
subclass.formatters = formatters.try(:dup)
121127
end
122128

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

185191
def self.unexpose(attribute)
192+
root_exposures.delete(attribute)
186193
exposures.delete(attribute)
194+
nested_exposures.delete(attribute)
195+
nested_attribute_names.delete(attribute)
187196
end
188197

189198
# Set options that will be applied to any exposures declared inside the block.

lib/grape_entity/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module GrapeEntity
2-
VERSION = '0.4.7'
2+
VERSION = '0.4.8'
33
end

spec/grape_entity/entity_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,26 @@ class Parent < Person
278278
subject.expose(:size, format_with: :size_formatter)
279279
expect(subject.represent(object).send(:value_for, :size)).to eq object.class.to_s
280280
end
281+
282+
it 'works global on Grape::Entity' do
283+
Grape::Entity.format_with :size_formatter do |_date|
284+
self.object.class.to_s
285+
end
286+
object = {}
287+
288+
subject.expose(:size, format_with: :size_formatter)
289+
expect(subject.represent(object).send(:value_for, :size)).to eq object.class.to_s
290+
end
291+
end
292+
293+
it 'works global on Grape::Entity' do
294+
Grape::Entity.expose :x
295+
object = { x: 11, y: 22 }
296+
expect(Grape::Entity.represent(object).send(:value_for, :x)).to eq 11
297+
subject.expose :y
298+
expect(subject.represent(object).send(:value_for, :x)).to eq 11
299+
expect(subject.represent(object).send(:value_for, :y)).to eq 22
300+
Grape::Entity.unexpose :x
281301
end
282302
end
283303

@@ -310,6 +330,13 @@ class Parent < Person
310330
end
311331
end
312332
end
333+
334+
it 'works global on Grape::Entity' do
335+
Grape::Entity.expose :x
336+
expect(Grape::Entity.exposures).to eq(x: {})
337+
Grape::Entity.unexpose :x
338+
expect(Grape::Entity.exposures).to eq({})
339+
end
313340
end
314341

315342
describe '.with_options' do

0 commit comments

Comments
 (0)