Sha256: afd38835084242b61d9148c4b9981306435655d137aff1bac9424f3e11f9ed42

Contents?: true

Size: 1.85 KB

Versions: 5

Compression:

Stored size: 1.85 KB

Contents

module PulseMeter
  module Sensor
    # @abstract Subclass and override {#event} to implement sensor 
    class Base
      include PulseMeter::Mixins::Dumper

      # @!attribute [rw] redis
      #   @return [Redis] 
      attr_accessor :redis
      # @!attribute [r] name
      #   @return [String] sensor name
      attr_reader :name

      # Initializes sensor and dumps it to redis
      # @param name [String] sensor name
      # @option options [String] :annotation Sensor annotation
      # @raise [BadSensorName] if sensor name is malformed
      # @raise [RedisNotInitialized] unless Redis is initialized
      def initialize(name, options={})
        @name = name.to_s
        if options[:annotation]
          annotate(options[:annotation])
        end
        raise BadSensorName, @name unless @name =~ /\A\w+\z/
        raise RedisNotInitialized unless PulseMeter.redis
        dump!
      end

      # Returns Redis instance
      def redis
        PulseMeter.redis
      end

      # Saves annotation to Redis
      # @param description [String] Sensor annotation
      def annotate(description)
        redis.set(desc_key, description)
      end

      # Retrieves annotation from Redis
      # @return [String] Sensor annotation
      def annotation
        redis.get(desc_key)
      end

      # Cleans up all sensor metadata in Redis
      def cleanup
        redis.del(desc_key)
        cleanup_dump
      end
  
      # @abstract Processes event
      # @param value [Object] value produced by some kind of event
      def event(value)
        # do nothing here
      end

      protected
      
      # Forms Redis key to store annotation
      def desc_key
        "pulse_meter:desc:#{name}"
      end
      
      # For a block
      # @yield Executes it within Redis multi
      def multi
        redis.multi do
          yield
        end
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pulse-meter-0.2.3 lib/pulse-meter/sensor/base.rb
pulse-meter-0.2.2 lib/pulse-meter/sensor/base.rb
pulse-meter-0.2.1 lib/pulse-meter/sensor/base.rb
pulse-meter-0.2.0 lib/pulse-meter/sensor/base.rb
pulse-meter-0.1.11 lib/pulse-meter/sensor/base.rb