class AdequateSerializer::Base

Attributes

_associations[RW]
_attributes[RW]
includes[RW]
object[RW]
root[RW]
scope[RW]

Public Class Methods

associations(*associations) click to toggle source
# File lib/adequate_serializer/base.rb, line 26
def self.associations(*associations)
  associations.each do |association|
    @_associations << association
  end
end
attributes(*attributes) click to toggle source
# File lib/adequate_serializer/base.rb, line 18
def self.attributes(*attributes)
  attributes.each do |attribute|
    @_attributes << attribute

    define_method_for(attribute)
  end
end
inherited(base) click to toggle source
# File lib/adequate_serializer/base.rb, line 13
def self.inherited(base)
  base._associations = (_associations || []).dup
  base._attributes = (_attributes || []).dup
end
new(object, options = {}) click to toggle source
# File lib/adequate_serializer/base.rb, line 34
def initialize(object, options = {})
  @object = object
  @includes = options[:includes]
  @root = options[:root]
  @scope = options[:scope]
end

Private Class Methods

define_method_for(attr) click to toggle source
# File lib/adequate_serializer/base.rb, line 65
def self.define_method_for(attr)
  define_method(attr) do
    object && object.read_attribute_for_serialization(attr)
  end
end

Public Instance Methods

as_json(options = {}) click to toggle source
# File lib/adequate_serializer/base.rb, line 41
def as_json(options = {})
  hash = attributes
  hash.merge!(associations)

  if root == false
    hash
  else
    { root_name => hash }
  end
end
associations() click to toggle source
# File lib/adequate_serializer/base.rb, line 59
def associations
  serialize_multiple_associations(normalized_associations)
end
attributes() click to toggle source
# File lib/adequate_serializer/base.rb, line 52
def attributes
  self.class._attributes.each_with_object({}) do |name, hash|
    clean_name = clean_name(name)
    hash[clean_name] = send(name)
  end
end

Private Instance Methods

associated_objects(association_key) click to toggle source
# File lib/adequate_serializer/base.rb, line 108
def associated_objects(association_key)
  if respond_to?(association_key)
    send(association_key)
  else
    object.send(association_key)
  end
end
clean_name(name) click to toggle source
# File lib/adequate_serializer/base.rb, line 75
def clean_name(name)
  name = name.to_s.gsub(/\?\Z/, '')
  name.to_sym
end
normalized_associations() click to toggle source
# File lib/adequate_serializer/base.rb, line 80
def normalized_associations
  all_associations = Array(includes) | self.class._associations

  all_associations.each_with_object({}) do |(key, value), hash|
    if key.is_a?(Hash)
      hash.merge!(key)
    else
      hash[key] ||= value
    end
  end
end
root_name() click to toggle source
# File lib/adequate_serializer/base.rb, line 71
def root_name
  (root || object.class.name.underscore.parameterize).to_sym
end
serialize_association(association_key, includes) click to toggle source
# File lib/adequate_serializer/base.rb, line 98
def serialize_association(association_key, includes)
  associated_objects = associated_objects(association_key)

  unless associated_objects.nil?
    serialize(
      associated_objects, root: false, includes: includes, scope: scope
    )
  end
end
serialize_multiple_associations(associations) click to toggle source
# File lib/adequate_serializer/base.rb, line 92
def serialize_multiple_associations(associations)
  associations.each_with_object({}) do |(key, includes), hash|
    hash[key] = serialize_association(key, includes)
  end
end