Class: Noah::Link

Inherits:
Model
  • Object
show all
Defined in:
lib/noah/models/link.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

inherited

Class Method Details

.find_or_create(opts = {}) ⇒ Object


71
72
73
74
75
76
77
78
# File 'lib/noah/models/link.rb', line 71

def find_or_create(opts={})
  begin
    find(opts).first.nil? ? obj=create(opts) : obj=find(opts).first
    obj
  rescue Exception => e
    e.message
  end
end

Instance Method Details

#nameObject


66
67
68
# File 'lib/noah/models/link.rb', line 66

def name
  @name = path
end

#nodesObject


9
10
11
12
13
14
15
# File 'lib/noah/models/link.rb', line 9

def nodes
  arr = []
  self.key[:nodes].smembers.each do |node|
    arr << node
  end
  arr
end

#nodes=(node) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/noah/models/link.rb', line 17

def nodes=(node)
  case node.class.to_s
  when "Array"
    node.each do |n|
      self.key[:nodes].sadd(n.key)
      n.links << self
    end
  else
    self.key[:nodes].sadd(node.key)
    node.links << self
  end
end

#to_hashObject


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
# File 'lib/noah/models/link.rb', line 36

def to_hash
  # TODO Holy shit, is this messy or what?
  # Prepopulate instance variables of each object type instead?
  %w[applications configurations hosts services ephemerals].each {|x| instance_variable_set("@#{x}", Hash.new)}
  if nodes.size > 0
    nodes.each do |node|
      rejected_vals = [:created_at, :updated_at, :links, :name]
      n = node_to_class(node)
      cls = class_to_lower(n.class.to_s)
      hsh = instance_variable_get("@#{cls}s")
      # all of this bs is because services are unique per [servicename, hostname]
      # so if I add multiple services just by name to the hash, I'll wipe the previous one
      # a better option would be for services to be named slug style 
      hsh["#{n.name}"] = Hash.new unless hsh.has_key?(n.name)
      case cls
      when "service"
        sh = Noah::Host[n.host_id].name
        hsh["#{n.name}"]["#{sh}"] = Hash.new unless hsh["#{n.name}"].has_key?("#{sh}")
        hsh["#{n.name}"]["#{sh}"].merge!({:id => n.id, :status => n.status, :tags => n.to_hash[:tags]})
      when "host"
        hsh["#{n.name}"].merge!({:id => n.id, :status => n.status, :tags => n.to_hash[:tags], :services => n.to_hash[:services]})
      else
        hsh["#{n.name}"].merge!(n.to_hash.reject{|key, value| rejected_vals.member?(key)})
      end
    end
  end
  h = {:name => name, :hosts => @hosts, :services => @services, :applications => @applications, :configurations => @configurations, :ephemerals => @ephemerals, :created_at => created_at, :updated_at => updated_at}
  super.merge(h)
end

#validateObject


30
31
32
33
34
# File 'lib/noah/models/link.rb', line 30

def validate
  super
  assert_present :path
  assert_unique :path
end