triathlon 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/lib/bike.rb CHANGED
@@ -1,23 +1,8 @@
1
1
  require 'converter'
2
-
3
- class TimePresenter
4
- def self.present_hours_as_string(time)
5
- hours = time.floor
6
- fraction = time - hours
7
- minutes = (60.0 * fraction)
8
- minutes_string = minutes.floor.to_s
9
- minutes_string = "0#{minutes_string}" if minutes < 10.0
10
- fraction = minutes - minutes.floor
11
- seconds = (60.0 * fraction)
12
- seconds_string = seconds.round.to_s
13
- seconds_string = "0#{seconds_string}" if seconds < 10.0
14
- "#{hours}:#{minutes_string}:#{seconds_string}"
15
- end
16
- end
17
-
2
+ require 'time_presenter'
18
3
  class Bike
19
4
  attr_accessor :distance, :speed
20
-
5
+ include TimePresenter
21
6
  def initialize(distance, speed)
22
7
  self.distance = distance.to_f
23
8
  self.speed = speed.to_f
@@ -25,7 +10,7 @@ class Bike
25
10
 
26
11
  def compute_time
27
12
  time = distance / speed # time in hours
28
- TimePresenter.present_hours_as_string(time)
13
+ present_hours_as_string(time)
29
14
  end
30
15
 
31
16
  def to_s
data/lib/converter.rb CHANGED
@@ -1,13 +1,28 @@
1
1
  class Converter
2
2
  SecondsPerHour = 3600
3
3
  SecondsPerMinute = 60
4
+
4
5
  def hours_to_seconds(hours)
5
6
  hours * SecondsPerHour
6
7
  end
8
+
7
9
  def minutes_to_seconds(minutes)
8
10
  minutes * SecondsPerMinute
9
11
  end
12
+
10
13
  def mps_to_mph(mps)
11
14
  mps * SecondsPerHour
12
15
  end
16
+
17
+ def string_to_seconds(time_string)
18
+ pace_times = time_string.split(':').map{|string| string.to_i}
19
+ converter = Converter.new
20
+ if pace_times.size == 3
21
+ @total_pace_in_seconds = converter.hours_to_seconds(pace_times[0]) + converter.minutes_to_seconds(pace_times[1]) + pace_times[2]
22
+ elsif pace_times.size == 2
23
+ @total_pace_in_seconds = converter.minutes_to_seconds(pace_times[0]) + pace_times[1]
24
+ else
25
+ @total_pace_in_seconds = pace_times[0]
26
+ end
27
+ end
13
28
  end
data/lib/splits.rb CHANGED
@@ -1,27 +1,7 @@
1
1
  require 'converter'
2
-
3
- class SplitPresenter
4
- def initialize(seconds)
5
- @total_seconds = seconds.to_f
6
- end
7
-
8
- def to_s
9
- if @total_seconds > 3600.0
10
- return 'NYI'
11
- end
12
-
13
- minutes = (@total_seconds / 60.0).truncate
14
- seconds = (@total_seconds - (minutes * 60.0)).round
15
- seconds_string = seconds.to_s
16
- if seconds < 10
17
- seconds_string = "0#{seconds_string}"
18
- end
19
- return "#{minutes}:#{seconds_string}"
20
- end
21
-
22
- end
23
-
2
+ require 'time_presenter'
24
3
  class Splits
4
+ include TimePresenter
25
5
  def initialize(pace)
26
6
  pace_times = pace.split(':').map{|string| string.to_i}
27
7
  converter = Converter.new
@@ -33,20 +13,12 @@ class Splits
33
13
  @total_pace_in_seconds = pace_times[0]
34
14
  end
35
15
  end
36
-
37
- def get_four_hundred()
38
- return SplitPresenter.new(@total_pace_in_seconds / 4.0)
39
- end
40
-
41
- def get_eight_hundred()
42
- return SplitPresenter.new(@total_pace_in_seconds / 2.0)
43
- end
44
-
45
- def get_one_hundred()
46
- return SplitPresenter.new(@total_pace_in_seconds / 16.0)
16
+ def split(meters)
17
+ ratio = meters.to_f / 1600.0
18
+ split = @total_pace_in_seconds * ratio
19
+ present_seconds_as_string split
47
20
  end
48
-
49
21
  def pace
50
- return SplitPresenter.new(@total_pace_in_seconds)
22
+ present_seconds_as_string @total_pace_in_seconds
51
23
  end
52
24
  end
data/lib/triathlon.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require 'bike'
2
2
  require 'converter'
3
- require 'splits'
3
+ require 'splits'
4
+ require 'swim'
@@ -13,12 +13,8 @@ class TestBike < Test::Unit::TestCase
13
13
  bike = Bike.new(distance, speed)
14
14
  assert_in_delta(distance, bike.distance, 0.001)
15
15
  assert_in_delta(speed, bike.speed, 0.001)
16
-
17
- puts bike
18
-
19
16
  time = bike.compute_time
20
17
  expected_time = "1:04:42"
21
- p time
22
18
  assert_equal(expected_time, time)
23
19
  end
24
20
  end
@@ -22,4 +22,14 @@ class TestConverter < Test::Unit::TestCase
22
22
  expected_mph = 24.5
23
23
  assert_in_delta(expected_mph, mph, 0.001)
24
24
  end
25
+
26
+ should 'convert a time string to seconds' do
27
+ converter = Converter.new
28
+ seconds = converter.string_to_seconds "1:45"
29
+ expected_seconds = 105
30
+ assert expected_seconds == seconds
31
+ seconds = converter.string_to_seconds "5:44"
32
+ expected_seconds = 344
33
+ assert expected_seconds == seconds
34
+ end
25
35
  end
@@ -3,36 +3,31 @@ require 'test_helper'
3
3
  require 'splits'
4
4
 
5
5
  class TestSplits < Test::Unit::TestCase
6
- should 'return split as a string' do
7
- split = SplitPresenter.new(430)
8
- assert_equal split.to_s, "7:10"
9
- end
10
-
11
6
  should 'compute 400m pace' do
12
7
  splits = Splits.new("10:00")
13
- four_hundred = splits.get_four_hundred
14
- assert_equal four_hundred.to_s, "2:30"
8
+ four_hundred = splits.split 400
9
+ assert_equal four_hundred, "2:30"
15
10
  end
16
11
 
17
12
  should 'compute 800m pace' do
18
13
  splits = Splits.new("6:53")
19
- eight_hundred = splits.get_eight_hundred
20
- assert_equal eight_hundred.to_s, "3:27"
14
+ eight_hundred = splits.split 800
15
+ assert_equal eight_hundred, "3:27"
21
16
  end
22
17
 
23
18
  should 'compute 100m pace' do
24
19
  splits = Splits.new("5:00")
25
- one_hundred = splits.get_one_hundred
26
- assert_equal one_hundred.to_s, "0:19"
20
+ one_hundred = splits.split 100
21
+ assert_equal one_hundred, "0:19"
27
22
  end
28
23
 
29
24
  should 'return original pace' do
30
25
  splits = Splits.new("7:15")
31
- assert_equal splits.pace.to_s, "7:15"
26
+ assert_equal splits.pace, "7:15"
32
27
  end
33
28
 
34
29
  should 'present time with seconds less than ten correctly' do
35
30
  splits = Splits.new("6:16")
36
- assert_equal splits.get_eight_hundred.to_s, "3:08"
31
+ assert_equal splits.split(800), "3:08"
37
32
  end
38
33
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: triathlon
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ - 8
10
+ version: 0.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Waylon Calabrese
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-08 00:00:00 -05:00
18
+ date: 2010-09-13 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies: []
21
21