Skip to content

Instantly share code, notes, and snippets.

@olistik
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olistik/cd3673ee0901593f1d96 to your computer and use it in GitHub Desktop.
Save olistik/cd3673ee0901593f1d96 to your computer and use it in GitHub Desktop.
require 'json'
UNITS = {
'unit_a' => 'kg.25',
'unit_b' => 'kg.15',
# ...
'unit_c' => 'kg.5',
}
STORE = {
'department_a' => {
'cemento' => {
price: 4.0,
unit: UNITS['unit_a']
},
'cemento_bianco' => {
price: 12.0
unit: UNITS['unit_a']
}
},
# ...
'department_b' => {
# ...
}
}
def load_or_create_database(filename:)
if File.exists?(filename)
JSON.parse(File.read(filename))
else
quantities = {}
STORE.each do |department, products|
quantities[department] ||= {}
products.each do |product_name|
quantities[department][product_name] = 100
end
end
File.write(filename, quantities.to_json)
quantities
end
end
quantities = load_or_create_database(filename: 'store.json')
def product_price(department:, product_name:, requested_quantity:)
amount = STORE[department][product_name][:price] * requested_quantity
"$ #{amount}"
end
product_price(department: 'department_a', product_name: 'cemento_bianco', requested_quantity: 23) # => "$ 276"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment