module Bronze::Entities::Normalization
Module for transforming entities to and from a normal form.
Public Instance Methods
normalize(permit: [])
click to toggle source
Returns a normalized representation of the entity. The normal representation of an entity is a hash with String keys. Each value must be nil, a literal value (true, false, a String, an Integer, a Float, etc), an Array of normal values, or a Hash with String keys and normal values.
@param [Array<Class>] permit 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.
@return [Hash] The normal representation.
# File lib/bronze/entities/normalization.rb, line 49 def normalize(permit: []) self.class.each_attribute.with_object({}) do |(name, metadata), hsh| value = get_attribute(name) value = normalize_attribute(value, metadata: metadata, permit: permit) hsh[name.to_s] = value end end
Private Instance Methods
normalize_attribute(value, metadata:, permit:)
click to toggle source
# File lib/bronze/entities/normalization.rb, line 60 def normalize_attribute(value, metadata:, permit:) return value unless metadata.transform? return value if metadata.default_transform? && Array(permit).any? { |type| metadata.type <= type } metadata.transform.normalize(value) end