Sha256: a0dafe1a6f65c3032605a93bf39a150e8158d4b444e8fcca76ce1b689c2a53d6

Contents?: true

Size: 1.22 KB

Versions: 8

Compression:

Stored size: 1.22 KB

Contents

module PulseMeter
  module Sensor
    class Configuration
      include PulseMeter::Mixins::Utils

      def initialize(opts = {})
        @sensors = {}
        opts.each do |name, opts|
          add_sensor(name, opts)
        end
      end

      def add_sensor(name, opts)
        sensor_type = opts.respond_to?(:sensor_type) ? opts.sensor_type : opts[:sensor_type]
        klass_s = sensor_class(sensor_type)
        klass = constantize(klass_s)
        raise ArgumentError, "#{klass_s} is not a valid class for a sensor" unless klass
        args = (opts.respond_to?(:args) ? opts.args : opts[:args]) || {}
        @sensors[name.to_s] = klass.new(name, symbolize_keys(args.to_hash))
      end

      def sensor(name)
        @sensors[name.to_s]
      end

      def method_missing(name, *args)
        name = name.to_s
        if @sensors.has_key?(name)
          @sensors[name].event(*args)
        else
          raise ArgumentError, "Unknown sensor: `#{name}'"
        end
      end

      protected

      def sensor_class(sensor_type)
        entries = sensor_type.to_s.split('/').map do |entry|
          entry.split('_').map(&:capitalize).join
        end
        entries.unshift('PulseMeter::Sensor').join('::')
      end


    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
pulse-meter-0.2.1 lib/pulse-meter/sensor/configuration.rb
pulse-meter-0.2.0 lib/pulse-meter/sensor/configuration.rb
pulse-meter-client-backport-0.1.4 lib/pulse-meter/sensor/configuration.rb
pulse-meter-0.1.11 lib/pulse-meter/sensor/configuration.rb
pulse-meter-client-backport-0.1.3 lib/pulse-meter/sensor/configuration.rb
pulse-meter-0.1.10 lib/pulse-meter/sensor/configuration.rb
pulse-meter-client-backport-0.1.2 lib/pulse-meter/sensor/configuration.rb
pulse-meter-0.1.9 lib/pulse-meter/sensor/configuration.rb