class ActiveModel::Csverizer
Constants
- EMBED_IN_ROOT_OPTIONS
- VERSION
Attributes
_associations[RW]
_attributes[RW]
_root[RW]
key_format[R]
root[RW]
root=[RW]
context[RW]
key_format[RW]
meta[RW]
meta_key[RW]
object[RW]
polymorphic[RW]
root[RW]
scope[RW]
serialization_options[W]
Public Class Methods
attributes(*attrs)
click to toggle source
# File lib/active_model/csverizer.rb, line 79 def attributes(*attrs) attrs.each do |attr| striped_attr = strip_attribute attr @_attributes << striped_attr define_method striped_attr do object.read_attribute_for_serialization attr end unless method_defined?(attr) end end
embed(type, options={})
click to toggle source
# File lib/active_model/csverizer.rb, line 27 def embed(type, options={}) CONFIG.embed = type if EMBED_IN_ROOT_OPTIONS.any? { |opt| options[opt].present? } CONFIG.embed_in_root = true end if options[:embed_in_root_key].present? CONFIG.embed_in_root_key = options[:embed_in_root_key] end ActiveSupport::Deprecation.warn <<-WARN ** Notice: embed is deprecated. ** The use of .embed method on a Serializer will be soon removed, as this should have a global scope and not a class scope. Please use the global .setup method instead: ActiveModel::Serializer.setup do |config| config.embed = :#{type} config.embed_in_root = #{CONFIG.embed_in_root || false} end WARN end
format_keys(format)
click to toggle source
# File lib/active_model/csverizer.rb, line 46 def format_keys(format) @key_format = format end
has_many(*attrs)
click to toggle source
# File lib/active_model/csverizer.rb, line 95 def has_many(*attrs) associate(Association::HasMany, *attrs) end
has_one(*attrs)
click to toggle source
# File lib/active_model/csverizer.rb, line 91 def has_one(*attrs) associate(Association::HasOne, *attrs) end
inherited(base)
click to toggle source
# File lib/active_model/csverizer.rb, line 8 def inherited(base) base._root = _root base._attributes = (_attributes || []).dup base._associations = (_associations || {}).dup end
new(object, options={})
click to toggle source
# File lib/active_model/csverizer.rb, line 134 def initialize(object, options={}) @object = object @scope = options[:scope] @root = options.fetch(:root, self.class._root) @polymorphic = options.fetch(:polymorphic, false) @meta_key = options[:meta_key] || :meta @meta = options[@meta_key] @wrap_in_array = options[:_wrap_in_array] @only = options[:only] ? Array(options[:only]) : nil @except = options[:except] ? Array(options[:except]) : nil @key_format = options[:key_format] @context = options[:context] @namespace = options[:namespace] @prefix = options.fetch(:prefix, '') end
root_name()
click to toggle source
# File lib/active_model/csverizer.rb, line 72 def root_name if name root_name = name.demodulize.underscore.sub(/_serializer$/, '') CONFIG.plural_default_root ? root_name.pluralize : root_name end end
serializer_for(resource, options = {})
click to toggle source
# File lib/active_model/csverizer.rb, line 51 def serializer_for(resource, options = {}) if resource.respond_to?(:serializer_class) resource.serializer_class elsif resource.respond_to?(:to_ary) if Object.constants.include?(:ArraySerializer) ::ArraySerializer else ArraySerializer end else klass_name = build_serializer_class(resource, options) Serializer.serializers_cache.fetch_or_store(klass_name) do _const_get(klass_name) end end end
serializers_cache()
click to toggle source
# File lib/active_model/csverizer.rb, line 99 def serializers_cache @serializers_cache ||= Concurrent::Map.new end
setup() { |CONFIG| ... }
click to toggle source
# File lib/active_model/csverizer.rb, line 14 def setup @mutex.synchronize do yield CONFIG end end
Private Class Methods
associate(klass, *attrs)
click to toggle source
# File lib/active_model/csverizer.rb, line 121 def associate(klass, *attrs) options = attrs.extract_options! attrs.each do |attr| define_method attr do object.send attr end unless method_defined?(attr) @_associations[attr] = klass.new(attr, options) end end
build_serializer_class(resource, options)
click to toggle source
# File lib/active_model/csverizer.rb, line 113 def build_serializer_class(resource, options) "".tap do |klass_name| klass_name << "#{options[:namespace]}::" if options[:namespace] klass_name << options[:prefix].to_s.classify if options[:prefix] klass_name << "#{resource.class.name}Serializer" end end
strip_attribute(attr)
click to toggle source
# File lib/active_model/csverizer.rb, line 105 def strip_attribute(attr) symbolized = attr.is_a?(Symbol) attr = attr.to_s.gsub(/\?\Z/, '') attr = attr.to_sym if symbolized attr end
Public Instance Methods
associated_serializers()
click to toggle source
# File lib/active_model/csverizer.rb, line 348 def associated_serializers @associated_serializers ||= self.class._associations.collect do |hash| object = read_attribute(hash[:associated]) hash[:serializer].new(object, prefix: hash[:associated].to_s + '_') end end
association_options_for_serializer(association)
click to toggle source
# File lib/active_model/csverizer.rb, line 238 def association_options_for_serializer(association) prefix = association.options[:prefix] namespace = association.options[:namespace] || @namespace || self.namespace { scope: scope }.tap do |opts| opts[:namespace] = namespace if namespace opts[:prefix] = prefix if prefix end end
associations(options={})
click to toggle source
# File lib/active_model/csverizer.rb, line 167 def associations(options={}) associations = self.class._associations included_associations = filter(associations.keys) associations.each_with_object({}) do |(name, association), hash| if included_associations.include? name if association.embed_ids? ids = serialize_ids association if association.embed_namespace? hash = hash[association.embed_namespace] ||= {} hash[association.key] = ids else hash[association.key] = ids end elsif association.embed_objects? if association.embed_namespace? hash = hash[association.embed_namespace] ||= {} end hash[association.embedded_key] = serialize association, options end end end end
attribute_names()
click to toggle source
# File lib/active_model/csverizer.rb, line 330 def attribute_names names = self.class._attributes.collect do |attribute| @prefix + attribute.to_s end associated_serializers.reduce(names) do |names, serializer| names.concat serializer.attribute_names end end
attributes()
click to toggle source
# File lib/active_model/csverizer.rb, line 161 def attributes filter(self.class._attributes.dup).each_with_object({}) do |name, hash| hash[name] = send(name) end end
build_serializer(association)
click to toggle source
# File lib/active_model/csverizer.rb, line 233 def build_serializer(association) object = send(association.name) association.build_serializer(object, association_options_for_serializer(association)) end
convert_keys(hash)
click to toggle source
# File lib/active_model/csverizer.rb, line 273 def convert_keys(hash) Hash[hash.map do |k,v| key = if k.is_a?(Symbol) format_key(k).to_sym else format_key(k) end [key ,v] end] end
embedded_in_root_associations()
click to toggle source
# File lib/active_model/csverizer.rb, line 200 def embedded_in_root_associations associations = self.class._associations included_associations = filter(associations.keys) associations.each_with_object({}) do |(name, association), hash| if included_associations.include? name association_serializer = build_serializer(association) # we must do this always because even if the current association is not # embedded in root, it might have its own associations that are embedded in root hash.merge!(association_serializer.embedded_in_root_associations) do |key, oldval, newval| if oldval.respond_to?(:to_ary) [oldval, newval].flatten.uniq else oldval.merge(newval) { |_, oldval, newval| [oldval, newval].flatten.uniq } end end if association.embed_in_root? if association.embed_in_root_key? hash = hash[association.embed_in_root_key] ||= {} end serialized_data = association_serializer.serializable_object key = association.root_key if hash.has_key?(key) hash[key].concat(serialized_data).uniq! else hash[key] = serialized_data end end end end end
filter(keys)
click to toggle source
# File lib/active_model/csverizer.rb, line 190 def filter(keys) if @only keys & @only elsif @except keys - @except else keys end end
format_key(key)
click to toggle source
# File lib/active_model/csverizer.rb, line 265 def format_key(key) if key_format == :lower_camel key.to_s.camelize(:lower) else key end end
json_key()
click to toggle source
# File lib/active_model/csverizer.rb, line 151 def json_key key = if root == true || root.nil? self.class.root_name else root end key_format == :lower_camel && key.present? ? key.camelize(:lower) : key end
read_association(name)
click to toggle source
# File lib/active_model/csverizer.rb, line 344 def read_association(name) respond_to?(name) ? send(name) : object.send(name) end
read_attribute(name)
click to toggle source
# File lib/active_model/csverizer.rb, line 339 def read_attribute(name) return send(name) if respond_to?(name) object.read_attribute_for_serialization(name) end
serializable_object(options={})
click to toggle source
# File lib/active_model/csverizer.rb, line 290 def serializable_object(options={}) self.serialization_options = options return @wrap_in_array ? [] : nil if @object.nil? hash = attributes hash.merge! associations(options) hash = convert_keys(hash) if key_format.present? hash = { :type => type_name(@object), type_name(@object) => hash } if @polymorphic @wrap_in_array ? [hash] : hash end
Also aliased as: serializable_hash
serialization_options()
click to toggle source
# File lib/active_model/csverizer.rb, line 286 def serialization_options @serialization_options || {} end
serialize(association,options={})
click to toggle source
# File lib/active_model/csverizer.rb, line 248 def serialize(association,options={}) build_serializer(association).serializable_object(options) end
serialize_id(elem, association)
click to toggle source
# File lib/active_model/csverizer.rb, line 301 def serialize_id(elem, association) id = elem.read_attribute_for_serialization(association.embed_key) association.polymorphic? ? { id: id, type: type_name(elem) } : id end
serialize_ids(association)
click to toggle source
# File lib/active_model/csverizer.rb, line 252 def serialize_ids(association) associated_data = send(association.name) if associated_data.respond_to?(:to_ary) associated_data.map { |elem| serialize_id(elem, association) } else serialize_id(associated_data, association) if associated_data end end
to_a()
click to toggle source
# File lib/active_model/csverizer.rb, line 317 def to_a return [[]] unless @object values = [] values << self.class._attributes.collect { |name| read_attribute(name) } associated_serializers.each do |serializer| values = values.product(serializer.to_a).collect(&:flatten) end values end
to_csv()
click to toggle source
# File lib/active_model/csverizer.rb, line 310 def to_csv CSV.generate do |csv| csv << attribute_names to_a.each { |record| csv << record } end end
type_name(elem)
click to toggle source
# File lib/active_model/csverizer.rb, line 306 def type_name(elem) elem.class.to_s.demodulize.underscore.to_sym end