Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
lib/models.rb | 230 | 190 | 65.22%
|
61.05%
|
Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.
1 require 'ohm/contrib' |
2 |
3 class Host < Ohm::Model |
4 include Ohm::Typecast |
5 include Ohm::Timestamping |
6 include Ohm::Callbacks |
7 include Ohm::ExtraValidations |
8 |
9 attribute :name |
10 attribute :status |
11 collection :services, Service |
12 |
13 index :name |
14 index :status |
15 |
16 def validate |
17 assert_present :name |
18 assert_present :status |
19 assert_unique :name |
20 assert_member :status, ["up","down","pending"] |
21 end |
22 |
23 def to_hash |
24 arr = [] |
25 services.sort.each {|s| arr << s.to_hash} |
26 super.merge(:name => name, :status => status, :updated_at => updated_at, :services => arr) |
27 end |
28 |
29 def is_new? |
30 self.created_at == self.updated_at |
31 end |
32 |
33 class << self |
34 def find_or_create(opts = {}) |
35 begin |
36 # exclude requested status from lookup |
37 h = find(opts.reject{|key,value| key == :status}).first |
38 host = h.nil? ? create(opts) : h |
39 host.status = opts[:status] |
40 if host.valid? |
41 host.save |
42 end |
43 host |
44 rescue Exception => e |
45 e.message |
46 end |
47 end |
48 end |
49 end |
50 |
51 class Service < Ohm::Model |
52 include Ohm::Typecast |
53 include Ohm::Timestamping |
54 include Ohm::Callbacks |
55 include Ohm::ExtraValidations |
56 |
57 attribute :name |
58 attribute :status |
59 reference :host, Host |
60 |
61 index :name |
62 index :status |
63 |
64 def validate |
65 assert_present :name |
66 assert_present :status |
67 assert_present :host_id |
68 assert_unique [:name, :host_id] |
69 assert_member :status, ["up", "down", "pending"] |
70 end |
71 |
72 def to_hash |
73 super.merge(:name => name, :status => status, :updated_at => updated_at, :host => Host[host_id].name) |
74 end |
75 |
76 def is_new? |
77 self.created_at == self.updated_at |
78 end |
79 |
80 class << self |
81 def find_or_create(opts = {}) |
82 begin |
83 # convert passed host object to host_id if passed |
84 if opts.has_key?(:host) |
85 opts.merge!({:host_id => opts[:host].id}) |
86 opts.reject!{|key, value| key == :host} |
87 end |
88 # exclude requested status from lookup |
89 s = find(opts.reject{|key,value| key == :status}).first |
90 service = s.nil? ? create(opts) : s |
91 service.status = opts[:status] |
92 if service.valid? |
93 service.save |
94 end |
95 service |
96 rescue Exception => e |
97 e.message |
98 end |
99 end |
100 end |
101 end |
102 |
103 class Configuration < Ohm::Model |
104 include Ohm::Typecast |
105 include Ohm::Timestamping |
106 include Ohm::Callbacks |
107 include Ohm::ExtraValidations |
108 |
109 attribute :name |
110 attribute :format |
111 attribute :body |
112 attribute :new_record |
113 reference :application, Application |
114 |
115 index :name |
116 |
117 def validate |
118 assert_present :name |
119 assert_present :format |
120 assert_present :body |
121 assert_unique [:name, :application_id] |
122 end |
123 |
124 def to_hash |
125 super.merge(:name => name, :format => format, :body => body, :update_at => updated_at, :application => Application[application_id].name) |
126 end |
127 |
128 def is_new? |
129 self.created_at == self.updated_at |
130 end |
131 |
132 class << self |
133 def find_or_create(opts={}) |
134 begin |
135 if find(opts).first.nil? |
136 conf = create(opts) |
137 else |
138 conf = find(opts).first |
139 end |
140 rescue Exception => e |
141 e.message |
142 end |
143 end |
144 end |
145 end |
146 |
147 class Application < Ohm::Model |
148 include Ohm::Typecast |
149 include Ohm::Timestamping |
150 include Ohm::Callbacks |
151 include Ohm::ExtraValidations |
152 |
153 attribute :name |
154 collection :configurations, Configuration |
155 |
156 index :name |
157 |
158 def validate |
159 assert_present :name |
160 assert_unique :name |
161 end |
162 |
163 def to_hash |
164 arr = [] |
165 configurations.sort.each {|c| arr << c.to_hash} |
166 super.merge(:name => name, :updated_at => updated_at, :configurations => arr) |
167 end |
168 |
169 def is_new? |
170 self.created_at == self.updated_at |
171 end |
172 |
173 class << self |
174 def find_or_create(opts = {}) |
175 begin |
176 find(opts).first.nil? ? (app = create(opts)) : (app = find(opts).first) |
177 if app.valid? |
178 app.save |
179 end |
180 app |
181 rescue Exception => e |
182 e.message |
183 end |
184 end |
185 end |
186 end |
187 |
188 class Watcher < Ohm::Model #NYI |
189 include Ohm::Typecast |
190 include Ohm::Timestamping |
191 include Ohm::Callbacks |
192 |
193 attribute :client |
194 attribute :endpoint |
195 attribute :event |
196 attribute :action |
197 |
198 index :client |
199 index :event |
200 |
201 def validate |
202 assert_present :client, :endpoint, :event, :action |
203 assert_unique [:client, :endpoint, :event, :action] |
204 end |
205 end |
206 |
207 # Some pluralized helper models |
208 class Hosts |
209 def self.all(options = {}) |
210 options.empty? ? Host.all.sort : Host.find(options).sort |
211 end |
212 end |
213 |
214 class Services |
215 def self.all(options = {}) |
216 options.empty? ? Service.all.sort : Service.find(options).sort |
217 end |
218 end |
219 |
220 class Applications |
221 def self.all(options = {}) |
222 options.empty? ? Application.all.sort : Application.find(options).sort |
223 end |
224 end |
225 |
226 class Configurations |
227 def self.all(options = {}) |
228 options.empty? ? Configuration.all.sort : Configuration.find(options).sort |
229 end |
230 end |
Generated on 2011-01-27 21:32:12 -0500 with rcov 0.9.8