Cleanup. (#2)

This commit is contained in:
Lucas Hosseini 2016-11-20 03:31:16 +01:00 committed by GitHub
parent 446102275c
commit a816fa56dd
2 changed files with 10 additions and 8 deletions

View File

@ -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)

View File

@ -6,9 +6,8 @@ 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 = {}
@ -38,9 +37,10 @@ module JSONAPI
deserialize!
end
def to_h
def to_hash
@hash
end
alias to_h to_hash
private