mirror of
https://github.com/ditkrg/jsonapi-deserializable.git
synced 2026-01-22 22:06:47 +00:00
Cleanup. (#2)
This commit is contained in:
parent
446102275c
commit
a816fa56dd
@ -24,23 +24,25 @@ module JSONAPI
|
|||||||
deserialize!
|
deserialize!
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_h
|
def to_hash
|
||||||
@hash
|
@hash
|
||||||
end
|
end
|
||||||
|
alias to_h to_hash
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def deserialize!
|
def deserialize!
|
||||||
@hash = {}
|
@hash = {}
|
||||||
|
return unless @document.key?('data')
|
||||||
if @data.is_a?(Array)
|
if @data.is_a?(Array)
|
||||||
deserialize_has_many!
|
deserialize_has_many!
|
||||||
else
|
elsif @data.nil? || @data.is_a?(Hash)
|
||||||
deserialize_has_one!
|
deserialize_has_one!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def deserialize_has_one!
|
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']
|
id = @data && @data['id']
|
||||||
type = @data && @data['type']
|
type = @data && @data['type']
|
||||||
instance_exec(@document, id, type, &self.class.has_one_block)
|
instance_exec(@document, id, type, &self.class.has_one_block)
|
||||||
|
|||||||
@ -6,13 +6,12 @@ module JSONAPI
|
|||||||
include ResourceDSL
|
include ResourceDSL
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
attr_accessor :type_block, :id_block
|
attr_accessor :type_block, :id_block, :attr_blocks,
|
||||||
attr_accessor :attr_blocks
|
:has_one_rel_blocks, :has_many_rel_blocks
|
||||||
attr_accessor :has_one_rel_blocks, :has_many_rel_blocks
|
|
||||||
end
|
end
|
||||||
|
|
||||||
self.attr_blocks = {}
|
self.attr_blocks = {}
|
||||||
self.has_one_rel_blocks = {}
|
self.has_one_rel_blocks = {}
|
||||||
self.has_many_rel_blocks = {}
|
self.has_many_rel_blocks = {}
|
||||||
|
|
||||||
def self.inherited(klass)
|
def self.inherited(klass)
|
||||||
@ -38,9 +37,10 @@ module JSONAPI
|
|||||||
deserialize!
|
deserialize!
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_h
|
def to_hash
|
||||||
@hash
|
@hash
|
||||||
end
|
end
|
||||||
|
alias to_h to_hash
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user