Class: Etna::Clients::Magma::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/etna/clients/magma/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, update_request, page_size) ⇒ Pager

Returns a new instance of Pager.



93
94
95
96
97
98
99
# File 'lib/etna/clients/magma/client.rb', line 93

def initialize(client, update_request, page_size)
  @client = client

  @update_request = update_request

  @page_size = [ page_size || 2, 2 ].max
end

Instance Method Details

#add_to_page(page, entry) ⇒ Object



187
188
189
190
# File 'lib/etna/clients/magma/client.rb', line 187

def add_to_page(page, entry)
  page[ entry[:model_name] ] ||= {}
  page[ entry[:model_name] ][ entry[:record_name] ] = entry[:record]
end

#flat_recordsObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/etna/clients/magma/client.rb', line 132

def flat_records
  project = @client.retrieve(
    project_name: @update_request.project_name,
    hide_templates: false
  )

  flat_records = {}
  revisions.each do |model_name, records|
    records.each do |record_name, record|
      flat = {
          model_name: model_name,
          record_name: record_name,
          record: record
      }
      flat_name = "#{model_name}.#{record_name}"

      # if the update sets a table attribute, attach table records to their parent so they are submitted together
      flat_tables = []
      record.each do |attribute_name, value|
        att = project.models.model(model_name).template.attributes.attribute(attribute_name)
        if att.attribute_type == 'table'
          value.each do |table_entry_name|
            table_record = revisions[ att.link_model_name ][table_entry_name]
            flat_tables.push(
              {
                model_name: att.link_model_name,
                record_name: table_entry_name,
                record: table_record
              }
            )

            # exclude the attached table record from the flattened list of records
            flat_records[ "#{ att.link_model_name }.#{table_entry_name}" ] = nil
          end
        end
      end
      flat[:tables] = flat_tables
      flat_records[ flat_name ] = flat unless flat_records.has_key?(flat_name)
    end
  end

  return flat_records.values.compact.shuffle
end

#responseObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/etna/clients/magma/client.rb', line 101

def response
  page_count = 0
  page = {}

  results = UpdateResponse.new({"models" => {}})

  flat_records.each do |flat_record|
    page_count += 1 + flat_record[:tables].length

    add_to_page( page, flat_record )

    flat_record[:tables].each do |table_record|
      add_to_page(page, table_record)
    end

    if page_count >= @page_size
      results.update(update_part(page))
      page = {}
      page_count = 0
    end
  end

  results.update(update_part(page)) if page_count > 0

  return results
end

#revisionsObject



128
129
130
# File 'lib/etna/clients/magma/client.rb', line 128

def revisions
  @update_request.revisions
end

#update_part(page) ⇒ Object



176
177
178
179
180
181
182
183
184
185
# File 'lib/etna/clients/magma/client.rb', line 176

def update_part(page)
  @client.update_json(
    UpdateRequest.new(
      project_name: @update_request.project_name,
      revisions: page,
      dry_run: @update_request.dry_run,
      autolink: @update_request.autolink
    )
  )
end