Class: Noah::Host

Inherits:
Model
  • Object
show all
Includes:
Linkable, Taggable
Defined in:
lib/noah/models/hosts.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


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/noah/models/hosts.rb', line 35

def find_or_create(opts = {})
  begin
    h = find(:name => opts[:name]).first
    #h = find(opts.reject{|key,value| key == :status}).first
    h.nil? ? host=new(opts) : host=h
    host.status = opts[:status]
    if host.valid?
      host.save
    end
    host
  rescue Exception => e
    e.message
  end
end

Instance Method Details

#to_hashHash

Returns A hash representation of a Noah::Host.

Returns:

  • (Hash)

    A hash representation of a Noah::Host


25
26
27
28
29
30
31
32
# File 'lib/noah/models/hosts.rb', line 25

def to_hash
  services_hash = Hash.new
  services.sort.each do |svc|
    services_hash["#{svc.name}"] = svc.status
  end
  h = {:name => name, :status => status, :created_at => created_at, :updated_at => updated_at, :services => services_hash}
  super.merge(h)
end

#validateObject


16
17
18
19
20
21
22
# File 'lib/noah/models/hosts.rb', line 16

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