Class: PackAPI::Mapping::AttributeMap
- Inherits:
-
Object
- Object
- PackAPI::Mapping::AttributeMap
show all
- Defined in:
- lib/pack_api/mapping/attribute_map.rb
Defined Under Namespace
Classes: ValueTransformation, ValueTransformationChain
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(data_source = nil, options = nil) ⇒ AttributeMap
Returns a new instance of AttributeMap.
85
86
87
88
89
90
91
|
# File 'lib/pack_api/mapping/attribute_map.rb', line 85
def initialize(data_source = nil, options = nil)
@options = DEFAULT_OPTIONS
@config = self.class.config
self.options = options
self.data_source = data_source
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
29
30
31
|
# File 'lib/pack_api/mapping/attribute_map.rb', line 29
def config
@config
end
|
#options ⇒ Object
Returns the value of attribute options.
29
30
31
|
# File 'lib/pack_api/mapping/attribute_map.rb', line 29
def options
@options
end
|
Class Method Details
.api_attribute_keys(hash) ⇒ Object
76
77
78
79
80
|
# File 'lib/pack_api/mapping/attribute_map.rb', line 76
def self.api_attribute_keys(hash)
options = { contains_model_attributes: true,
transformer_type_for_source: AttributeHashTransformer.name }
new(hash.symbolize_keys, options).attributes
end
|
.api_type(api_type = nil) ⇒ Object
46
47
48
49
50
|
# File 'lib/pack_api/mapping/attribute_map.rb', line 46
def api_type(api_type = nil)
return @api_type unless api_type
@api_type = api_type
end
|
.config ⇒ Object
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/pack_api/mapping/attribute_map.rb', line 58
def config
{
mappings: @mappings,
from_api_attributes: @from_api_attributes,
from_model_attributes: @from_model_attributes,
transform_nested_attributes_with: @transform_nested_attributes_with,
api_type: @api_type,
model_type: @model_type
}
end
|
.map(source_attr, to: nil, from_api_attribute: nil, from_model_attribute: nil, readonly: nil, transform_nested_attributes_with: nil) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/pack_api/mapping/attribute_map.rb', line 32
def map(source_attr, to: nil, from_api_attribute: nil, from_model_attribute: nil, readonly: nil, transform_nested_attributes_with: nil)
@mappings ||= {}
@from_api_attributes ||= {}
@from_model_attributes ||= {}
@transform_nested_attributes_with ||= {}
@mappings[source_attr] = to || source_attr
@from_model_attributes[source_attr] = from_model_attribute if from_model_attribute.present?
@from_api_attributes[source_attr] = from_api_attribute if from_api_attribute.present?
@transform_nested_attributes_with[source_attr] = transform_nested_attributes_with if transform_nested_attributes_with.present?
if readonly
@from_api_attributes[source_attr] = ->(*) { raise PackAPI::InternalError, "Unable to modify read-only attribute '#{source_attr}'" }
end
end
|
.model_attribute_keys(hash) ⇒ Object
70
71
72
73
74
|
# File 'lib/pack_api/mapping/attribute_map.rb', line 70
def self.model_attribute_keys(hash)
options = { contains_model_attributes: false,
transformer_type_for_source: AttributeHashTransformer.name }
new(hash.symbolize_keys, options).attributes
end
|
.model_type(model_type = nil) ⇒ Object
52
53
54
55
56
|
# File 'lib/pack_api/mapping/attribute_map.rb', line 52
def model_type(model_type = nil)
return @model_type unless model_type
@model_type = model_type
end
|
Instance Method Details
#api_type ⇒ Object
150
151
152
|
# File 'lib/pack_api/mapping/attribute_map.rb', line 150
def api_type
self.class.api_type
end
|
#attributes ⇒ Object
146
147
148
|
# File 'lib/pack_api/mapping/attribute_map.rb', line 146
def attributes
@transformer.execute
end
|
#data_source ⇒ Object
125
126
127
|
# File 'lib/pack_api/mapping/attribute_map.rb', line 125
def data_source
@transformer&.data_source
end
|
#data_source=(data_source) ⇒ Object
116
117
118
119
120
121
122
123
|
# File 'lib/pack_api/mapping/attribute_map.rb', line 116
def data_source=(data_source)
transformer_type = transformer_type_for_source(data_source)
unless @transformer.is_a?(transformer_type)
@transformer = transformer_type.new(config_for_adapter_type(transformer_type))
end
@transformer.data_source = data_source
@transformer.options = @options
end
|
#from_api_attributes ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/pack_api/mapping/attribute_map.rb', line 99
def from_api_attributes
return @from_api_attributes if defined?(@from_api_attributes)
@from_api_attributes = config[:from_api_attributes].transform_values do |proc_or_method_name|
ValueTransformationChain.new([ValueTransformation.new(self.class, proc_or_method_name, options)])
end
config[:transform_nested_attributes_with].each do |source_attr, attribute_map_class|
@from_api_attributes[source_attr] ||= ValueTransformationChain.new([])
@from_api_attributes[source_attr].transformations << ValueTransformation.new(
self.class, :convert_nested_attribute, { attribute_map_class: attribute_map_class }
)
end
@from_api_attributes
end
|
#from_model_attributes ⇒ Object
93
94
95
96
97
|
# File 'lib/pack_api/mapping/attribute_map.rb', line 93
def from_model_attributes
@from_model_attributes ||= config[:from_model_attributes].transform_values do |proc_or_method_name|
ValueTransformationChain.new([ValueTransformation.new(self.class, proc_or_method_name, options)])
end
end
|
#model_type ⇒ Object
154
155
156
|
# File 'lib/pack_api/mapping/attribute_map.rb', line 154
def model_type
self.class.model_type
end
|
140
141
142
143
144
|
# File 'lib/pack_api/mapping/attribute_map.rb', line 140
def register_transformation_from_model_attribute(source_attr, proc_or_method_name)
from_model_attributes[source_attr] ||= ValueTransformationChain.new([])
transformation = ValueTransformation.new(self.class, proc_or_method_name, options)
from_model_attributes[source_attr].transformations << transformation
end
|