Class: PulseMeter::Sensor::Configuration

Inherits:
Object
  • Object
show all
Includes:
Mixins::Utils
Defined in:
lib/pulse-meter/sensor/configuration.rb

Instance Method Summary collapse

Methods included from Mixins::Utils

#assert_positive_integer!, #assert_ranged_float!, #camelize, #camelize_keys, #constantize, #symbolize_keys, #titleize, #uniqid

Constructor Details

#initialize(opts = {}) ⇒ Configuration

Returns a new instance of Configuration.



6
7
8
9
10
11
# File 'lib/pulse-meter/sensor/configuration.rb', line 6

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/pulse-meter/sensor/configuration.rb', line 26

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

Instance Method Details

#add_sensor(name, opts) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
# File 'lib/pulse-meter/sensor/configuration.rb', line 13

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

#sensor(name) ⇒ Object



22
23
24
# File 'lib/pulse-meter/sensor/configuration.rb', line 22

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