pulse-meter-client-backport 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,47 @@
1
+ module PulseMeter
2
+ module Sensor
3
+ class Configuration
4
+ include PulseMeter::Mixins::Utils
5
+
6
+ def initialize(opts = {})
7
+ @sensors = {}
8
+ opts.each do |name, opts|
9
+ add_sensor(name, opts)
10
+ end
11
+ end
12
+
13
+ def add_sensor(name, opts)
14
+ sensor_type = opts.respond_to?(:sensor_type) ? opts.sensor_type : opts[:sensor_type]
15
+ klass_s = sensor_class(sensor_type)
16
+ klass = constantize(klass_s)
17
+ raise ArgumentError, "#{klass_s} is not a valid class for a sensor" unless klass
18
+ args = (opts.respond_to?(:args) ? opts.args : opts[:args]) || {}
19
+ @sensors[name.to_s] = klass.new(name, symbolize_keys(args.to_hash))
20
+ end
21
+
22
+ def sensor(name)
23
+ @sensors[name.to_s]
24
+ end
25
+
26
+ def method_missing(name, *args)
27
+ name = name.to_s
28
+ if @sensors.has_key?(name)
29
+ @sensors[name].event(*args)
30
+ else
31
+ raise ArgumentError, "Unknown sensor: `#{name}'"
32
+ end
33
+ end
34
+
35
+ protected
36
+
37
+ def sensor_class(sensor_type)
38
+ entries = sensor_type.to_s.split('/').map do |entry|
39
+ entry.split('_').map(&:capitalize).join
40
+ end
41
+ entries.unshift('PulseMeter::Sensor').join('::')
42
+ end
43
+
44
+
45
+ end
46
+ end
47
+ end
@@ -1,3 +1,3 @@
1
1
  module PulseMeter
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -0,0 +1,83 @@
1
+ require "spec_helper"
2
+
3
+ describe PulseMeter::Sensor::Configuration do
4
+ describe "#add_sensor" do
5
+ let(:cfg) {described_class.new}
6
+
7
+ it "should create sensor available under passed name" do
8
+ cfg.sensor(:foo).should be_nil
9
+ cfg.add_sensor(:foo, :sensor_type =>'counter')
10
+ cfg.sensor(:foo).should_not be_nil
11
+ end
12
+
13
+ it "should event shortcut for the sensor" do
14
+ cfg.add_sensor(:foo, :sensor_type =>'counter')
15
+ sensor = cfg.sensor(:foo)
16
+ sensor.should_receive(:event).with(321)
17
+ cfg.foo(321)
18
+ end
19
+
20
+ it "should create sensor with correct type" do
21
+ cfg.add_sensor(:foo, :sensor_type =>'counter')
22
+ cfg.sensor(:foo).should be_kind_of(PulseMeter::Sensor::Counter)
23
+ end
24
+
25
+ it "should raise exception if sensor type is bad" do
26
+ expect{ cfg.add_sensor(:foo, :sensor_type =>'baaaar') }.to raise_exception(ArgumentError)
27
+ end
28
+
29
+ it "should pass args to created sensor" do
30
+ cfg.add_sensor(:foo, :sensor_type =>'counter', :args =>{:annotation => "My Foo Counter"} )
31
+ cfg.sensor(:foo).annotation.should == "My Foo Counter"
32
+ end
33
+
34
+ it "should accept hashie-objects" do
35
+ class Dummy
36
+ def sensor_type
37
+ 'counter'
38
+ end
39
+ def args
40
+ Hashie::Mash.new(:annotation =>"My Foo Counter")
41
+ end
42
+ end
43
+
44
+ cfg.add_sensor(:foo, Dummy.new)
45
+ cfg.sensor(:foo).annotation.should == "My Foo Counter"
46
+ end
47
+
48
+
49
+ end
50
+
51
+ describe ".new" do
52
+ it "should add passed sensor setting hash using keys as names" do
53
+ opts = {
54
+ :cnt => {
55
+ :sensor_type => 'counter'
56
+ },
57
+ :ind => {
58
+ :sensor_type => 'indicator'
59
+ }
60
+ }
61
+ cfg1 = described_class.new(opts)
62
+ cfg2 = described_class.new
63
+ opts.each{|k,v| cfg2.add_sensor(k, v)}
64
+ cfg1.to_yaml.should == cfg2.to_yaml
65
+ end
66
+ end
67
+
68
+ describe "#sensor" do
69
+ it "should give access to added sensors" do
70
+ opts = {
71
+ :cnt => {
72
+ :sensor_type => 'counter',
73
+ :args => {
74
+ :annotation => "MySensor"
75
+ }
76
+ }
77
+ }
78
+ cfg = described_class.new(opts)
79
+ cfg.sensor(:cnt).annotation.should == "MySensor"
80
+ cfg.sensor("cnt").annotation.should == "MySensor"
81
+ end
82
+ end
83
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pulse-meter-client-backport
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ilya Averyanov
@@ -200,6 +200,7 @@ files:
200
200
  - lib/pulse-meter/mixins/utils.rb
201
201
  - lib/pulse-meter/sensor.rb
202
202
  - lib/pulse-meter/sensor/base.rb
203
+ - lib/pulse-meter/sensor/configuration.rb
203
204
  - lib/pulse-meter/sensor/counter.rb
204
205
  - lib/pulse-meter/sensor/hashed_counter.rb
205
206
  - lib/pulse-meter/sensor/indicator.rb
@@ -216,6 +217,7 @@ files:
216
217
  - spec/pulse_meter/mixins/dumper_spec.rb
217
218
  - spec/pulse_meter/mixins/utils_spec.rb
218
219
  - spec/pulse_meter/sensor/base_spec.rb
220
+ - spec/pulse_meter/sensor/configuration_spec.rb
219
221
  - spec/pulse_meter/sensor/counter_spec.rb
220
222
  - spec/pulse_meter/sensor/hashed_counter_spec.rb
221
223
  - spec/pulse_meter/sensor/indicator_spec.rb
@@ -271,6 +273,7 @@ test_files:
271
273
  - spec/pulse_meter/mixins/dumper_spec.rb
272
274
  - spec/pulse_meter/mixins/utils_spec.rb
273
275
  - spec/pulse_meter/sensor/base_spec.rb
276
+ - spec/pulse_meter/sensor/configuration_spec.rb
274
277
  - spec/pulse_meter/sensor/counter_spec.rb
275
278
  - spec/pulse_meter/sensor/hashed_counter_spec.rb
276
279
  - spec/pulse_meter/sensor/indicator_spec.rb