diff --git a/lib/jsonapi/deserializable/relationship.rb b/lib/jsonapi/deserializable/relationship.rb index deb87fd..56e809e 100644 --- a/lib/jsonapi/deserializable/relationship.rb +++ b/lib/jsonapi/deserializable/relationship.rb @@ -24,23 +24,25 @@ module JSONAPI deserialize! end - def to_h + def to_hash @hash end + alias to_h to_hash private def deserialize! @hash = {} + return unless @document.key?('data') if @data.is_a?(Array) deserialize_has_many! - else + elsif @data.nil? || @data.is_a?(Hash) deserialize_has_one! end end def deserialize_has_one! - return unless self.class.has_one_block && @document.key?('data') + return unless self.class.has_one_block id = @data && @data['id'] type = @data && @data['type'] instance_exec(@document, id, type, &self.class.has_one_block) diff --git a/lib/jsonapi/deserializable/resource.rb b/lib/jsonapi/deserializable/resource.rb index 7729cbd..e08944a 100644 --- a/lib/jsonapi/deserializable/resource.rb +++ b/lib/jsonapi/deserializable/resource.rb @@ -6,13 +6,12 @@ module JSONAPI include ResourceDSL class << self - attr_accessor :type_block, :id_block - attr_accessor :attr_blocks - attr_accessor :has_one_rel_blocks, :has_many_rel_blocks + attr_accessor :type_block, :id_block, :attr_blocks, + :has_one_rel_blocks, :has_many_rel_blocks end self.attr_blocks = {} - self.has_one_rel_blocks = {} + self.has_one_rel_blocks = {} self.has_many_rel_blocks = {} def self.inherited(klass) @@ -38,9 +37,10 @@ module JSONAPI deserialize! end - def to_h + def to_hash @hash end + alias to_h to_hash private