Sha256: 08132c74b6542cdf9fd510830b78c9e2e1fa2ba551f5129466d8296c59c405e2

Contents?: true

Size: 1.25 KB

Versions: 9

Compression:

Stored size: 1.25 KB

Contents

require 'pulse-meter/extensions/enumerable'

module PulseMeter
  module Mixins
    # Mixin with command-line utilities
    module Cmd
      def with_redis
        PulseMeter.redis = Redis.new :host => options[:host], :port => options[:port], :db => options[:db]
        yield
      end

      def with_safe_restore_of(name, &block)
        with_redis do
          sensor = PulseMeter::Sensor::Base.restore(name)
          block.call(sensor)
        end
      rescue PulseMeter::RestoreError
        fail! "Sensor #{name} is unknown or cannot be restored"
      end

      def all_sensors
        PulseMeter::Sensor::Timeline.list_objects
      end

      def all_sensors_table(format = nil)
        data = [
          ["Name", "Class", "ttl", "raw data ttl", "interval", "reduce delay"],
        ]
        data << :separator unless 'csv' == format.to_s
        all_sensors.each do |s|
          if s.kind_of? PulseMeter::Sensor::Timeline
            data << [s.name, s.class, s.ttl, s.raw_data_ttl, s.interval, s.reduce_delay]
          else
            data << [s.name, s.class] + [''] * 4
          end
        end
        data.to_table(format)
      end

      def fail!(description = nil)
        puts description if description
        exit 1
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pulse-meter-0.4.1 lib/pulse-meter/mixins/cmd.rb
pulse-meter-0.4.0 lib/pulse-meter/mixins/cmd.rb
pulse-meter-0.3.2 lib/pulse-meter/mixins/cmd.rb
pulse-meter-0.3.1 lib/pulse-meter/mixins/cmd.rb
pulse-meter-0.3.0 lib/pulse-meter/mixins/cmd.rb
pulse-meter-0.2.11 lib/pulse-meter/mixins/cmd.rb
pulse-meter-0.2.10 lib/pulse-meter/mixins/cmd.rb
pulse-meter-0.2.9 lib/pulse-meter/mixins/cmd.rb
pulse-meter-0.2.8 lib/pulse-meter/mixins/cmd.rb