Skip to content

Commit

Permalink
Generalize hash conversion
Browse files Browse the repository at this point in the history
Before it called to_hash just for Likeno::Entity instances. This was too
strict and was relaxed to check if the object responds to the method.

This should improve compatibility with other applications avoiding
overring by them.

Signed off by: Diego Araújo <[email protected]>
  • Loading branch information
rafamanzo committed Mar 23, 2016
1 parent e4bf02c commit dcf1f79
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 dcf1f79

Please sign in to comment.