-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.rb
203 lines (152 loc) · 5.52 KB
/
db.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
require 'dm-core'
require 'dm-types'
require 'dm-migrations'
require 'dm-validations' # XXX - not working for me :(
DataMapper::Logger.new(STDOUT, :debug) if ENV['DEBUG']
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3:///#{Dir.pwd}/devel.db")
class ApplicationNumber < DataMapper::Type # NB. causes an erroneous deprecation warning in dm v1.0.0
primitive String
length 16 # XXX - sqlite is ignoring this :(
auto_validation true # XXX - sqlite is ignoring this :(
def self.re
@re ||= Regexp.compile('^((AM|MIN|MPS|SP|SC)/\d{4}/\d+(?:/([A-K]))?)$')
end
def self.dump(value, property)
return nil unless value
raise "ERROR bad #{name}: #{value.inspect}" unless re.match(value)
value.to_s
end
def self.load(value, property) # (2010/06/18 dm documentation needs updating ... doesn't mention "property" arg)
value
end
end
class Council
include DataMapper::Resource
property :name , String ,:key => true
has n, :addresses
has n, :reports
end
class Address
include DataMapper::Resource
property :id , Serial
property :name , String , :required => false
property :number , String , :required => false
property :street , String , :required => true
property :suburb , String , :required => true
property :state , String , :required => true
property :postcode , Integer , :required => true
belongs_to :council
belongs_to :ward #, :required => false # XXX - can associations even be required => false?
has n, :applications
default_scope(:default).update(:order => [:state, :suburb, :street, :number])
def to_s
#"#{number} #{street} #{suburb}"# #{state} #{postcode}"
"#{number} #{street} #{suburb} #{state} #{postcode}"
end
end
class Ward
include DataMapper::Resource
property :name , String , :key => true
has n, :addresses
#has n, :reports , :through => ReportWard
has n, :reports , :through => Resource
default_scope(:default).update(:order => :name)
def to_s
name
end
end
class ApplicationEvent
include DataMapper::Resource
property :id , Serial
property :application_number , ApplicationNumber
property :date , Date #, :required => false # XXX - nullable?
property :status , String
property :event , Enum[:received, :updated, :advertised, :decided]
belongs_to :application
belongs_to :page
# keep the order that appeared in the report
#default_scope(:default).update(:order => [:application_number, :date])
# keep the order that appeared in the report
def to_s
"(#{event}:#{status} #{date})"
end
end
class Application
include DataMapper::Resource
property :number , ApplicationNumber , :key => true , :length => (4..16)
property :description , Text , :length => (0..1024)
property :applicant , Text , :length => (0..256)
belongs_to :address
has n , :application_events
default_scope(:default).update(:order => [:number])
def to_s
"<##{self.class.name}: #{number} : #{application_events.last} : #{address} : #{address.ward} : #{'%-20s' % description}}>"
end
end
class Page
include DataMapper::Resource
property :report_id , Integer , :key => true
property :number , Integer , :key => true
belongs_to :report
has n , :application_events
default_scope(:default).update(:order => [:report_id, :number])
def to_s
"<##{self.class.name} #{report.report_type} #{report.date}: Page #{number} of #{report.page_count} (#{application_events.count} application events)>"
end
end
=begin
class ReportWard
include DataMapper::Resource
belongs_to :reports
belongs_to :wards
end
=end
class Report
include DataMapper::Resource
property :id , Serial
property :date , Date #, :key => true
property :report_type , Discriminator #, :key => true
property :title , Text
property :date_from , Date
property :date_to , Date
property :page_count , Integer
belongs_to :council
has n, :pages
#has n, :wards , :through => ReportWard
has n, :wards , :through => Resource
default_scope(:default).update(:order => [:date, :report_type])
has n, :application_events , :through => :pages
def application_events2
# XXX - chaining associations doesn't work until the objects have been saved to the database...
pages.collect { |page| page.application_events }.flatten
end
def to_s
#"<##{self.class.name} Report: #{title} : #{date} : (#{date_from} - #{date_to}) (#{page_count} pages) (#{application_events.count} application events)>" #+ "\n #{pages.join("\n ")}"
"<##{self.class.name} Report: #{title} : #{date} : (#{date_from} - #{date_to}) (#{page_count}(#{pages.count}) pages) (#{application_events.count}(#{application_events2.size}) application events)>" #+ "\n #{pages.join("\n ")}"
end
def self.subclasses(direct = false)
classes = []
if direct
ObjectSpace.each_object(Class) do |c|
next unless c.superclass == self
classes << c
end
else
ObjectSpace.each_object(Class) do |c|
next unless c.ancestors.include?(self) and (c != self)
classes << c
end
end
classes
end
end
#
# Report subclasses
#
class Received < Report; end
class Updated < Report; end
class UpdatedSubdivisions < Updated; end
class Advertised < Report; end
class Decided < Report; end
class DecidedSubdivisions < Report; end
DataMapper.auto_upgrade!