Class: WeighflowCli::OrderHandler::OrderRepository

Inherits:
Object
  • Object
show all
Includes:
Handlers
Defined in:
lib/weighflow_cli/order_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Handlers

#build_detail_file_name, #create_checksum, #external_ids, #find_deletes, #find_index, #for_each_index, #index_file_name, #index_line, #store_in_cache

Constructor Details

#initialize(orders = []) ⇒ OrderRepository

Returns a new instance of OrderRepository.



203
204
205
# File 'lib/weighflow_cli/order_handler.rb', line 203

def initialize(orders = [])
  @orders = orders 
end

Instance Attribute Details

#ordersObject (readonly)

Returns the value of attribute orders.



201
202
203
# File 'lib/weighflow_cli/order_handler.rb', line 201

def orders
  @orders
end

Instance Method Details

#performObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/weighflow_cli/order_handler.rb', line 207

def perform                
  adds = []
  updates = []
  unchanged = 0
  deletes = []

  # create file if not existing
  File.open(index_file_name, "w") {} unless File.exists?(index_file_name)

  #
  orders.each do |order|
    order_checksum = create_checksum(order)       
    index_result = find_index(order[:external_unique_id].to_s)
    
    if index_result 
      if index_result.checksum != order_checksum 
        index_result.delete!  # remove old, schedule update
        updates << order # setup update to be added to cache 
      else
        unchanged += 1           
      end                
    else
      adds << order
    end
  end

  # Remove and orders that will be deleted on store_in_cache
  deletes = find_deletes(orders) 
  #
  # Store all orders in data path with indexed file
  store_in_cache(orders)
  #
  #
  #
  { statistics: { adds: adds.size, updates: updates.size, deletes: deletes.size, unchanged: unchanged },
    adds: external_ids(adds),
    updates: external_ids(updates),
    deletes: external_ids(deletes)
  }
end