diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 6696356..7b6b14f 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -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 diff --git a/lib/likeno/helpers/hash_converters.rb b/lib/likeno/helpers/hash_converters.rb index 70f53f6..ac70a24 100644 --- a/lib/likeno/helpers/hash_converters.rb +++ b/lib/likeno/helpers/hash_converters.rb @@ -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 diff --git a/spec/likeno/helpers/hash_converters_spec.rb b/spec/likeno/helpers/hash_converters_spec.rb index cd967a6..aebedfb 100644 --- a/spec/likeno/helpers/hash_converters_spec.rb +++ b/spec/likeno/helpers/hash_converters_spec.rb @@ -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 @@ -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