class ScopedSerializer::Serializer

Attributes

default_scope[RW]
scopes[RW]
options[R]
resource[R]
scope[R]

Public Class Methods

find_scope(name) click to toggle source

Finds a defined scope by name.

# File lib/scoped_serializer/serializer.rb, line 43
def find_scope(name)
  self.scopes ||= {}
  self.scopes[name] || self.default_scope || Scope.new(:default)
end
inherited(base) click to toggle source

Set default values.

# File lib/scoped_serializer/serializer.rb, line 11
def inherited(base)
  base.scopes = {}

  if scopes.present?
    # Inheritance from a serializer that has scoped defined
    scopes.each do |name, scope|
      base.scopes[name] = Scope.new(name, scope)
    end

    base.default_scope = base.find_scope(:default)
  else
    # Nothing to inherit, set defaults
    base.default_scope = Scope.new(:default)
    base.scopes = { :default => base.default_scope }
  end
end
new(resource, options={}) click to toggle source
# File lib/scoped_serializer/serializer.rb, line 64
def initialize(resource, options={})
  @resource = resource
  @options  = options || {}
  scope = options[:scope] || :default

  if scope.is_a?(Symbol)
    @scope_name = scope
    @scope = self.class.find_scope(scope)
  elsif scope.is_a?(Hash)
    @scope_name = :custom
    @scope = Scope.from_hash(scope)
  else
    @scope_name = scope.name
    @scope = scope
  end

  set_scope(@scope)

  # Inherit options from scope
  @options = {}.merge(@scope.options).merge(@options)

  if @resource
    @options[:root] = default_root_key(@resource.class) unless @options.key?(:root)
  end
end
scope(name, &block) click to toggle source

Defines a scope. In this scope all scopes methods are available. See {ScopedSerializer::Scope}

@example

scope :resource do
  association :notes, :employee
end
# File lib/scoped_serializer/serializer.rb, line 36
def scope(name, &block)
  self.scopes[name] = Scope.new(name, self.default_scope, &block)
end

Public Instance Methods

associations_hash() click to toggle source

Collects associations for serialization. Associations can be overwritten in the serializer.

@return [Hash]

# File lib/scoped_serializer/serializer.rb, line 116
def associations_hash
  hash = {}
  @scope.associations.each do |association, options|
    hash.merge!(render_association(association, options))
  end
  hash
end
attributes_hash() click to toggle source

Collects attributes for serialization. Attributes can be overwritten in the serializer.

@return [Hash]

# File lib/scoped_serializer/serializer.rb, line 96
def attributes_hash
  attributes = @scope.attributes.collect do |attr|
    value = fetch_property(attr)

    if value.kind_of?(BigDecimal)
      value = value.to_f
    end

    [attr, value]
  end

  Hash[attributes]
end
fetch_association(name, includes=nil) click to toggle source

Fetches association and eager loads data. Doesn't eager load when includes is empty or when the association has already been loaded.

@example

fetch_association(:comments, :user)
# File lib/scoped_serializer/serializer.rb, line 182
def fetch_association(name, includes=nil)
  association = fetch_property(name)

  if includes.present? && ! @resource.association(name).loaded?
    association.includes(includes)
  else
    association
  end
end
fetch_property(property) click to toggle source

Fetches property from the serializer or resource. This method makes it possible to overwrite defined attributes or associations.

# File lib/scoped_serializer/serializer.rb, line 165
def fetch_property(property)
  return nil unless property

  unless respond_to?(property)
    object = @resource.send(property)
  else
    object = send(property)
  end
end
render_association(association_data, options={}) click to toggle source

Renders a specific association.

@return [Hash]

@example

render_association(:employee)
render_association([:employee, :company])
render_association({ :employee => :address })
# File lib/scoped_serializer/serializer.rb, line 134
def render_association(association_data, options={})
  hash = {}

  if association_data.is_a?(Hash)
    association_data.each do |association, association_options|
      data = render_association(association, options.merge(:include => association_options))
      hash.merge!(data) if data
    end
  elsif association_data.is_a?(Array)
    association_data.each do |option|
      data = render_association(option)
      hash.merge!(data) if data
    end
  else
    if options[:preload]
      includes = options[:preload] == true ? options[:include] : options[:preload]
    end

    if (object = fetch_association(association_data, includes))
      data = ScopedSerializer.for(object, options.merge(:associations => options[:include])).as_json
      hash.merge!(data) if data
    end
  end

  hash
end
serializable_hash(options={}) click to toggle source

The serializable hash returned.

# File lib/scoped_serializer/serializer.rb, line 195
def serializable_hash(options={})
  {}.merge(attributes_hash).merge(associations_hash)
end