Skip to content

Commit

Permalink
Merge pull request #17 from mezuro/generalize_nested_object_hash_conv…
Browse files Browse the repository at this point in the history
…ersion

Generalize hash conversion
  • Loading branch information
danielkza committed Mar 23, 2016
2 parents e4bf02c + dcf1f79 commit e7bd336
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Likeno is a library for remote data model access over HTTP

== Unreleased

* Generalize Hash conversion for non Likeno::Entity instances

== v1.0.1 - 23/03/2016

* Fix hash conversion
Expand Down
2 changes: 1 addition & 1 deletion lib/likeno/helpers/hash_converters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def date_time_to_s(date)
def convert_to_hash(value)
return value if value.nil?
return value.collect { |element| convert_to_hash(element) } if value.is_a? Array
return value.to_hash if value.is_a?(Likeno::Entity)
return value.to_hash if value.respond_to?(:to_hash)
return date_time_to_s(value) if value.is_a? DateTime
return 'INF' if value.is_a?(Float) && value.infinite? == 1
return '-INF' if value.is_a?(Float) && value.infinite? == -1
Expand Down
20 changes: 20 additions & 0 deletions spec/likeno/helpers/hash_converters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@

include Likeno::HashConverters

class Another
attr_accessor :attr

def to_hash
{'attr' => self.attr}
end
end

describe Likeno::HashConverters do
describe 'date_time_to_s' do
context 'with 21/12/1995 (first Ruby publication)' do
Expand Down Expand Up @@ -74,6 +82,18 @@
expect(convert_to_hash(-1.0 / 0.0)).to eq('-INF')
end
end

context 'without a base class that responds to to_hash' do
let(:obj) { Another.new }

before do
obj.attr = "attribute"
end

it 'is expected to do the conversion' do
expect(convert_to_hash(obj)).to eq({'attr' => obj.attr})
end
end
end

describe 'field_to_hash' do
Expand Down

0 comments on commit e7bd336

Please sign in to comment.