module IronBank::Local

A local store for exported records.

Public Instance Methods

all() click to toggle source
Calls superclass method
# File lib/iron_bank/local.rb, line 18
def all
  store.any? ? store.values : super
end
find(id) click to toggle source
Calls superclass method
# File lib/iron_bank/local.rb, line 7
def find(id)
  store[id] || super
end
find_each(&block) click to toggle source
Calls superclass method
# File lib/iron_bank/local.rb, line 11
def find_each(&block)
  return enum_for(:find_each) unless block

  values = store.values
  values.any? ? values.each(&block) : super
end
first() click to toggle source
Calls superclass method
# File lib/iron_bank/local.rb, line 22
def first
  store.any? ? store.values.first : super
end
reset_store() click to toggle source
# File lib/iron_bank/local.rb, line 36
def reset_store
  @store = nil
end
where(conditions, limit: IronBank::Actions::Query::DEFAULT_ZUORA_LIMIT) click to toggle source
Calls superclass method
# File lib/iron_bank/local.rb, line 26
def where(conditions, limit: IronBank::Actions::Query::DEFAULT_ZUORA_LIMIT)
  records = store.values.select do |record|
    conditions.all? do |field, match_value|
      value_matches?(record.public_send(field), match_value)
    end
  end

  records.any? ? records : super
end

Private Instance Methods

csv_converters() click to toggle source
# File lib/iron_bank/local.rb, line 73
def csv_converters
  %i[
    decimal_integer
    decimal_float
  ]
end
csv_options() click to toggle source
# File lib/iron_bank/local.rb, line 65
def csv_options
  {
    headers:           true,
    header_converters: [underscore_header],
    converters:        csv_converters
  }
end
file_path() click to toggle source
# File lib/iron_bank/local.rb, line 80
def file_path
  File.expand_path(
    "#{object_name}.csv",
    IronBank.configuration.export_directory
  )
end
load_records() click to toggle source
# File lib/iron_bank/local.rb, line 59
def load_records
  CSV.foreach(file_path, **csv_options).with_object({}) do |row, store|
    store[row[:id]] = new(row.to_h.compact)
  end
end
store() click to toggle source
# File lib/iron_bank/local.rb, line 55
def store
  @store ||= File.exist?(file_path) ? load_records : {}
end
underscore_header() click to toggle source

NOTE: Handle associations within the CSV export. E.g., when reading the

`ProductRatePlan.csv` file, we have `ProductRatePlan.Id` and
`Product.Id` fields. We want to end up with `id` and `product_id`
respectively.
# File lib/iron_bank/local.rb, line 46
def underscore_header
  lambda do |header|
    parts  = header.split(".")
    header = parts.first.casecmp?(object_name) ? parts.last : parts.join

    IronBank::Utils.underscore(header).to_sym
  end
end
value_matches?(record_value, match_value) click to toggle source

:reek: UtilityFunction

# File lib/iron_bank/local.rb, line 88
def value_matches?(record_value, match_value)
  if match_value.is_a? Array
    match_value.include? record_value
  else
    record_value == match_value
  end
end