This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.rb
232 lines (209 loc) · 8.09 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
require 'data_mapper'
require 'dm-is-reflective'
require 'open-uri/cached'
require 'json'
DATA_ROOT = "/home/harry/src/social_intervention/data"
DataMapper.setup(:default, "postgres://harry@localhost/social_intervention")
class Cost
include DataMapper::Resource
property :id, Serial
property :unit_cost, Decimal
property :actual_cost, Decimal
property :expected_cost, Decimal
property :activity, Decimal
property :bed_days, Decimal
property :mean, Decimal
property :mapping_pot, String
belongs_to :service
belongs_to :organisation
belongs_to :department
belongs_to :currency
end
class Department
include DataMapper::Resource
property :code, Text, key: true
property :name, Text
has n, :costs
end
class Currency
include DataMapper::Resource
property :code, Text, key: true
property :name, Text
has n, :costs
def self.alcohol_currencies
%w{Alcohol Liver Kidney}.map { |s| Currency.all(:name.like => "%#{s}%") }.flatten
end
end
class Service
include DataMapper::Resource
property :code, Text, key: true
property :name, Text
has n, :costs
def self.alcohol_service
Service.first(code: 'DAS')
end
end
class AlcoholCounty
include DataMapper::Resource
property :name, Text, key: true
property :date, Date, key: true
property :no_in_treatment, Decimal
property :new_presentations, Decimal
property :no_in_treatment_ytd, Decimal
property :discharges, Decimal
def output
{
date: date,
no_in_treatment: no_in_treatment,
new_presentations: new_presentations,
no_in_treatment_ytd: no_in_treatment_ytd,
discharges: discharges
}
end
end
class PerceptionDistrict
include DataMapper::Resource
property :district, Text, key: true
property :perception_drug_use, Decimal
property :perception_drunk_behaviour, Decimal
def output
{
drug_use: perception_drug_use.to_f,
drunk_behaviour: perception_drunk_behaviour.to_f
}
end
end
class Organisation
include DataMapper::Resource
property :code, Text, key: true
property :name, Text
property :national_grouping_code, Text
property :high_level_health_authority, Text
property :address_line_1, Text
property :address_line_2, Text
property :address_line_3, Text
property :address_line_4, Text
property :address_line_5, Text
property :postcode, Text
property :open_date, Text
property :close_date, Text
has n, :costs
def alcohol_costs
costs = Currency.alcohol_currencies.map { |currency| self.costs(currency: currency) }.flatten
costs.map do |cost|
{
cost: {
actual: cost.actual_cost.to_f,
expected: cost.expected_cost.to_f,
unit: cost.unit_cost.to_f
}
}
end
end
def output
{
name: self.name,
address: ([self.address_line_1,self.address_line_2,self.address_line_3,self.address_line_4,self.address_line_5].reject(&:nil?).reject(&:empty?) << self.postcode).join(", "),
alcohol_costs: self.alcohol_costs
}
end
end
class LAPELocalAuthority
include DataMapper::Resource
property :name, Text, key: true
property :male_months_lost_2004_2006, Float
property :male_months_lost_2005_2007, Float
property :male_months_lost_2006_2008, Float
property :male_months_lost_2007_2009, Float
property :male_months_lost_2008_2010, Float
property :female_months_lost_2004_2006, Float
property :female_months_lost_2005_2007, Float
property :female_months_lost_2006_2008, Float
property :female_months_lost_2007_2009, Float
property :female_months_lost_2008_2010, Float
property :male_specific_mortality_rate_2008_2010, Float
property :male_specific_mortality_rate_2004_2006, Float
property :male_specific_mortality_rate_2005_2007, Float
property :male_specific_mortality_rate_2006_2008, Float
property :male_specific_mortality_rate_2007_2009, Float
property :female_specific_mortality_rate_2008_2010, Float
property :female_specific_mortality_rate_2004_2006, Float
property :female_specific_mortality_rate_2005_2007, Float
property :female_specific_mortality_rate_2006_2008, Float
property :female_specific_mortality_rate_2007_2009, Float
property :male_chronic_liver_disease_mortality_rate_2008_2010, Float
property :female_chronic_liver_disease_mortality_rate_2008_2010, Float
property :male_attributable_mortality_rate_2010, Float
property :male_attributable_mortality_rate_2006, Float
property :male_attributable_mortality_rate_2007, Float
property :male_attributable_mortality_rate_2008, Float
property :male_attributable_mortality_rate_2009, Float
property :female_attributable_mortality_rate_2010, Float
property :female_attributable_mortality_rate_2006, Float
property :female_attributable_mortality_rate_2007, Float
property :female_attributable_mortality_rate_2008, Float
property :under_18s_specific_hospital_admissions_rate_2004_2007, Float
property :under_18s_specific_hospital_admissions_rate_2005_2008, Float
property :under_18s_specific_hospital_admissions_rate_2006_2009, Float
property :under_18s_specific_hospital_admissions_rate_2007_2010, Float
property :under_18s_specific_hospital_admissions_rate_2008_2011, Float
property :male_specific_hospital_admissions_rate_2006_2007, Float
property :male_specific_hospital_admissions_rate_2007_2008, Float
property :male_specific_hospital_admissions_rate_2008_2009, Float
property :male_specific_hospital_admissions_rate_2009_2010, Float
property :female_specific_hospital_admissions_rate_2006_2007, Float
property :female_specific_hospital_admissions_rate_2007_2008, Float
property :female_specific_hospital_admissions_rate_2008_2009, Float
property :female_specific_hospital_admissions_rate_2009_2010, Float
property :male_attributable_hospital_admissions_rate_2006_2007, Float
property :male_attributable_hospital_admissions_rate_2007_2008, Float
property :male_attributable_hospital_admissions_rate_2008_2009, Float
property :male_attributable_hospital_admissions_rate_2009_2010, Float
property :female_attributable_hospital_admissions_rate_2006_2007, Float
property :female_attributable_hospital_admissions_rate_2007_2008, Float
property :female_attributable_hospital_admissions_rate_2008_2009, Float
property :female_attributable_hospital_admissions_rate_2009_2010, Float
property :admission_episodes_2006_2007, Float
property :admission_episodes_2007_2008, Float
property :admission_episodes_2008_2009, Float
property :admission_episodes_2009_2010, Float
property :admission_episodes_rate_2010_2011, Float
property :attributable_crime_rate_2007_2008, Float
property :attributable_crime_rate_2008_2009, Float
property :attributable_crime_rate_2009_2010, Float
property :attributable_crime_rate_2010_2011, Float
property :attributable_crime_rate_2011_2012, Float
property :attributable_violent_crime_rate_2007_2008, Float
property :attributable_violent_crime_rate_2008_2009, Float
property :attributable_violent_crime_rate_2009_2010, Float
property :attributable_violent_crime_rate_2010_2011, Float
property :attributable_violent_crime_rate_2011_2012, Float
property :attributable_violent_crime_rate_2007_2008, Float
property :attributable_sexual_crime_rate_2008_2009, Float
property :attributable_sexual_crime_rate_2009_2010, Float
property :attributable_sexual_crime_rate_2010_2011, Float
property :attributable_sexual_crime_rate_2011_2012, Float
def output
attrs = attributes.tap { |attr| attr.delete(:name) }.map do |attr, val|
group, key, date = attr.to_s.match(/(male|female|under_18s)?_?([a-zA-Z_]+)_((20\d\d)_?(20\d\d)?)/).captures
date = date.split "_"
date = [date] if date.is_a? Integer
[group, key, date]
end
keys = attrs.map { |attr| attr[1] }.uniq
d = keys.map do |key|
results = attrs.select { |attr| attr[1] == key }
results.map! do |group, key2, date|
date_string = date.join("_")
{
group: group,
date: date,
value: attributes[((group.nil? ? "" : "#{group}_") + key + "_" + date_string).to_sym]
}.tap { |attr| attr.delete(:group) if attr[:group].nil? }
end
[key, results]
end
Hash[d]
end
end
DataMapper.finalize