module SmoothOperator::Serialization::HelperMethods
Public Instance Methods
attribute_names(object, options)
click to toggle source
# File lib/smooth_operator/serialization.rb, line 45 def attribute_names(object, options) attribute_names = object.internal_data.keys.sort if only = options[:only] white_list = [*only].map(&:to_s) attribute_names &= white_list elsif except = options[:except] black_list = [*except].map(&:to_s) attribute_names -= black_list end attribute_names end
attribute_to_hash(object, options = nil)
click to toggle source
# File lib/smooth_operator/serialization.rb, line 112 def attribute_to_hash(object, options = nil) if object.respond_to?(:serializable_hash) Helpers.symbolyze_keys(object.serializable_hash(options)) else object end end
method_names(object, options)
click to toggle source
# File lib/smooth_operator/serialization.rb, line 61 def method_names(object, options) [*options[:methods]].select { |n| object.respond_to?(n) } end
serialize_has_many_attribute(parent_object, reflection, attribute_name, options)
click to toggle source
# File lib/smooth_operator/serialization.rb, line 84 def serialize_has_many_attribute(parent_object, reflection, attribute_name, options) return nil unless reflection.has_many? object = parent_object.read_attribute_for_serialization(attribute_name) object.reduce({}) do |hash, array_entry| id = Helpers.present?(array_entry.id) ? array_entry.id : Helpers.generated_id hash[id.to_s] = attribute_to_hash(array_entry, options) hash end end
serialize_normal_attribute(parent_object, attribute_name, options)
click to toggle source
# File lib/smooth_operator/serialization.rb, line 98 def serialize_normal_attribute(parent_object, attribute_name, options) if parent_object.respond_to?(:read_attribute_for_serialization) object = parent_object.read_attribute_for_serialization(attribute_name) else object = parent_object end if object.is_a?(Array) object.map { |array_entry| attribute_to_hash(array_entry, options) } else attribute_to_hash(object, options) end end
serialize_normal_or_rails_way(object, attribute_name, options)
click to toggle source
# File lib/smooth_operator/serialization.rb, line 65 def serialize_normal_or_rails_way(object, attribute_name, options) _attribute_name, attribute_sym = attribute_name, attribute_name.to_sym reflection = object.class.respond_to?(:reflect_on_association) && object.class.reflect_on_association(attribute_sym) attribute_options = options[attribute_sym] if reflection && reflection.rails_serialization? attribute_value = serialize_has_many_attribute(object, reflection, attribute_name, attribute_options) _attribute_name = "#{attribute_name}_attributes" end attribute_value ||= serialize_normal_attribute(object, attribute_name, attribute_options) [_attribute_name, attribute_value] end