Class: PackAPI::Mapping::ErrorHashToAPIAttributesTransformer
- Inherits:
-
AbstractTransformer
- Object
- AbstractTransformer
- PackAPI::Mapping::ErrorHashToAPIAttributesTransformer
- Defined in:
- lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb
Overview
Specialized attribute transformer converting the attribute names in an ActiveRecord Error object to those that should be present in the Error Hash in the API Result object.
Constant Summary collapse
- NESTED_ATTRIBUTE_ERROR_KEY =
/\A(?<parent>[\w_]+)\[(?<index>\d+)\]\.(?<child>[\w_]+)\z/
Instance Attribute Summary
Attributes inherited from AbstractTransformer
#api_type, #data_source, #mappings, #model_type, #options
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(config) ⇒ ErrorHashToAPIAttributesTransformer
constructor
A new instance of ErrorHashToAPIAttributesTransformer.
Constructor Details
#initialize(config) ⇒ ErrorHashToAPIAttributesTransformer
Returns a new instance of ErrorHashToAPIAttributesTransformer.
10 11 12 13 |
# File 'lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb', line 10 def initialize(config) super @transform_nested_attributes_with = config[:transform_nested_attributes_with] end |
Instance Method Details
#execute ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb', line 15 def execute api_attributes = api_attribute_names.flat_map do |api_attribute| model_attribute = model_attribute(api_attribute) next unless error_present_for?(model_attribute) error_keys_for(model_attribute).map do |error_key| converted_error_key = nested_attribute_error_key?(error_key) ? convert_nested_attribute_error_key(error_key, api_attribute) : normalize_association_reference(api_attribute, model_attribute) [converted_error_key, data_source[error_key]] end end api_attributes.compact.to_h end |