Class: Puppet::Util::Metric
- Includes:
- Network::FormatSupport, PsychSupport
- Defined in:
- lib/puppet/util/metric.rb
Overview
A class for handling metrics. This is currently ridiculously hackish.
Instance Attribute Summary collapse
-
#label ⇒ Object
Returns the value of attribute label.
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
- #values ⇒ Object
Class Method Summary collapse
- .from_data_hash(data) ⇒ Object
-
.labelize(name) ⇒ Object
Convert a name into a label.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Return a specific value.
-
#initialize(name, label = nil) ⇒ Metric
constructor
A new instance of Metric.
- #initialize_from_hash(data) ⇒ Object
- #newvalue(name, value, label = nil) ⇒ Object
- #to_data_hash ⇒ Object
Methods included from Network::FormatSupport
included, #mime, #render, #support_format?, #to_json, #to_msgpack, #to_pson
Methods included from PsychSupport
Constructor Details
#initialize(name, label = nil) ⇒ Metric
Returns a new instance of Metric.
42 43 44 45 46 47 48 |
# File 'lib/puppet/util/metric.rb', line 42 def initialize(name,label = nil) @name = name.to_s @label = label || self.class.labelize(name) @values = [] end |
Instance Attribute Details
#label ⇒ Object
Returns the value of attribute label.
10 11 12 |
# File 'lib/puppet/util/metric.rb', line 10 def label @label end |
#name ⇒ Object
Returns the value of attribute name.
10 11 12 |
# File 'lib/puppet/util/metric.rb', line 10 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
10 11 12 |
# File 'lib/puppet/util/metric.rb', line 10 def type @type end |
#value ⇒ Object
Returns the value of attribute value.
10 11 12 |
# File 'lib/puppet/util/metric.rb', line 10 def value @value end |
#values ⇒ Object
56 57 58 |
# File 'lib/puppet/util/metric.rb', line 56 def values @values.sort { |a, b| a[1] <=> b[1] } end |
Class Method Details
.from_data_hash(data) ⇒ Object
13 14 15 16 17 |
# File 'lib/puppet/util/metric.rb', line 13 def self.from_data_hash(data) metric = allocate metric.initialize_from_hash(data) metric end |
.labelize(name) ⇒ Object
Convert a name into a label.
61 62 63 |
# File 'lib/puppet/util/metric.rb', line 61 def self.labelize(name) name.to_s.capitalize.gsub("_", " ") end |
Instance Method Details
#[](name) ⇒ Object
Return a specific value
34 35 36 37 38 39 40 |
# File 'lib/puppet/util/metric.rb', line 34 def [](name) if value = @values.find { |v| v[0] == name } return value[2] else return 0 end end |
#initialize_from_hash(data) ⇒ Object
19 20 21 22 23 |
# File 'lib/puppet/util/metric.rb', line 19 def initialize_from_hash(data) @name = data['name'] @label = data['label'] || self.class.labelize(@name) @values = data['values'] end |
#newvalue(name, value, label = nil) ⇒ Object
50 51 52 53 54 |
# File 'lib/puppet/util/metric.rb', line 50 def newvalue(name,value,label = nil) raise ArgumentError.new("metric name #{name.inspect} is not a string") unless name.is_a? String label ||= self.class.labelize(name) @values.push [name,label,value] end |
#to_data_hash ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/puppet/util/metric.rb', line 25 def to_data_hash { 'name' => @name, 'label' => @label, 'values' => @values } end |