Skip to content

Commit d56122f

Browse files
committed
Merge pull request #91 from etehtsea/fix-arg-error
Fix OpenStruct serializing
2 parents 4f520f6 + 0d2f46c commit d56122f

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

CHANGELOG.md

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

44
* Your contribution here.
5+
* [#91](https://github.com/intridea/grape-entity/pull/91): Fix OpenStruct serializing - [@etehtsea](https://github.com/etehtsea).
56

67
0.4.4 (2014-08-17)
78
==================

lib/grape_entity/entity.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ def delegate_attribute(attribute)
554554
elsif object.respond_to?(:fetch, true)
555555
object.fetch(name)
556556
else
557-
raise ArgumentError
557+
object.send(name)
558558
end
559559
end
560560
end

spec/grape_entity/entity_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'spec_helper'
2+
require 'ostruct'
23

34
describe Grape::Entity do
45

@@ -442,6 +443,19 @@ class Parent < Person
442443
representation = subject.represent({ awesome: true }, serializable: true)
443444
expect(representation).to eq(awesome: true)
444445
end
446+
447+
it 'returns a serialized hash of an OpenStruct' do
448+
subject.expose(:awesome)
449+
representation = subject.represent(OpenStruct.new, serializable: true)
450+
expect(representation).to eq(awesome: nil)
451+
end
452+
453+
it 'raises error if field not found' do
454+
subject.expose(:awesome)
455+
expect {
456+
subject.represent(Object.new, serializable: true)
457+
}.to raise_error(NoMethodError)
458+
end
445459
end
446460

447461
describe '.present_collection' do

0 commit comments

Comments
 (0)