Sha256: 13884373da5a4ca6f61a69bbe76b6035cfb14aaecd28072aaaea3a3e4182813a

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

module God
  
  class Metric
    attr_accessor :watch, :destination, :conditions
    
    def initialize(watch, destination)
      self.watch = watch
      self.destination = destination
      self.conditions = []
    end
    
    # Instantiate a Condition of type +kind+ and pass it into the optional
    # block. Attributes of the condition must be set in the config file
    def condition(kind)
      # create the condition
      begin
        c = Condition.generate(kind, self.watch)
      rescue NoSuchConditionError => e
        abort e.message
      end
      
      # send to block so config can set attributes
      yield(c) if block_given?
      
      # call prepare on the condition
      c.prepare
      
      # abort if the Condition is invalid, the Condition will have printed
      # out its own error messages by now
      unless c.valid?
        abort "Exiting on invalid condition"
      end
      
      # inherit interval from watch if no poll condition specific interval was set
      if c.kind_of?(PollCondition) && !c.interval
        if self.watch.interval
          c.interval = self.watch.interval
        else
          abort "No interval set for Condition '#{c.class.name}' in Watch '#{self.watch.name}', and no default Watch interval from which to inherit"
        end
      end
      
      # remember
      self.conditions << c
    end
    
    def enable
      self.conditions.each do |c|
        Hub.attach(c, self)
      end
    end
    
    def disable
      self.conditions.each do |c|
        Hub.detach(c)
      end
    end
  end
  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
god-0.4.0 lib/god/metric.rb
god-0.4.1 lib/god/metric.rb
god-0.4.3 lib/god/metric.rb
god-0.3.0 lib/god/metric.rb