class AMA::Entity::Mapper::Type::Attribute
Stores data about single type attribute
Attributes
@!attribute aliases
@return [Array<Symbol>]
Default value that is set on automatic object creation.
@!attribute default
@return [Object]
@!attribute
@return [Symbol]
Whether or not this attribute may be represented by null.
@!attribute nullable
@return [TrueClass, FalseClass]
@!attribute
@return [AMA::Entity::Mapper::Type]
If set to true, this attribute will be omitted during normalization and won't be present in resulting structure.
@!attribute sensitive
@return [TrueClass, FalseClass]
@!attribute types List of possible types attribute may take
@return [Array<AMA::Entity::Mapper::Type>]
List of values this attribute acceptable to take. Part of automatic validation.
@!attribute values
@return [Array<Object>]
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
# 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
@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
# File lib/ama-entity-mapper/type/attribute.rb, line 151 def ==(other) eql?(other) end
# 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
# File lib/ama-entity-mapper/type/attribute.rb, line 142 def hash @owner.hash ^ @name.hash end
@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
# File lib/ama-entity-mapper/type/attribute.rb, line 122 def resolved!(context = nil) types.each { |type| type.resolved!(context) } end
# File lib/ama-entity-mapper/type/attribute.rb, line 118 def resolved? types.all?(&:resolved?) end
# 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
# 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
# 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
# File lib/ama-entity-mapper/type/attribute.rb, line 106 def valid?(value, context) violations(value, context).empty? end
# File lib/ama-entity-mapper/type/attribute.rb, line 102 def violations(value, context) validator.validate(value, self, context) end
Private Instance Methods
# 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
# 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
# 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