Module: ActiveFacets::Serializer::Base::ClassMethods

Defined in:
lib/active_facets/serializer/base.rb

Instance Method Summary collapse

Instance Method Details

#configConfig

Memoized class getter

Returns:



88
89
90
# File 'lib/active_facets/serializer/base.rb', line 88

def config
  @config ||= ActiveFacets::Config.new
end

#expose(field_set_name, options = {}) ⇒ Object

DSL Defines an alias that can be used instead of a Field Set

Parameters:

  • field_set_name (Symbol)

    the alias name

  • options (Hash) (defaults to: {})
  • as (Hash)

    a customizable set of options

Raises:



52
53
54
55
56
57
# File 'lib/active_facets/serializer/base.rb', line 52

def expose(field_set_name, options = {})
  field_set_name = field_set_name.to_sym
  raise ActiveFacets::Errors::ConfigurationError.new(ActiveFacets::Errors::ConfigurationError::ALL_ATTRIBUTES_ERROR_MSG) if field_set_name == :all_attributes
  raise ActiveFacets::Errors::ConfigurationError.new(ActiveFacets::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_timestampsObject

DSL Defines an alias for common ActiveRecord attributes



60
61
62
63
64
# File 'lib/active_facets/serializer/base.rb', line 60

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) ⇒ Object

DSL Defines an attribute extension available for decoration and serialization

Parameters:

  • api_attribute (Symbol)

    name of the attribute



42
43
44
45
46
# File 'lib/active_facets/serializer/base.rb', line 42

def extension(api_attribute)
  config.extensions[api_attribute] = true
  config.serializers[api_attribute] = api_attribute.to_sym
  expose api_attribute
end

#newSerializer::Base

Singleton

Returns:



77
78
79
# File 'lib/active_facets/serializer/base.rb', line 77

def new
  @instance ||= super
end

#resource_class(klass) ⇒ Object

DSL Registers the class type to be serialized



67
68
69
# File 'lib/active_facets/serializer/base.rb', line 67

def resource_class(klass)
  config.resource_class = klass
end

#transform(api_attribute, options = {}) ⇒ Object

DSL Defines a transform to rename or reformat an attribute

Parameters:

  • api_attribute (Symbol)

    name of the attribute

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :as (Symbol)

    internal method name to call on the resource for serialization and hydration

  • :from (Symbol)

    internal method name to call on the resource for serialization

  • :to (Symbol)

    internal method name to call on the resource for hydration

  • :with (Symbol)

    name of a CustomAttributeSerializer Class for serialization and hydration

  • :within (Symbol)

    name of nested json attribute to serialize/hyrdrate within



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_facets/serializer/base.rb', line 22

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