Class: AMA::Entity::Mapper::Type::Attribute

Inherits:
Object
  • Object
show all
Includes:
Mixin::Errors, Mixin::HandlerSupport, Mixin::Reflection
Defined in:
lib/ama-entity-mapper/type/attribute.rb

Overview

Stores data about single type attribute

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::HandlerSupport

declare_handler_block_setter, declare_handler_getter, declare_handler_method, declare_handler_setter, declare_namespace_method, included

Methods included from Mixin::Reflection

#install_object_method, #method_object, #object_variable, #object_variable_exists, #object_variables, #set_object_attribute

Methods included from Mixin::Errors

#compliance_error, #mapping_error, #raise_if_internal, #validation_error

Constructor Details

#initialize(owner, name, *types, **options) ⇒ Attribute

Returns a new instance of Attribute.

Parameters:

  • owner (Mapper::Type)
  • name (Symbol)
  • types (Array<Mapper::Type>)
  • Hash<Symbol, (Hash<Symbol, Object] options)

    Object] options



90
91
92
93
94
95
96
97
98
99
100
# 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

Instance Attribute Details

#aliasesArray<Symbol>

Returns:

  • (Array<Symbol>)


64
65
66
# File 'lib/ama-entity-mapper/type/attribute.rb', line 64

def aliases
  @aliases
end

#defaultObject

Returns:

  • (Object)


50
51
52
# File 'lib/ama-entity-mapper/type/attribute.rb', line 50

def default
  @default
end

#nullableTrueClass, FalseClass

Returns:

  • (TrueClass, FalseClass)


55
56
57
# File 'lib/ama-entity-mapper/type/attribute.rb', line 55

def nullable
  @nullable
end

#sensitiveTrueClass, FalseClass

Returns:

  • (TrueClass, FalseClass)


45
46
47
# File 'lib/ama-entity-mapper/type/attribute.rb', line 45

def sensitive
  @sensitive
end

#typesObject

Returns the value of attribute types.



29
30
31
# File 'lib/ama-entity-mapper/type/attribute.rb', line 29

def types
  @types
end

#types List of possible types attribute may take(Listofpossibletypesattributemaytake) ⇒ Array<AMA::Entity::Mapper::Type>

Returns:



29
# File 'lib/ama-entity-mapper/type/attribute.rb', line 29

attr_accessor :types

#validatorAPI::AttributeValidator

Returns:

  • (API::AttributeValidator)


72
# File 'lib/ama-entity-mapper/type/attribute.rb', line 72

handler :validator, :validate

#valuesArray<Object>

Returns:

  • (Array<Object>)


61
62
63
# File 'lib/ama-entity-mapper/type/attribute.rb', line 61

def values
  @values
end

#virtualTrueClass, FalseClass

Returns:

  • (TrueClass, FalseClass)


39
40
41
# File 'lib/ama-entity-mapper/type/attribute.rb', line 39

def virtual
  @virtual
end

Class Method Details

.defaultsObject



74
75
76
77
78
79
80
81
82
83
84
# 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

Instance Method Details

#==(other) ⇒ Object



151
152
153
# File 'lib/ama-entity-mapper/type/attribute.rb', line 151

def ==(other)
  eql?(other)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
149
# 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

#hashObject



142
143
144
# File 'lib/ama-entity-mapper/type/attribute.rb', line 142

def hash
  @owner.hash ^ @name.hash
end

#nameObject



26
27
28
# File 'lib/ama-entity-mapper/type/attribute.rb', line 26

def name
  @name
end

#ownerObject



23
24
25
# File 'lib/ama-entity-mapper/type/attribute.rb', line 23

def owner
  @owner
end

#resolve_parameter(parameter, substitution) ⇒ AMA::Entity::Mapper::Type::Attribute



129
130
131
132
133
134
135
136
137
138
139
140
# 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) ⇒ Object



122
123
124
# File 'lib/ama-entity-mapper/type/attribute.rb', line 122

def resolved!(context = nil)
  types.each { |type| type.resolved!(context) }
end

#resolved?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/ama-entity-mapper/type/attribute.rb', line 118

def resolved?
  types.all?(&:resolved?)
end

#to_defObject



155
156
157
158
159
160
# 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_sObject



162
163
164
165
166
167
# 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) ⇒ Object



110
111
112
113
114
115
116
# 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) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/ama-entity-mapper/type/attribute.rb', line 106

def valid?(value, context)
  violations(value, context).empty?
end

#violations(value, context) ⇒ Object



102
103
104
# File 'lib/ama-entity-mapper/type/attribute.rb', line 102

def violations(value, context)
  validator.validate(value, self, context)
end