class AMA::Entity::Mapper::Type::Attribute

Stores data about single type attribute

Attributes

aliases[RW]

@!attribute aliases

@return [Array<Symbol>]
default[RW]

Default value that is set on automatic object creation.

@!attribute default

@return [Object]
name[RW]

@!attribute

@return [Symbol]
nullable[RW]

Whether or not this attribute may be represented by null.

@!attribute nullable

@return [TrueClass, FalseClass]
owner[RW]

@!attribute

@return [AMA::Entity::Mapper::Type]
sensitive[RW]

If set to true, this attribute will be omitted during normalization and won't be present in resulting structure.

@!attribute sensitive

@return [TrueClass, FalseClass]
types[RW]

@!attribute types List of possible types attribute may take

@return [Array<AMA::Entity::Mapper::Type>]
values[RW]

List of values this attribute acceptable to take. Part of automatic validation.

@!attribute values

@return [Array<Object>]
virtual[RW]

If attribute is declared as virtual, it is omitted from all automatic actions, such enumeration, normalization and denormalization. Main motivation behind virtual attributes was collections problem: collection can't be represented as hash of attributes, however, virtual attribute may describe collection content.

@!attribute virtual

@return [TrueClass, FalseClass]

Public Class Methods

defaults() click to toggle source
# File lib/ama-entity-mapper/type/attribute.rb, line 74
def self.defaults
  {
    virtual: false,
    sensitive: false,
    default: nil,
    nullable: false,
    values: [],
    validator: nil,
    aliases: []
  }
end
new(owner, name, *types, **options) click to toggle source

@param [Mapper::Type] owner @param [Symbol] name @param [Array<Mapper::Type>] types @param [Hash<Symbol, Object] options

# File lib/ama-entity-mapper/type/attribute.rb, line 90
def initialize(owner, name, *types, **options)
  @owner = validate_owner!(owner)
  @name = validate_name!(name)
  @types = validate_types!(types)
  self.class.defaults.each do |key, value|
    value = options.fetch(key, value)
    unless value.nil?
      set_object_attribute(self, key, options.fetch(key, value))
    end
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/ama-entity-mapper/type/attribute.rb, line 151
def ==(other)
  eql?(other)
end
eql?(other) click to toggle source
# File lib/ama-entity-mapper/type/attribute.rb, line 146
def eql?(other)
  return false unless other.is_a?(self.class)
  @owner == other.owner && @name == other.name
end
hash() click to toggle source
# File lib/ama-entity-mapper/type/attribute.rb, line 142
def hash
  @owner.hash ^ @name.hash
end
resolve_parameter(parameter, substitution) click to toggle source

@param [AMA::Entity::Mapper::Type] parameter @param [AMA::Entity::Mapper::Type] substitution @return [AMA::Entity::Mapper::Type::Attribute]

# File lib/ama-entity-mapper/type/attribute.rb, line 129
def resolve_parameter(parameter, substitution)
  clone.tap do |clone|
    clone.types = types.each_with_object([]) do |type, carrier|
      if type == parameter
        buffer = substitution
        buffer = [buffer] unless buffer.is_a?(Enumerable)
        next carrier.push(*buffer)
      end
      carrier.push(type.resolve_parameter(parameter, substitution))
    end
  end
end
resolved!(context = nil) click to toggle source
# File lib/ama-entity-mapper/type/attribute.rb, line 122
def resolved!(context = nil)
  types.each { |type| type.resolved!(context) }
end
resolved?() click to toggle source
# File lib/ama-entity-mapper/type/attribute.rb, line 118
def resolved?
  types.all?(&:resolved?)
end
to_def() click to toggle source
# File lib/ama-entity-mapper/type/attribute.rb, line 155
def to_def
  types = @types ? @types.map(&:to_def).join(', ') : 'none'
  message = "#{owner.type}.#{name}"
  message += ':virtual' if virtual
  "#{message}<#{types}>"
end
to_s() click to toggle source
# File lib/ama-entity-mapper/type/attribute.rb, line 162
def to_s
  message = "Attribute #{owner.type}.#{name}"
  message = "#{message} (virtual)" if virtual
  types = @types ? @types.map(&:to_def).join(', ') : 'none'
  "#{message} <#{types}>"
end
valid!(value, context) click to toggle source
# File lib/ama-entity-mapper/type/attribute.rb, line 110
def valid!(value, context)
  violations = self.violations(value, context)
  return if violations.empty?
  repr = violations.join(', ')
  message = "Attribute #{self} has failed validation: #{repr}"
  validation_error(message, context: context)
end
valid?(value, context) click to toggle source
# File lib/ama-entity-mapper/type/attribute.rb, line 106
def valid?(value, context)
  violations(value, context).empty?
end
violations(value, context) click to toggle source
# File lib/ama-entity-mapper/type/attribute.rb, line 102
def violations(value, context)
  validator.validate(value, self, context)
end

Private Instance Methods

validate_name!(name) click to toggle source
# File lib/ama-entity-mapper/type/attribute.rb, line 178
def validate_name!(name)
  return name if name.is_a?(Symbol)
  message = "Provided name has to be Symbol, #{name.class} received"
  compliance_error(message)
end
validate_owner!(owner) click to toggle source
# File lib/ama-entity-mapper/type/attribute.rb, line 171
def validate_owner!(owner)
  return owner if owner.is_a?(Type)
  message = 'Provided owner has to be a Type instance,' \
    " #{owner.class} received"
  compliance_error(message)
end
validate_types!(types) click to toggle source
# File lib/ama-entity-mapper/type/attribute.rb, line 184
def validate_types!(types)
  compliance_error("No types provided for #{self}") if types.empty?
  types.each do |type|
    next if type.is_a?(Type) || type.is_a?(Parameter)
    message = 'Provided type has to be a Type instance, ' \
      "#{type.class} received"
    compliance_error(message)
  end
end