Class: Bronze::Transforms::Entities::NormalizeTransform

Inherits:
Bronze::Transform show all
Defined in:
lib/bronze/transforms/entities/normalize_transform.rb

Overview

Transform class that maps an entity to a normal representation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity_class, permit: []) ⇒ NormalizeTransform

Returns a new instance of NormalizeTransform.

Parameters:

  • entity_class (Class)

    The entity class to normalize.

  • permit (Array<Class>) (defaults to: [])

    An optional array of types to normalize as-is, rather than applying a transform. Only default transforms can be permitted, i.e. the built-in default transforms for BigDecimal, Date, DateTime, Symbol, and Time, or for an attribute with the :default_transform flag set to true.



15
16
17
18
# File 'lib/bronze/transforms/entities/normalize_transform.rb', line 15

def initialize(entity_class, permit: [])
  @entity_class    = entity_class
  @permitted_types = permit
end

Instance Attribute Details

#entity_classClass (readonly)

Returns the entity class to normalize.

Returns:

  • (Class)

    the entity class to normalize.



21
22
23
# File 'lib/bronze/transforms/entities/normalize_transform.rb', line 21

def entity_class
  @entity_class
end

#permitted_typesArray<Class> (readonly)

Returns array of types to normalize as-is.

Returns:

  • (Array<Class>)

    array of types to normalize as-is.



24
25
26
# File 'lib/bronze/transforms/entities/normalize_transform.rb', line 24

def permitted_types
  @permitted_types
end

Instance Method Details

#denormalize(attributes) ⇒ Bronze::Entity

Returns an entity instance.

Parameters:

  • attributes (Hash)

    The normal representation of an entity.

Returns:

See Also:

  • Bronze::Transforms::Entities::NormalizeTransform.[Bronze[Bronze::Entities[Bronze::Entities::Normalization[Bronze::Entities::Normalization::denormalize]


33
34
35
36
37
# File 'lib/bronze/transforms/entities/normalize_transform.rb', line 33

def denormalize(attributes)
  return nil if attributes.nil?

  entity_class.denormalize(attributes)
end

#normalize(entity) ⇒ Hash

Returns a normalized representation of the entity.

Parameters:

Returns:

  • (Hash)

    the normal representation.

See Also:

  • Bronze::Transforms::Entities::NormalizeTransform.[Bronze[Bronze::Entities[Bronze::Entities::Normalization[Bronze::Entities::Normalization#normalize]


46
47
48
49
50
# File 'lib/bronze/transforms/entities/normalize_transform.rb', line 46

def normalize(entity)
  return nil if entity.nil?

  entity.normalize(permit: permitted_types)
end