-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupport.rb
More file actions
43 lines (30 loc) · 784 Bytes
/
support.rb
File metadata and controls
43 lines (30 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require_relative "lib/voom"
Voom.store = Voom::MemoryWriter.new
class Item < Voom::Type
str :name
float :price
end
class QuantifiedItem < Voom::Type
int :quantity
reference :item, Item
end
class ShoppingCart < Voom::Type
list :data, QuantifiedItem
include Enumerable
def each(&b)
data.each(&b)
end
def to_s
"In your cart: \n\n" +
map { |e| "- #{e.item.name} @ #{e.item.price} x #{e.quantity}" }.join("\n") +
"\n\nTOTAL: " +
('%.2f' % reduce(0) { |s,e| s + (e.item.price * e.quantity)})
end
end
### .................................
def item(i_name, i_price)
Item.write(:name => i_name, :price => i_price)
end
def quantified_item(item_ref, i_quantity)
QuantifiedItem.write(:item => item_ref, :quantity => i_quantity)
end