module HALPresenter::Deserializer
Public Class Methods
included(base)
click to toggle source
# File lib/hal_presenter/deserializer.rb, line 14 def self.included(base) base.extend ClassMethods end
Public Instance Methods
from_hal(payload, resource = nil)
click to toggle source
# File lib/hal_presenter/deserializer.rb, line 18 def from_hal(payload, resource = nil) return if payload.nil? || payload.empty? hash = JSON.parse(payload) from_hash(hash, resource) end
Protected Instance Methods
deserialize_attributes(hash, resource)
click to toggle source
# File lib/hal_presenter/deserializer.rb, line 46 def deserialize_attributes(hash, resource) attributes.each do |attribute| setter_method = setter_method_name(attribute.name) next unless resource.respond_to? setter_method resource.public_send(setter_method, hash[attribute.name.to_s]) end end
deserialize_collection(hash, resource)
click to toggle source
# File lib/hal_presenter/deserializer.rb, line 74 def deserialize_collection(hash, resource) hash['_embedded'][collection_properties.name].each do |resource_hash| resource << from_hash(resource_hash, nil) end end
deserialize_embedded(hash, resource)
click to toggle source
# File lib/hal_presenter/deserializer.rb, line 54 def deserialize_embedded(hash, resource) embedded.each do |embed| setter_method = setter_method_name(embed.name) or next next unless resource.respond_to? setter_method presenter = embed.presenter_class or next _embedded = hash.dig('_embedded', embed.name.to_s) next unless _embedded&.any? embedded_resource = resource.public_send(embed.name) embedded_resource = if Array === _embedded _embedded.map { |h| presenter.from_hash(h, embedded_resource) } else presenter.from_hash(_embedded, embedded_resource) end resource.public_send(setter_method, embedded_resource) end end
from_hash(hash, resource)
click to toggle source
# File lib/hal_presenter/deserializer.rb, line 26 def from_hash(hash, resource) as_collection = deserialize_as_collection?(hash) if resource.nil? model = HALPresenter.lookup_model self raise Error, "No model for #{self.class}" unless model resource = as_collection ? [] : model.new elsif as_collection resource.clear end if as_collection deserialize_collection(hash, resource) else deserialize_attributes(hash, resource) deserialize_embedded(hash, resource) end resource end
Private Instance Methods
deserialize_as_collection?(hash)
click to toggle source
# File lib/hal_presenter/deserializer.rb, line 82 def deserialize_as_collection?(hash) name = collection_properties&.name # return true/false (Hash#key? returns nil if not found..) name && hash['_embedded']&.key?(name) || false end
setter_method_name(attr)
click to toggle source
# File lib/hal_presenter/deserializer.rb, line 88 def setter_method_name(attr) "#{attr}=".to_sym end