Permalink
Browse files

Make sensor accept blocks instead of procs

Fixes #28
  • Loading branch information...
1 parent 3c9766d commit 0adfb83a7a0911ec31159364394725ad18765bbf @pcasaretto pcasaretto committed Feb 22, 2013
Showing with 16 additions and 12 deletions.
  1. +2 −2 lib/dino/components/sensor.rb
  2. +14 −10 spec/lib/components/sensor_spec.rb
@@ -7,8 +7,8 @@ def after_initialize(options={})
@board.start_read
end
- def when_data_received(callback)
- @data_callbacks << callback
+ def when_data_received(&block)
+ @data_callbacks << block
end
def update(data)
@@ -32,25 +32,29 @@ module Components
end
describe '#when_data_received' do
- it 'should add a callback back to the list of callbacks' do
+
+ it "should add a callback to the list of callbacks" do
sensor = Sensor.new(board: board, pin: 'a pin')
- sensor.when_data_received 'Foo'
- sensor.instance_variable_get(:@data_callbacks).should == ['Foo']
+ sensor.when_data_received { "this is a block" }
+ sensor.instance_variable_get(:@data_callbacks).should_not be_empty
end
end
describe '#update' do
it 'should call all callbacks passing in the given data' do
- first_callback, second_callback = mock, mock
- first_callback.should_receive(:call).with('Some data')
- second_callback.should_receive(:call).with('Some data')
-
sensor = Sensor.new(board: board, pin: 'a pin')
-
- sensor.when_data_received first_callback
- sensor.when_data_received second_callback
+
+ first_block_called = false
+ second_block_called = false
+ sensor.when_data_received do |data|
+ first_block_called = true
+ end
+ sensor.when_data_received do |data|
+ second_block_called = true
+ end
sensor.update('Some data')
+ fail unless first_block_called && second_block_called
end
end
end

0 comments on commit 0adfb83

Please sign in to comment.