Class: Cuprum::Rails::Serializers::Json::AttributesSerializer

Inherits:
PropertiesSerializer show all
Defined in:
lib/cuprum/rails/serializers/json/attributes_serializer.rb

Overview

Generates a JSON representation of the object’s attributes.

Defined properties are inherited from the parent serializer. This allows you to extend existing serializers with additional functionality.

Examples:

Serializing an attribute.

User = Struct.new(:first_name, :last_name, :salary, :department)
class FirstNameSerializer < Cuprum::Rails::Serializers::Json::AttributesSerializer
  attribute :first_name
end

user       = User.new('Alan', 'Bradley')
serializer = FirstNameSerializer.new
serializer.call(user, context: context)
#=> {
  'first_name' => 'Alan'
}

Using a custom serializer.

User = Struct.new(:first_name, :last_name, :salary, :department)
class SalarySerializer < Cuprum::Rails::Serializers::Json::AttributesSerializer
  attribute :salary, serializer: BigDecimalSerializer
end

user       = User.new('Alan', 'Bradley', BigDecimal('100000'))
serializer = SalarySerializer.new
serializer.call(user, context: context)
#=> {
  'salary' => '0.1e6'
}

Using a custom mapping.

User = Struct.new(:first_name, :last_name, :hire_date)
class HireDateSerializer < Cuprum::Rails::Serializers::Json::AttributesSerializer
  attribute(:hire_date) { |value| value&.iso8601 }
end

user       = User.new('Alan', 'Bradley', Date.new(1977, 5, 25))
serializer = HireDateSerializer.new
serializer.call(user, context: context)
#=> {
  'hire_date' => '1977-05-25'
}

Serializing multiple attributes.

User = Struct.new(:first_name, :last_name, :hire_date)
class UserSerializer < Cuprum::Rails::Serializers::Json::AttributesSerializer
  attributes \
    :first_name,
    :last_name,
    hire_date: :iso8601
end

user       = User.new('Alan', 'Bradley', Date.new(1977, 5, 25))
serializer = HireDateSerializer.new
serializer.call(user, context: context)
#=> {
  'first_name' => 'Alan',
  'last_name'  => 'Bradley',
  'hire_date'  => '1977-05-25'
}

See Also:

Class Method Summary collapse

Methods inherited from PropertiesSerializer

#call, properties, property

Methods inherited from BaseSerializer

#call, instance

Class Method Details

.attribute(name, serializer: nil) {|value| ... } ⇒ Object

Registers the attribute to be serialized.

Parameters:

  • name (String, Symbol)

    the name of the attribute to serialize. This will determine the hash key of the serialized value, as well as the base value to be serialized.

  • serializer (#call) (defaults to: nil)

    the serializer used to serialize the value. If no serializer is given, the default serializer (if any) will be used.

Yields:

  • If a block is given, the block is used to transform the attribute value prior to serialization.

Yield Parameters:

  • value (Object)

    the attribute value.

Yield Returns:

  • (Object)

    the transformed value.

Raises:

  • AbstractSerializerError when attempting to define a serialized property on an abstract class.



89
90
91
92
93
94
95
96
# File 'lib/cuprum/rails/serializers/json/attributes_serializer.rb', line 89

def attribute(name, serializer: nil, &block)
  property(
    name,
    scope:      name,
    serializer: serializer,
    &block
  )
end

.attributes(*attribute_names, **attribute_mappings) ⇒ Object

Registers the attributes to be serialized.

Parameters:

  • attribute_names (Array<String, Symbol>)

    the names of the attributes to serialize.

  • attribute_mappings (Hash{String, Symbol => #to_proc})

    the names and mappings of additional attributes to serialize.

Raises:

  • AbstractSerializerError when attempting to define a serialized property on an abstract class.



107
108
109
110
111
112
113
114
115
116
# File 'lib/cuprum/rails/serializers/json/attributes_serializer.rb', line 107

def attributes(*attribute_names, **attribute_mappings)
  require_concrete_class!

  validate_property_names!(*attribute_names, *attribute_mappings.keys)
  validate_property_mappings!(*attribute_mappings.values)

  attribute_names.each { |name| attribute(name) }

  attribute_mappings.each { |name, mapping| attribute(name, &mapping) }
end