Class: Host

Inherits:
Ohm::Model
  • Object
show all
Includes:
Ohm::Callbacks, Ohm::ExtraValidations, Ohm::Timestamping, Ohm::Typecast
Defined in:
lib/noah/hosts.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) find_or_create(opts = {})



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/noah/hosts.rb', line 44

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

Instance Method Details

- (Boolean) is_new?

Evaluate if Host record is new or not

Returns:

  • (Boolean)


39
40
41
# File 'lib/noah/hosts.rb', line 39

def is_new?
  self.created_at == self.updated_at
end

- (Object) stash_name (protected)

Saves the original #name attribute before deleting an object



62
63
64
# File 'lib/noah/hosts.rb', line 62

def stash_name
  @deleted_name = self.name
end

- (Hash) to_hash

A hash representation of a Host

Returns:

  • (Hash)

    A hash representation of a Host



31
32
33
34
35
36
# File 'lib/noah/hosts.rb', line 31

def to_hash
  arr = []
  services.sort.each {|s| arr << s.to_hash}
  h = {:name => name, :status => status, :created_at => created_at, :updated_at => updated_at, :services => arr}
  super.merge(h)
end

- (Object) validate



22
23
24
25
26
27
28
# File 'lib/noah/hosts.rb', line 22

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