forked from osyoyu/monakuji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.rb
71 lines (53 loc) · 1.41 KB
/
models.rb
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
DataMapper.setup(:default, 'sqlite:db.sqlite3')
class Sheet
include DataMapper::Resource
property :id, Serial
property :name, String
property :address, String
property :units, Integer, :default => 0, :required => true
property :paid, Float, :default => 0.0
property :paid_confirmed, Float, :default => 0.0
property :payout_address, String
property :payout, Float, :default => -1.0
property :payouted?, Boolean, :default => false
has n, :tickets
def price
(0.3 * self.units).round(8)
end
def paid?
if self.paid_confirmed >= self.price
true
else
false
end
end
def paid_unconfirmed
(self.paid - self.paid_confirmed).round(8)
end
before :create do
name = ((0..9).to_a + ("a".."z").to_a + ("A".."Z").to_a).sample(32).join
self.name = name
end
end
class Ticket
include DataMapper::Resource
property :id, Serial
property :number, Integer
property :message, String
belongs_to :sheet
before :create do
number = ""
loop do
# BUG: Strangely, save fails when number starts from a '0'
number = (1..9).to_a.sample(1).join + (0..9).to_a.sample(5).join
print "Ticket ##{number} generated: "
if !self.class.first(:number => number)
puts "valid."
break
else
puts "invalid (already exists), will regenerate number"
end
end
self.number = number
end
end