module ActiveFacet::Serializer::Base::ClassMethods

Public Instance Methods

config() click to toggle source

Memoized class getter @return [Config]

# File lib/active_facet/serializer/base.rb, line 90
def config
  @config ||= ActiveFacet::Config.new
end
expose(field_set_name, options = {}) click to toggle source

DSL Defines an alias that can be used instead of a Field Set @param field_set_name [Symbol] the alias name @param options [Hash] @option as [MIXED] a nested field_set collection

# File lib/active_facet/serializer/base.rb, line 54
def expose(field_set_name, options = {})
  field_set_name = field_set_name.to_sym
  raise ActiveFacet::Errors::ConfigurationError.new(ActiveFacet::Errors::ConfigurationError::ALL_ATTRIBUTES_ERROR_MSG) if field_set_name == :all_attributes
  raise ActiveFacet::Errors::ConfigurationError.new(ActiveFacet::Errors::ConfigurationError::ALL_FIELDS_ERROR_MSG) if field_set_name == :all
  config.alias_field_set(field_set_name, options.key?(:as) ? options[:as] : field_set_name)
end
expose_timestamps() click to toggle source

DSL Defines an alias for common ActiveRecord attributes

# File lib/active_facet/serializer/base.rb, line 62
def expose_timestamps
  transform :created_at, with: :time
  transform :updated_at, with: :time
  expose :timestamps, as: [:id, :created_at, :updated_at]
end
extension(api_attribute) click to toggle source

DSL Defines an attribute extension available for decoration and serialization @param api_attribute [Symbol] name of the attribute

# File lib/active_facet/serializer/base.rb, line 44
def extension(api_attribute)
  config.extensions[api_attribute] = true
  config.serializers[api_attribute] = api_attribute.to_sym
  expose api_attribute
end
new() click to toggle source

Singleton @return [Serializer::Base]

Calls superclass method
# File lib/active_facet/serializer/base.rb, line 79
def new
  @instance ||= super
end
resource_class(klass) click to toggle source

DSL Registers the class type to be serialized

# File lib/active_facet/serializer/base.rb, line 69
def resource_class(klass)
  config.resource_class = klass
end
transform(api_attribute, options = {}) click to toggle source

DSL Defines a transform to rename or reformat an attribute @param api_attribute [Symbol] name of the attribute @param options [Hash] @option options [Symbol] :as internal method name to call on the resource for serialization and hydration @option options [Symbol] :from internal method name to call on the resource for serialization @option options [Symbol] :to internal method name to call on the resource for hydration @option options [Symbol] :with name of a CustomAttributeSerializer Class for serialization and hydration @option options [Symbol] :within name of nested json attribute to serialize/hyrdrate within

# File lib/active_facet/serializer/base.rb, line 24
def transform(api_attribute, options = {})
  if options[:as].present?
    config.transforms_from[api_attribute]  = options[:as]
    config.transforms_to[api_attribute] = options[:as]
  end
  if options[:from].present?
    config.transforms_from[api_attribute] = options[:from]
    config.transforms_to[api_attribute] ||= options[:from]
  end
  if options[:to].present?
    config.transforms_from[api_attribute] ||= options[:to]
    config.transforms_to[api_attribute] = options[:to]
  end
  config.serializers[api_attribute] = options[:with]    if options[:with].present?
  config.namespaces[api_attribute]  = options[:within]  if options[:within].present?
  expose api_attribute
end