Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TirePressureMonitoringSystem/Ruby: synchronised the code with the ori…
…ginal C# version, minor syntax style improvements.
- Loading branch information
Luca Minudel
committed
Sep 13, 2013
1 parent
fb487a9
commit 614f5f4
Showing
3 changed files
with
36 additions
and
28 deletions.
There are no files selected for viewing
32 changes: 15 additions & 17 deletions
32
TDDMicroExercises/Ruby/tire_pressure_monitoring_system/alarm.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,22 @@ | ||
require_relative "./sensor" | ||
require_relative './sensor' | ||
|
||
class Alarm | ||
LOWPRESSURETHRESHOLD = 17 | ||
HIGHPRESSURETHRESHOLD = 21 | ||
|
||
def initialize | ||
@sensor = Sensor.new | ||
@alarm_on = false | ||
@alarm_count = 0 | ||
end | ||
attr_reader :alarm_on | ||
|
||
def check | ||
psi_pressure_value = @sensor.pop_next_pressure_psi_value | ||
if psi_pressure_value < LOWPRESSURETHRESHOLD || HIGHPRESSURETHRESHOLD < psi_pressure_value | ||
@alarm_on = true | ||
@alarm_count += 1 | ||
end | ||
end | ||
def initialize | ||
@sensor = Sensor.new | ||
@alarm_on = false | ||
end | ||
|
||
def check | ||
pressure = @sensor.pop_next_pressure_psi_value() | ||
|
||
def alarm_on | ||
return @alarm_on | ||
@alarm_on = true if pressure < LOW_PRESSURE || HIGH_PRESSURE < pressure | ||
end | ||
|
||
private | ||
|
||
LOW_PRESSURE = 17 | ||
HIGH_PRESSURE = 21 | ||
end |
9 changes: 9 additions & 0 deletions
9
TDDMicroExercises/Ruby/tire_pressure_monitoring_system/alarm_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require 'test/unit' | ||
require_relative './alarm' | ||
|
||
class AlarmTest < Test::Unit::TestCase | ||
def test_do_something | ||
alarm = Alarm.new | ||
alarm.check | ||
end | ||
end |
23 changes: 12 additions & 11 deletions
23
TDDMicroExercises/Ruby/tire_pressure_monitoring_system/sensor.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
class Sensor | ||
OFFSET = 16; | ||
# The reading of the pressure value from the sensor is simulated in this implementation. | ||
# Because the focus of the exercise is on the other class. | ||
|
||
def pop_next_pressure_psi_value | ||
pressure_telemetry_value = sample_pressure | ||
OFFSET + pressure_telemetry_value; | ||
end | ||
OFFSET = 16 | ||
private_constant :OFFSET | ||
|
||
def sample_pressure | ||
#placeholder implementation that | ||
#simulate a real sensor in a real tire | ||
pressure_telemetry_value = 6 * rand(5) * rand(4) | ||
pressure_telemetry_value | ||
end | ||
def pop_next_pressure_psi_value | ||
OFFSET + read_sample_pressure() | ||
end | ||
|
||
def read_sample_pressure | ||
#Simulate info read from a real sensor in a real tire | ||
6 * rand * rand | ||
end | ||
end |