Class: PackAPI::Types::AggregateType::AttributeBlender

Inherits:
Object
  • Object
show all
Defined in:
lib/pack_api/types/aggregate_type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute_sources, aggregate_type) ⇒ AttributeBlender

Returns a new instance of AttributeBlender.



99
100
101
102
103
# File 'lib/pack_api/types/aggregate_type.rb', line 99

def initialize(attribute_sources, aggregate_type)
  @attribute_sources = attribute_sources
  @aggregate_type = aggregate_type
  @combined_attributes = {}
end

Instance Attribute Details

#aggregate_typeObject (readonly)

Returns the value of attribute aggregate_type.



97
98
99
# File 'lib/pack_api/types/aggregate_type.rb', line 97

def aggregate_type
  @aggregate_type
end

#attribute_sourcesObject (readonly)

Returns the value of attribute attribute_sources.



97
98
99
# File 'lib/pack_api/types/aggregate_type.rb', line 97

def attribute_sources
  @attribute_sources
end

#combined_attributesObject (readonly)

Returns the value of attribute combined_attributes.



97
98
99
# File 'lib/pack_api/types/aggregate_type.rb', line 97

def combined_attributes
  @combined_attributes
end

Instance Method Details

#create(params) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/pack_api/types/aggregate_type.rb', line 152

def create(params)
  attribute_sources_created = []
  primary_resource_id = nil
  attribute_sources.each do |resource_name, resource_attributes|
    resource_method = :"create_#{resource_name}"
    args = [resource_params(params, resource_attributes)]
    args << primary_resource_id if primary_resource_id
    resource = aggregate_type.send(resource_method, *args)
    primary_resource_id ||= resource.id
    combined_attributes.merge!(resource.to_h.slice(*resource_attributes))
  end
  combined_attributes
rescue PackAPI::InternalError
  rollback_create(attribute_sources_created, primary_resource_id)
  raise
end

#delete(id) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/pack_api/types/aggregate_type.rb', line 169

def delete(id)
  attribute_sources.each_key do |resource_name|
    resource_method = :"delete_#{resource_name}"
    aggregate_type.send(resource_method, id)
  rescue PackAPI::InternalError => e
    Rails.logger.error("Failed to delete #{resource_name} with id: #{id}")
    Rails.logger.error(e.backtrace.join("\n"))
  end
end

#get(id) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/pack_api/types/aggregate_type.rb', line 125

def get(id)
  attribute_sources.each do |resource_name, attribute_names|
    resource_method = :"get_#{resource_name}"
    resource = aggregate_type.send(resource_method, id)
    combined_attributes.merge!(resource.to_h.slice(*attribute_names))
  end
  combined_attributes
end

#query(**params) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/pack_api/types/aggregate_type.rb', line 105

def query(**params)
  primary_source, primary_resource_attributes = attribute_sources.first
  resource_method = :"query_#{primary_source}s"
  primary_resource_results = aggregate_type.send(resource_method, **params)
  combined_attributes = primary_resource_results.transform_values do |primary_resource|
    primary_resource.to_h.slice(*primary_resource_attributes)
  end
  primary_result_ids = primary_resource_results.keys
  attribute_sources.drop(1).each do |resource_name, resource_attributes|
    resource_method = :"query_#{resource_name}s"
    aggregate_type.send(resource_method, id: primary_result_ids)
                  .each do |primary_resource_id, secondary_resource|
      secondary_attributes = secondary_resource.to_h.slice(*resource_attributes)
      combined_attributes[primary_resource_id].merge!(secondary_attributes)
    end
  end

  combined_attributes.values
end

#update(id, params) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/pack_api/types/aggregate_type.rb', line 134

def update(id, params)
  initial_state = get(id)
  @combined_attributes = initial_state.to_h
  attribute_sources_updated = []
  attribute_sources.each do |resource_name, resource_attributes|
    resource_params = resource_params(params, resource_attributes)
    next unless resource_params.any?

    resource_method = :"update_#{resource_name}"
    resource = aggregate_type.send(resource_method, id, resource_params)
    combined_attributes.merge!(resource.to_h.slice(*resource_attributes))
  end
  combined_attributes
rescue PackAPI::InternalError
  rollback_update(attribute_sources_updated, id, initial_state)
  raise
end