class Salus::Group

Public Class Methods

new(defaults={}, &block) click to toggle source
# File lib/salus/group.rb, line 18
def initialize(defaults={}, &block)
  @_metrics = {}
  @_groups  = {}
  @_cache   = {}
  @_proc    = block
  @_opts    = defaults.clone
end

Public Instance Methods

default(opts) click to toggle source
# File lib/salus/group.rb, line 47
def default(opts)
  return unless opts.is_a?(Hash)
  opts.each do |k, v|
    next if [:value, :timestamp].include?(k)
    @_opts[k] = v
  end
end
each(allow_mute=false, &block) click to toggle source
# File lib/salus/group.rb, line 99
def each(allow_mute=false, &block)
  synchronize do
    if allow_mute
      @_metrics.each(&block)
    else
      @_metrics.select { |k, v| !v.mute? }.each(&block)
    end
  end
end
group(title, &block) click to toggle source
# File lib/salus/group.rb, line 55
def group(title, &block)
  synchronize do
    unless @_groups.key?(title)
      @_groups[title] = Group.new(@_opts, &block)
      if @_cache.key?(title)
        @_groups[title].load(@_cache[title])
        @_cache.delete(title)
      end
    end
  end
end
groups() click to toggle source
# File lib/salus/group.rb, line 67
def groups
  synchronize { @_groups }
end
has_subgroups?() click to toggle source
# File lib/salus/group.rb, line 71
def has_subgroups?
  synchronize { !@_groups.empty? }
end
keys(allow_mute=false) click to toggle source
# File lib/salus/group.rb, line 79
def keys(allow_mute=false)
  synchronize do
    if allow_mute
      @_metrics.keys
    else
      @_metrics.keys.select { |x| !@_metrics[x].mute? }
    end
  end
end
load(data) click to toggle source
# File lib/salus/group.rb, line 109
def load(data)
  return unless data
  return if data.empty?
  synchronize do
    if data.key?(:defaults)
      @_opts = data[:defaults].clone
    end
    if data.key?(:metrics)
      types = Metric.descendants.map{ |x| x.name.split("::").last }
      data[:metrics].each do |k, v|
        next unless v.key?(:type)
        next unless types.include?(v[:type])
        @_metrics[k] = Object.const_get("Salus::" + v[:type]).new(@_opts)
        @_metrics[k].load(v)
      end
    end
    if data.key?(:groups)
      @_cache = data[:groups]
    end
  end
end
on_win?() click to toggle source
# File lib/salus/group.rb, line 39
def on_win?
  Salus.on_win?
end
save() click to toggle source
# File lib/salus/group.rb, line 131
def save
  to_h
end
tick() click to toggle source
# File lib/salus/group.rb, line 153
def tick
  instance_eval(&@_proc)
  @_groups.each do |k, v|
    v.tick
  end
  @_cache.clear unless @_cache.empty?
end
to_h() click to toggle source
# File lib/salus/group.rb, line 135
def to_h
  ret = {}
  synchronize do
    unless @_metrics.empty?
      ret[:metrics] = {}
      @_metrics.each { |k, v| ret[:metrics][k] = v.to_h }
    end
    unless @_groups.empty?
      ret[:groups]  = {}
      @_groups.each  { |k, v| ret[:groups][k]  = v.to_h }
    end
    unless @_opts.empty?
      ret[:defaults] = @_opts
    end
    ret
  end
end
value(title) click to toggle source
# File lib/salus/group.rb, line 75
def value(title)
  synchronize { @_metrics.key?(title) ? @_metrics[title].value : nil }
end
values(allow_mute=false) click to toggle source
# File lib/salus/group.rb, line 89
def values(allow_mute=false)
  synchronize do
    if allow_mute
      @_metrics.values
    else
      @_metrics.values.select { |x| !x.mute? }
    end
  end
end
var(arg, default=nil, &block) click to toggle source
# File lib/salus/group.rb, line 43
def var(arg, default=nil, &block)
  Salus.var(arg, default, &block)
end