class Naplug::PerformanceData

Constants

FIELDS

Attributes

data[R]
meta[R]
tag[R]

Public Class Methods

new(plugin) click to toggle source
# File lib/naplug/performancedata.rb, line 13
def initialize(plugin)
  @tag = plugin.tag
  @data = Hash.new
  @meta = OpenStruct.new :plugin => plugin, :ancestors => traverse_to_root(plugin)
end

Public Instance Methods

[](label) click to toggle source
# File lib/naplug/performancedata.rb, line 47
def [](label)
  @data[curate_label(label)]
end
[]=(label,valuefields) click to toggle source

@raise Naplug::Error if the label contains invalid characters, the value is not a number, or specify invalid fields

# File lib/naplug/performancedata.rb, line 34
def []=(label,valuefields)
  value, fields = valuefields
  if validate_label label and validate_value value and validate_fields fields
    @data[curate_label(label)] = { :label => curate_label(label), :value => value }.merge fields
  else
    raise Naplug::Error, "invalid performance data label (#{label}), value (#{value}), field representation (#{fields.class}) or field (#{fields.keys.join(',')})"
  end
end
ancestors(options = { :mode => :tags, :separator => :/ }) click to toggle source

@return [Array<Plugin>] of parent plugins

# File lib/naplug/performancedata.rb, line 75
def ancestors(options = { :mode => :tags, :separator => :/ })
  options[:separator] = :/ if options[:separator].nil?
  options[:mode].nil? ? @meta.ancestors : @meta.ancestors.map { |a| a.tag }.join(options[:separator].to_s)
end
delete(label) click to toggle source
# File lib/naplug/performancedata.rb, line 51
def delete(label)
  @data.delete(curate_label(label))
end
each(&block) click to toggle source
# File lib/naplug/performancedata.rb, line 43
def each(&block)
  @data.values.each(&block)
end
fields() click to toggle source
# File lib/naplug/performancedata.rb, line 65
def fields
  FIELDS
end
has_label?(label)
Alias for: include?
include?(label) click to toggle source
# File lib/naplug/performancedata.rb, line 55
def include?(label)
  @data.has_key? curate_label(label)
end
Also aliased as: has_label?
keys() click to toggle source
# File lib/naplug/performancedata.rb, line 60
def keys
  @data.keys
end
Also aliased as: labels
labels()
Alias for: keys
plugin() click to toggle source

@return [Plugin] plugin performance data belongs to

# File lib/naplug/performancedata.rb, line 70
def plugin
  @meta.plugin
end
to_a() click to toggle source

List of performance data label entries @return [Array<Hash<label,field_data>>] an array of hashes keyed by field

# File lib/naplug/performancedata.rb, line 29
def to_a
  @data.values
end
to_str(label = nil) click to toggle source

performance data format: 'label=value;[warn];;[min];'

# File lib/naplug/performancedata.rb, line 20
def to_str(label = nil)
  label_ary = label.nil? ? labels : [curate_label(label)]
  label_ary.map do |l|
    '%s=%s%s;%s;%s;%s;%s' % FIELDS.map { |k| @data[l][k] }
  end.join(' ').strip
end

Private Instance Methods

curate_label(l) click to toggle source

single quotes for the label are optional; required if spaces are in the label

# File lib/naplug/performancedata.rb, line 96
def curate_label(l)
  l.to_s.index(/\s+/) ? "'#{l}'" : l.to_s
end
traverse_to_root(plugin) click to toggle source
# File lib/naplug/performancedata.rb, line 100
def traverse_to_root(plugin)
  lineage = []
  loop do
    break unless plugin.is_a? Plugin
    lineage.push plugin
    plugin = plugin.parent
  end
  lineage.reverse
end
validate_fields(fields) click to toggle source
# File lib/naplug/performancedata.rb, line 91
def validate_fields(fields)
  fields.is_a? Hash and fields.keys.select { |field| not FIELDS.include? field }.empty? ? true : false
end
validate_label(l) click to toggle source

can contain any characters except the equals sign or single quote (')

# File lib/naplug/performancedata.rb, line 83
def validate_label(l)
  l.nil? or l.to_s.index(/['=]/) ? false : true
end
validate_value(v) click to toggle source
# File lib/naplug/performancedata.rb, line 87
def validate_value(v)
  true #      v =~ /^[0-9.-]+$/ ? true : false
end