lib/conglomerate/tree_serializer.rb in conglomerate-0.10.0 vs lib/conglomerate/tree_serializer.rb in conglomerate-0.11.0

- old
+ new

@@ -7,14 +7,25 @@ def serialize(item = object) if Conglomerate::Particle === item serialize_particle(item) elsif Conglomerate::Array === item serialize_array(item) - elsif Array === item + elsif ::Array === item serialize_array(item) elsif item.is_a?(Numeric) || item.is_a?(String) || item.nil? item + elsif item.respond_to?(:to_s) + case item + when DateTime + item.to_time.utc.iso8601.sub(/\+00:00$/, "Z") + when Time + item.utc.iso8601.sub(/\+00:00$/, "Z") + when Date + item.strftime("%Y-%m-%d") + else + item + end end end private @@ -24,11 +35,11 @@ attributes = particle.class.attributes attributes.inject({}) do |hash, (attr, attr_metadata)| hash.tap do |h| attribute = particle.send(attr) - attribute ||= attr_metadata[:default] + attribute = attr_metadata[:default] if attribute == nil unless attr_metadata[:cull] && cull_attribute(attribute) h[attr.to_s] = serialize(attribute) end end @@ -40,9 +51,9 @@ serialize(item) end end def cull_attribute(attribute) - !attribute || (Conglomerate::Array === attribute && attribute.empty?) + (attribute == nil) || (Conglomerate::Array === attribute && attribute.empty?) end end end