Class: AMA::Entity::Mapper::Type::Attribute
Overview
Stores data about single type attribute
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
declare_handler_block_setter, declare_handler_getter, declare_handler_method, declare_handler_setter, declare_namespace_method, included
#install_object_method, #method_object, #object_variable, #object_variable_exists, #object_variables, #set_object_attribute
#compliance_error, #mapping_error, #raise_if_internal, #validation_error
Constructor Details
#initialize(owner, name, *types, **options) ⇒ Attribute
Returns a new instance of Attribute.
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
#aliases ⇒ Array<Symbol>
64
65
66
|
# File 'lib/ama-entity-mapper/type/attribute.rb', line 64
def aliases
@aliases
end
|
#default ⇒ Object
50
51
52
|
# File 'lib/ama-entity-mapper/type/attribute.rb', line 50
def default
@default
end
|
#nullable ⇒ TrueClass, FalseClass
55
56
57
|
# File 'lib/ama-entity-mapper/type/attribute.rb', line 55
def nullable
@nullable
end
|
#sensitive ⇒ TrueClass, FalseClass
45
46
47
|
# File 'lib/ama-entity-mapper/type/attribute.rb', line 45
def sensitive
@sensitive
end
|
#types ⇒ Object
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>
29
|
# File 'lib/ama-entity-mapper/type/attribute.rb', line 29
attr_accessor :types
|
#validator ⇒ API::AttributeValidator
72
|
# File 'lib/ama-entity-mapper/type/attribute.rb', line 72
handler :validator, :validate
|
#values ⇒ Array<Object>
61
62
63
|
# File 'lib/ama-entity-mapper/type/attribute.rb', line 61
def values
@values
end
|
#virtual ⇒ TrueClass, FalseClass
39
40
41
|
# File 'lib/ama-entity-mapper/type/attribute.rb', line 39
def virtual
@virtual
end
|
Class Method Details
.defaults ⇒ Object
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
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
|
#hash ⇒ Object
142
143
144
|
# File 'lib/ama-entity-mapper/type/attribute.rb', line 142
def hash
@owner.hash ^ @name.hash
end
|
#name ⇒ Object
26
27
28
|
# File 'lib/ama-entity-mapper/type/attribute.rb', line 26
def name
@name
end
|
#owner ⇒ Object
23
24
25
|
# File 'lib/ama-entity-mapper/type/attribute.rb', line 23
def owner
@owner
end
|
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
118
119
120
|
# File 'lib/ama-entity-mapper/type/attribute.rb', line 118
def resolved?
types.all?(&:resolved?)
end
|
#to_def ⇒ Object
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_s ⇒ Object
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
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
|