Skip to content

Commit 9305945

Browse files
committed
updates dependencies
- adds changelog entry
1 parent 6efccaa commit 9305945

File tree

10 files changed

+71
-81
lines changed

10 files changed

+71
-81
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### Features
44

5+
* [#247](https://github.com/ruby-grape/grape-entity/pull/247): Updates dependencies; refactores to make specs green - [@LeFnord](https://github.com/LeFnord).
56
* Your contribution here.
67

78
#### Fixes

Gemfile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ if RUBY_VERSION < '2.2.2'
77
gem 'activesupport', '<5.0.0'
88
end
99

10-
gem 'json', '< 2', group: [:development, :test]
11-
1210
group :development, :test do
13-
gem 'rubocop', '0.31.0'
1411
gem 'ruby-grape-danger', '~> 0.1.0', require: false
1512
end
1613

grape-entity.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
1919

2020
s.add_development_dependency 'bundler'
2121
s.add_development_dependency 'rake'
22+
s.add_development_dependency 'rubocop', '~> 0.40'
2223
s.add_development_dependency 'rspec', '~> 3.0'
2324
s.add_development_dependency 'rack-test'
2425
s.add_development_dependency 'maruku'

lib/grape_entity/condition/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def initialize(inverse = false)
1111
end
1212

1313
def ==(other)
14-
(self.class == other.class) && (self.inversed? == other.inversed?)
14+
(self.class == other.class) && (inversed? == other.inversed?)
1515
end
1616

1717
def inversed?
@@ -23,7 +23,7 @@ def met?(entity, options)
2323
end
2424

2525
def if_value(_entity, _options)
26-
fail NotImplementedError
26+
raise NotImplementedError
2727
end
2828

2929
def unless_value(entity, options)

lib/grape_entity/entity.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ def self.expose(*args, &block)
151151
options = merge_options(args.last.is_a?(Hash) ? args.pop : {})
152152

153153
if args.size > 1
154-
fail ArgumentError, 'You may not use the :as option on multi-attribute exposures.' if options[:as]
155-
fail ArgumentError, 'You may not use block-setting on multi-attribute exposures.' if block_given?
154+
raise ArgumentError, 'You may not use the :as option on multi-attribute exposures.' if options[:as]
155+
raise ArgumentError, 'You may not use block-setting on multi-attribute exposures.' if block_given?
156156
end
157157

158-
fail ArgumentError, 'You may not use block-setting when also using format_with' if block_given? && options[:format_with].respond_to?(:call)
158+
raise ArgumentError, 'You may not use block-setting when also using format_with' if block_given? && options[:format_with].respond_to?(:call)
159159

160160
if block_given?
161161
if block.parameters.any?
@@ -214,7 +214,7 @@ def self.can_unexpose?
214214
end
215215

216216
def self.cannot_unexpose!
217-
fail "You cannot call 'unexpose` inside of nesting exposure!"
217+
raise "You cannot call 'unexpose` inside of nesting exposure!"
218218
end
219219

220220
# Set options that will be applied to any exposures declared inside the block.
@@ -270,7 +270,7 @@ def self.documentation
270270
# end
271271
#
272272
def self.format_with(name, &block)
273-
fail ArgumentError, 'You must pass a block for formatters' unless block_given?
273+
raise ArgumentError, 'You must pass a block for formatters' unless block_given?
274274
formatters[name.to_sym] = block
275275
end
276276

@@ -392,8 +392,8 @@ def self.present_collection(present_collection = false, collection_name = :items
392392
# @option options :only [Array] all the fields that should be returned
393393
# @option options :except [Array] all the fields that should not be returned
394394
def self.represent(objects, options = {})
395-
if objects.respond_to?(:to_ary) && ! @present_collection
396-
root_element = root_element(:collection_root)
395+
if objects.respond_to?(:to_ary) && !@present_collection
396+
root_element = root_element(:collection_root)
397397
inner = objects.to_ary.map { |object| new(object, options.reverse_merge(collection: true)).presented }
398398
else
399399
objects = { @collection_name => objects } if @present_collection
@@ -485,7 +485,7 @@ def delegate_attribute(attribute)
485485
end
486486
end
487487

488-
alias_method :as_json, :serializable_hash
488+
alias as_json serializable_hash
489489

490490
def to_json(options = {})
491491
options = options.to_h if options && options.respond_to?(:to_h)
@@ -536,7 +536,7 @@ def self.merge_options(options)
536536
# @param options [Hash] Exposure options.
537537
def self.valid_options(options)
538538
options.keys.each do |key|
539-
fail ArgumentError, "#{key.inspect} is not a valid option." unless OPTIONS.include?(key)
539+
raise ArgumentError, "#{key.inspect} is not a valid option." unless OPTIONS.include?(key)
540540
end
541541

542542
options[:using] = options.delete(:with) if options.key?(:with)

lib/grape_entity/exposure/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ def valid?(entity)
5151
if @is_safe
5252
is_delegatable
5353
else
54-
is_delegatable || fail(NoMethodError, "#{entity.class.name} missing attribute `#{@attribute}' on #{entity.object}")
54+
is_delegatable || raise(NoMethodError, "#{entity.class.name} missing attribute `#{@attribute}' on #{entity.object}")
5555
end
5656
end
5757

5858
def value(_entity, _options)
59-
fail NotImplementedError
59+
raise NotImplementedError
6060
end
6161

6262
def serializable_value(entity, options)

lib/grape_entity/exposure/nesting_exposure.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,7 @@ def normalized_exposures(entity, options)
107107
# For the given key if the last candidates for exposing are nesting then combine them.
108108
nesting_tail = []
109109
exposures.reverse_each do |exposure|
110-
if exposure.nesting?
111-
nesting_tail.unshift exposure
112-
else
113-
break
114-
end
110+
nesting_tail.unshift exposure if exposure.nesting?
115111
end
116112
new_nested_exposures = nesting_tail.flat_map(&:nested_exposures)
117113
NestingExposure.new(key, {}, [], new_nested_exposures).tap do |new_exposure|

lib/grape_entity/exposure/nesting_exposure/output_builder.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,20 @@ def add(exposure, result)
1212
# Save a result array in collections' array if it should be merged
1313
if result.is_a?(Array) && exposure.for_merge
1414
@output_collection << result
15-
else
16-
15+
elsif exposure.for_merge
1716
# If we have an array which should not be merged - save it with a key as a hash
1817
# If we have hash which should be merged - save it without a key (merge)
19-
if exposure.for_merge
20-
return unless result
21-
@output_hash.merge! result, &merge_strategy(exposure.for_merge)
22-
else
23-
@output_hash[exposure.key] = result
24-
end
25-
18+
return unless result
19+
@output_hash.merge! result, &merge_strategy(exposure.for_merge)
20+
else
21+
@output_hash[exposure.key] = result
2622
end
2723
end
2824

2925
def kind_of?(klass)
3026
klass == output.class || super
3127
end
32-
alias_method :is_a?, :kind_of?
28+
alias is_a? kind_of?
3329

3430
def __getobj__
3531
output

lib/grape_entity/options.rb

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ def empty?
5454
end
5555

5656
def ==(other)
57-
if other.is_a? Options
58-
@opts_hash == other.opts_hash
59-
else
60-
@opts_hash == other
61-
end
57+
@opts_hash == if other.is_a? Options
58+
other.opts_hash
59+
else
60+
other
61+
end
6262
end
6363

6464
def should_return_key?(key)
@@ -79,42 +79,20 @@ def only_fields(for_key = nil)
7979
return nil unless @has_only
8080

8181
@only_fields ||= @opts_hash[:only].each_with_object({}) do |attribute, allowed_fields|
82-
if attribute.is_a?(Hash)
83-
attribute.each do |attr, nested_attrs|
84-
allowed_fields[attr] ||= []
85-
allowed_fields[attr] += nested_attrs
86-
end
87-
else
88-
allowed_fields[attribute] = true
89-
end
90-
end.symbolize_keys
91-
92-
if for_key && @only_fields[for_key].is_a?(Array)
93-
@only_fields[for_key]
94-
elsif for_key.nil?
95-
@only_fields
82+
build_symbolized_hash(attribute, allowed_fields)
9683
end
84+
85+
only_for_given(for_key, @only_fields)
9786
end
9887

9988
def except_fields(for_key = nil)
10089
return nil unless @has_except
10190

10291
@except_fields ||= @opts_hash[:except].each_with_object({}) do |attribute, allowed_fields|
103-
if attribute.is_a?(Hash)
104-
attribute.each do |attr, nested_attrs|
105-
allowed_fields[attr] ||= []
106-
allowed_fields[attr] += nested_attrs
107-
end
108-
else
109-
allowed_fields[attribute] = true
110-
end
111-
end.symbolize_keys
112-
113-
if for_key && @except_fields[for_key].is_a?(Array)
114-
@except_fields[for_key]
115-
elsif for_key.nil?
116-
@except_fields
92+
build_symbolized_hash(attribute, allowed_fields)
11793
end
94+
95+
only_for_given(for_key, @except_fields)
11896
end
11997

12098
def with_attr_path(part)
@@ -141,6 +119,28 @@ def build_for_nesting(key)
141119

142120
Options.new(new_opts_hash)
143121
end
122+
123+
def build_symbolized_hash(attribute, hash)
124+
if attribute.is_a?(Hash)
125+
attribute.each do |attr, nested_attrs|
126+
hash[attr.to_sym] = build_symbolized_hash(nested_attrs, {})
127+
end
128+
elsif attribute.is_a?(Array)
129+
return attribute.each { |x| build_symbolized_hash(x, {}) }
130+
else
131+
hash[attribute.to_sym] = true
132+
end
133+
134+
hash
135+
end
136+
137+
def only_for_given(key, fields)
138+
if key && fields[key].is_a?(Array)
139+
fields[key]
140+
elsif key.nil?
141+
fields
142+
end
143+
end
144144
end
145145
end
146146
end

spec/grape_entity/entity_spec.rb

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,7 @@ class Parent < Person
289289
end
290290

291291
additional_hash = { users: [{ id: 1, name: 'John' }, { id: 2, name: 'Jay' }],
292-
admins: [{ id: 3, name: 'Jack' }, { id: 4, name: 'James' }]
293-
}
292+
admins: [{ id: 3, name: 'Jack' }, { id: 4, name: 'James' }] }
294293
expect(subject.represent(additional_hash).serializable_hash).to eq(
295294
profiles: additional_hash[:users] + additional_hash[:admins],
296295
awesome: { just_a_key: 'value', just_another_key: 'value' }
@@ -354,20 +353,20 @@ class Parent < Person
354353

355354
subject.expose :birthday, format_with: :timestamp
356355

357-
model = { birthday: Time.gm(2012, 2, 27) }
356+
model = { birthday: Time.gm(2012, 2, 27) }
358357
expect(subject.new(double(model)).as_json[:birthday]).to eq '02/27/2012'
359358
end
360359

361360
it 'formats an exposure with a :format_with lambda that returns a value from the entity instance' do
362361
object = {}
363362

364-
subject.expose(:size, format_with: ->(_value) { self.object.class.to_s })
363+
subject.expose(:size, format_with: ->(_value) { object.class.to_s })
365364
expect(subject.represent(object).value_for(:size)).to eq object.class.to_s
366365
end
367366

368367
it 'formats an exposure with a :format_with symbol that returns a value from the entity instance' do
369368
subject.format_with :size_formatter do |_date|
370-
self.object.class.to_s
369+
object.class.to_s
371370
end
372371

373372
object = {}
@@ -378,7 +377,7 @@ class Parent < Person
378377

379378
it 'works global on Grape::Entity' do
380379
Grape::Entity.format_with :size_formatter do |_date|
381-
self.object.class.to_s
380+
object.class.to_s
382381
end
383382
object = {}
384383

@@ -609,14 +608,14 @@ class Parent < Person
609608
end
610609

611610
it 'returns multiple entities if called with a collection' do
612-
representation = subject.represent(4.times.map { Object.new })
611+
representation = subject.represent(Array.new(4) { Object.new })
613612
expect(representation).to be_kind_of Array
614613
expect(representation.size).to eq(4)
615614
expect(representation.reject { |r| r.is_a?(subject) }).to be_empty
616615
end
617616

618617
it 'adds the collection: true option if called with a collection' do
619-
representation = subject.represent(4.times.map { Object.new })
618+
representation = subject.represent(Array.new(4) { Object.new })
620619
representation.each { |r| expect(r.options[:collection]).to be true }
621620
end
622621

@@ -628,7 +627,7 @@ class Parent < Person
628627

629628
it 'returns a serialized array of hashes of multiple objects if serializable: true' do
630629
subject.expose(:awesome) { |_| true }
631-
representation = subject.represent(2.times.map { Object.new }, serializable: true)
630+
representation = subject.represent(Array.new(2) { Object.new }, serializable: true)
632631
expect(representation).to eq([{ awesome: true }, { awesome: true }])
633632
end
634633

@@ -903,7 +902,7 @@ class Parent < Person
903902
subject.present_collection true
904903
subject.expose :items
905904

906-
representation = subject.represent(4.times.map { Object.new })
905+
representation = subject.represent(Array.new(4) { Object.new })
907906
expect(representation).to be_kind_of(subject)
908907
expect(representation.object).to be_kind_of(Hash)
909908
expect(representation.object).to have_key :items
@@ -915,7 +914,7 @@ class Parent < Person
915914
subject.present_collection true, :my_items
916915
subject.expose :my_items
917916

918-
representation = subject.represent(4.times.map { Object.new }, serializable: true)
917+
representation = subject.represent(Array.new(4) { Object.new }, serializable: true)
919918
expect(representation).to be_kind_of(Grape::Entity::Exposure::NestingExposure::OutputBuilder)
920919
expect(representation).to be_kind_of(Hash)
921920
expect(representation).to have_key :my_items
@@ -941,7 +940,7 @@ class Parent < Person
941940

942941
context 'with an array of objects' do
943942
it 'allows a root element name to be specified' do
944-
representation = subject.represent(4.times.map { Object.new })
943+
representation = subject.represent(Array.new(4) { Object.new })
945944
expect(representation).to be_kind_of Hash
946945
expect(representation).to have_key 'things'
947946
expect(representation['things']).to be_kind_of Array
@@ -952,13 +951,13 @@ class Parent < Person
952951

953952
context 'it can be overridden' do
954953
it 'can be disabled' do
955-
representation = subject.represent(4.times.map { Object.new }, root: false)
954+
representation = subject.represent(Array.new(4) { Object.new }, root: false)
956955
expect(representation).to be_kind_of Array
957956
expect(representation.size).to eq 4
958957
expect(representation.reject { |r| r.is_a?(subject) }).to be_empty
959958
end
960959
it 'can use a different name' do
961-
representation = subject.represent(4.times.map { Object.new }, root: 'others')
960+
representation = subject.represent(Array.new(4) { Object.new }, root: 'others')
962961
expect(representation).to be_kind_of Hash
963962
expect(representation).to have_key 'others'
964963
expect(representation['others']).to be_kind_of Array
@@ -984,7 +983,7 @@ class Parent < Person
984983

985984
context 'with an array of objects' do
986985
it 'allows a root element name to be specified' do
987-
representation = subject.represent(4.times.map { Object.new })
986+
representation = subject.represent(Array.new(4) { Object.new })
988987
expect(representation).to be_kind_of Array
989988
expect(representation.size).to eq 4
990989
expect(representation.reject { |r| r.is_a?(subject) }).to be_empty
@@ -1005,7 +1004,7 @@ class Parent < Person
10051004

10061005
context 'with an array of objects' do
10071006
it 'allows a root element name to be specified' do
1008-
representation = subject.represent(4.times.map { Object.new })
1007+
representation = subject.represent(Array.new(4) { Object.new })
10091008
expect(representation).to be_kind_of Hash
10101009
expect(representation).to have_key('things')
10111010
expect(representation['things']).to be_kind_of Array
@@ -1030,7 +1029,7 @@ class Parent < Person
10301029

10311030
it 'inherits array root root' do
10321031
child_class = Class.new(subject)
1033-
representation = child_class.represent(4.times.map { Object.new })
1032+
representation = child_class.represent(Array.new(4) { Object.new })
10341033
expect(representation).to be_kind_of Hash
10351034
expect(representation).to have_key('things')
10361035
expect(representation['things']).to be_kind_of Array

0 commit comments

Comments
 (0)