Make sensor accept blocks instead of procs #29

Merged
merged 3 commits into from Feb 23, 2013
Jump to file or symbol
Failed to load files and symbols.
+20 −22
Split
@@ -13,12 +13,10 @@
delay = 500.0
-set_delay = Proc.new do |data|
+potentiometer.when_data_received do |data|
puts "DATA: #{delay = data.to_i}"
end
-potentiometer.when_data_received(set_delay)
-
[:on, :off].cycle do |switch|
puts "DELAY: #{seconds = (delay / 1000.0)}"
led.send(switch)
@@ -12,13 +12,11 @@
delay = 500.0
-set_delay = Proc.new do |data|
+potentiometer.when_data_received do |data|
sleep 0.5
puts "DATA: #{delay = data.to_i}"
end
-potentiometer.when_data_received(set_delay)
-
sleep(2)
loop do
puts "DELAY: #{seconds = (delay / 1000.0)}"
@@ -10,10 +10,8 @@
board = Dino::Board.new(Dino::TxRx.new)
sensor = Dino::Components::Sensor.new(pin: 'A0', board: board)
-on_data = Proc.new do |data|
- puts data
+sensor.when_data_received do |data|
+ puts data
end
-sensor.when_data_received(on_data)
-
sleep
@@ -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_data = nil
+ second_block_data = nil
+ sensor.when_data_received do |data|
+ first_block_data = data
+ end
+ sensor.when_data_received do |data|
+ second_block_data = data
+ end
sensor.update('Some data')
+ [first_block_data, second_block_data].each { |block_data| block_data.should == "Some data" }
end
end
end