lib/representable.rb in representable-2.2.3 vs lib/representable.rb in representable-2.3.0
- old
+ new
@@ -12,12 +12,12 @@
require "representable/deserializer"
require "representable/serializer"
require "representable/cached"
-require 'uber/callable'
-require 'representable/pipeline'
+require "uber/callable"
+require "representable/pipeline"
module Representable
attr_writer :representable_attrs
def self.included(base)
@@ -34,36 +34,43 @@
end
private
# Reads values from +doc+ and sets properties accordingly.
def update_properties_from(doc, options, format)
- private_options = normalize_options!(options)
+ propagated_options, private_options = normalize_options!(options)
- representable_mapper(format, options).deserialize(represented, doc, options, private_options)
+ representable_mapper(format, propagated_options).deserialize(represented, doc, propagated_options, private_options)
end
# Compiles the document going through all properties.
def create_representation_with(doc, options, format)
- private_options = normalize_options!(options)
+ propagated_options, private_options = normalize_options!(options)
- representable_mapper(format, options).serialize(represented, doc, options, private_options)
+ representable_mapper(format, propagated_options).serialize(represented, doc, propagated_options, private_options)
end
def representable_bindings_for(format, options)
representable_attrs.collect {|definition| representable_binding_for(definition, format, options) }
end
def representable_binding_for(definition, format, options)
format.build(definition, self)
end
+ # Make sure we do not change original options. However, private options like :include or :wrap are
+ # not passed on to child representers.
def normalize_options!(options)
- # TODO: ideally, private_options would be nil if none set or so, so we could save a lot of time in nested objects.
+ # here, we could also filter out local options e.g. like options[:band].
private_options = {}
- # return private_options if options.size == 0
- private_options[:include] = options.delete(:include) if options[:include]
- private_options[:exclude] = options.delete(:exclude) if options[:exclude]
- private_options
+ return [options, private_options] if options.size == 0
+
+ propagated_options = options.dup
+
+ private_options[:include] = propagated_options.delete(:include) if options[:include]
+ private_options[:exclude] = propagated_options.delete(:exclude) if options[:exclude]
+ propagated_options.delete(:wrap) # FIXME.
+
+ [propagated_options, private_options]
end
def representable_attrs
@representable_attrs ||= self.class.representable_attrs # DISCUSS: copy, or better not? what about "freezing"?
end
\ No newline at end of file