Class: Noah::Service

Inherits:
Model
  • Object
show all
Includes:
Linkable, Taggable
Defined in:
lib/noah/models/services.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Linkable

included, #link!, #unlink!

Methods included from Taggable

included, #tag!, #untag!

Methods inherited from Model

inherited

Class Method Details

.find_or_create(opts = {}) ⇒ Object


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/noah/models/services.rb', line 28

def find_or_create(opts = {})
  begin
    # convert passed host object to host_id if passed
    if opts.has_key?(:host)
      opts.merge!({:host_id => opts[:host].id})
      opts.reject!{|key, value| key == :host}
    end  
    # exclude requested status from lookup
    s = find(:name => opts[:name], :host_id => opts[:host_id]).first
    s.nil? ? service=new(opts) : service=s
    service.status = opts[:status]
    if service.valid?
      service.save
    end
    service
  rescue Exception => e
    e.message
  end
end

Instance Method Details

#to_hashObject


22
23
24
25
# File 'lib/noah/models/services.rb', line 22

def to_hash
  Host[host_id].nil? ? host_name=nil : host_name=Host[host_id].name
  super.merge(:name => name, :status => status, :updated_at => updated_at, :host => host_name)
end

#validateObject


13
14
15
16
17
18
19
20
# File 'lib/noah/models/services.rb', line 13

def validate
  super
  assert_present :name
  assert_present :status
  assert_present :host_id
  assert_unique [:name, :host_id]
  assert_member :status, ["up", "down", "pending"]
end