class ProductAPI

Public Class Methods

add(product) click to toggle source
# File lib/ProductAPI.rb, line 18
def self.add(product)
  @@list ||= Array.new
  product[:id] = max_id_value + 1
  puts "ProductAPI#add: Creating a product with these details: #{product}"
  @@list.push(product)
  add_line(@@file_path, product)
end
delete(id) click to toggle source
# File lib/ProductAPI.rb, line 59
def self.delete(id)
  puts "ProductAPI#delete: Deleting product id#{id}"
  @@list -= [search_id(id)]
  remove_line(@@file_path, search_id(id))
end
get_list() click to toggle source
# File lib/ProductAPI.rb, line 14
def self.get_list
  @@list ||= Array.new
end
search_id(id) click to toggle source
# File lib/ProductAPI.rb, line 36
def self.search_id(id)
  search(:id, id)
end
seed() click to toggle source
# File lib/ProductAPI.rb, line 10
def self.seed
  @@list = read_file(@@file_path)
end
update(id, key, value) click to toggle source
# File lib/ProductAPI.rb, line 40
def self.update(id, key, value)
  unless(key==:name || key==:color || key==:price || key==:quantity)
    puts "Invalid command. Cannot update #{key} field."
    return
  end
  if(key==:quantity || key==:price)
    value = value.to_i
  end
  old_product = search_id(id).clone
  @@list = @@list.map do |product|
    if product[:id] == id
      product[key] = value
      puts "ProductAPI#update: Updating product id#{id} with these details: #{key}: #{value}"
      update_line(@@file_path, old_product, product)
    end
      product
  end
end

Private Class Methods

max_id_value() click to toggle source
# File lib/ProductAPI.rb, line 66
def self.max_id_value
  @@list.inject(0){|max, p| max = max > p[:id]? max:p[:id] }
end