Class: Dash::Models::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/models/base.rb

Direct Known Subclasses

Dashboard, Graph, Host

Constant Summary collapse

@@objects =
Hash.new { |h, k| h[k] = Hash.new }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, params = {}) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
# File 'lib/models/base.rb', line 6

def initialize(name, params={})
  @name = name
  @match_name = name
  @params = params
  @@objects[self.class.to_s][name] = self
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/models/base.rb', line 4

def name
  @name
end

Class Method Details

.allObject



22
23
24
# File 'lib/models/base.rb', line 22

def self.all
  return @@objects[self.name].values
end

.each(&block) ⇒ Object



17
18
19
20
# File 'lib/models/base.rb', line 17

def self.each(&block)
  h = @@objects[self.name] rescue {}
  h.each { |k, v| yield(k, v) }
end

.find(name) ⇒ Object



13
14
15
# File 'lib/models/base.rb', line 13

def self.find(name)
  return @@objects[self.name][name] rescue []
end

Instance Method Details

#<=>(other) ⇒ Object



59
60
61
# File 'lib/models/base.rb', line 59

def <=>(other)
  return to_s <=> other.to_s
end

#[](key) ⇒ Object



26
27
28
# File 'lib/models/base.rb', line 26

def [](key)
  return @params[key] rescue []
end

#compose_metric(m, c, h) ⇒ Object

compose a metric using a :metric_format format string with %c for metric, %c for cluster, and %h for host



69
70
71
# File 'lib/models/base.rb', line 69

def compose_metric (m, c, h)
  @params[:metric_format].dup.gsub("%m", m).gsub("%c", c).gsub("%h", h)
end

#match(glob) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/models/base.rb', line 30

def match(glob)
  return true if glob == '*'
  # convert glob to a regular expression
  glob_parts = glob.split('*').collect { |s| Regexp.escape(s) }
  if glob[0].chr == '*'
    glob_re = /^.*#{glob_parts.join('.*')}$/
  elsif glob[-1].chr == '*'
    glob_re = /^#{glob_parts.join('.*')}.*$/
  else
    glob_re = /^#{glob_parts.join('.*')}$/
  end
  return @match_name.match(glob_re)
end

#multi_match(globs) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/models/base.rb', line 44

def multi_match(globs)
  ret = false

  globs.each do |glob|
    ret = match(glob)
    break if ret
  end

  return ret
end

#to_sObject



55
56
57
# File 'lib/models/base.rb', line 55

def to_s
  return @name
end

#update_params(hash) ⇒ Object



63
64
65
# File 'lib/models/base.rb', line 63

def update_params(hash)
  @params.merge!(hash)
end