lib/representable/serializer.rb in representable-2.3.0 vs lib/representable/serializer.rb in representable-2.4.0.rc1

- old
+ new

@@ -1,60 +1,54 @@ -require "representable/deserializer" - module Representable - # serialize -> serialize! -> marshal. # TODO: same flow in deserialize. - class Serializer < Deserializer - def call(object, &block) - return object if object.nil? # DISCUSS: move to Object#serialize ? + Getter = ->(input, options) do + options[:binding].evaluate_option(:getter, input, options) + end - serialize(object, @binding.user_options, &block) - end + Get = ->(input, options) { options[:binding].send(:exec_context, options).send(options[:binding].getter) } - private - def serialize(object, user_options, &block) - return yield if @binding.evaluate_option(:skip_render, object) # this will jump out of #render_fragment. introduce Skip object here. + Writer = ->(input, options) do + options[:binding].evaluate_option(:writer, input, options) + Pipeline::Stop + end - serialize!(object, user_options) - end + # TODO: evaluate this, if we need this. + RenderDefault = ->(input, options) do + binding = options[:binding] - # Serialize one object by calling to_json etc. on it. - def serialize!(object, user_options) - object = prepare(object) + binding.skipable_empty_value?(input) ? binding[:default] : input + end - return object unless @binding.representable? + StopOnSkipable = ->(input, options) do + options[:binding].send(:skipable_empty_value?, input) ? Pipeline::Stop : input + end - @binding.evaluate_option(:serialize, object) do - marshal(object, user_options) - end - end + RenderFilter = ->(input, options) do + options[:binding][:render_filter].(input, options) + end - # 0.33 0.004 0.004 0.000 0.000 20001 Hash#merge! - # 0.00 0.000 0.000 0.000 0.000 1 Hash#merge! - def marshal(object, user_options) - object.send(@binding.serialize_method, user_options) - end + SkipRender = ->(input, options) do + options[:binding].evaluate_option(:skip_render, input, options) ? Pipeline::Stop : input + end + Serializer = ->(input, options) do + return if input.nil? # DISCUSS: how can we prevent that? - class Collection < self - def serialize(array, *args) - collection = [] # TODO: unify with Deserializer::Collection. + options[:binding].evaluate_option(:serialize, input, options) + end - array.each do |item| - next if @binding.evaluate_option(:skip_render, item) # TODO: allow skipping entire collections? same for deserialize. + Serialize = ->(input, options) do + return if input.nil? # DISCUSS: how can we prevent that? + binding, user_options = options[:binding], options[:user_options] - collection << serialize!(item, *args) - end # TODO: i don't want Array but Forms here - what now? + user_options = user_options.merge(wrap: binding[:wrap]) unless binding[:wrap].nil? # DISCUSS: can we leave that here? - collection - end - end + input.send(binding.serialize_method, user_options) + end + WriteFragment = ->(input, options) { options[:binding].write(options[:doc], input, options[:as]) } - class Hash < self - def serialize(hash, *args) - {}.tap do |hsh| - hash.each { |key, obj| hsh[key] = super(obj, *args) } - end - end - end - end + As = ->(input, options) { options[:binding].evaluate_option(:as, input, options) } + + # Warning: don't rely on AssignAs/AssignName, i am not sure if i leave that as functions. + AssignAs = ->(input, options) { options[:as] = As.(input, options); input } + AssignName = ->(input, options) { options[:as] = options[:binding].name; input } end \ No newline at end of file