module Served::Resource::Serializable::ClassMethods
Public Instance Methods
from_hash(hash)
click to toggle source
# File lib/served/resource/serializable.rb, line 34 def from_hash(hash) hash = hash.clone hash.each do |name, value| hash[name] = serialize_attribute(name, value) end hash.symbolize_keys end
load(string)
click to toggle source
# File lib/served/resource/serializable.rb, line 24 def load(string) begin result = serializer.load(self, string) rescue StandardError => e raise ResponseInvalid.new(self, e) end raise ResponseInvalid.new(self) unless result result end
Private Instance Methods
attribute_serializer_for(type)
click to toggle source
# File lib/served/resource/serializable.rb, line 44 def attribute_serializer_for(type) # case statement wont work here because of how it does class matching return ->(v) { return v } unless type # nil return ->(v) { return v.try(:to_i) } if type == Integer || type == Fixnum return ->(v) { return v.try(:to_s) } if type == String return ->(v) { return v.try(:to_sym) } if type == Symbol return ->(v) { return v.try(:to_f) } if type == Float return ->(v) { return v.try(:to_a) } if type == Array if type == Boolean return lambda do |v| return false unless v == "true" || v.is_a?(TrueClass) true end end if type.ancestors.include?(Served::Resource::Base) || type.ancestors.include?(Served::Attribute::Base) return ->(v) { type.new(v) } end raise InvalidAttributeSerializer.new(type) end
serialize_attribute(attr, value)
click to toggle source
# File lib/served/resource/serializable.rb, line 67 def serialize_attribute(attr, value) return false unless attributes[attr.to_sym] serializer = attribute_serializer_for(attributes[attr.to_sym][:serialize]) if value.is_a? Array # TODO: Remove the Array class check below in 1.0, only here # for backwards compatibility return value if attributes[attr.to_sym][:serialize].nil? || attributes[attr.to_sym][:serialize] == Array value.collect do |v| if v.is_a? attributes[attr.to_sym][:serialize] v else serializer.call(v) end end else serializer.call(value) end end