Class: Speed

Inherits:
Object
  • Object
show all
Defined in:
lib/speed.rb

Overview

This class represents a speed training session.

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Speed

Initialization method for the Speed class. Params:

name

the name of the speed session



12
13
14
15
16
17
18
# File 'lib/speed.rb', line 12

def initialize(name)
  @name = name
  @sport = ''
  @info = ''
  @average_heart_rate = 0
  @total_duration = 0
end

Instance Method Details

#average_heart_rate(heart_rate) ⇒ Object

Adding an average heart rate to the object. Params:

heart_rate

the average heart rate in bpm



42
43
44
45
46
47
48
49
# File 'lib/speed.rb', line 42

def average_heart_rate(heart_rate)
  heart_rate_str = heart_rate.to_s
  heart_rate_int = heart_rate.to_s.to_i
  raise 'The given average heart rate is not an integer.' if heart_rate_str != heart_rate_str.to_i.to_s
  raise 'The average heart rate should be between 60 and 205 bpm.' if heart_rate_int < 60 || heart_rate_int > 205

  @average_heart_rate = heart_rate_int
end

#info(message) ⇒ Object

Adding a message to the object. Params:

message

the info message



34
35
36
# File 'lib/speed.rb', line 34

def info(message)
  @info = message
end

#sport(type) ⇒ Object

Adding a sport type to the object. Params:

type

the type of the sport



24
25
26
27
28
# File 'lib/speed.rb', line 24

def sport(type)
  raise 'The sport name has to be from the Sport class collection.' unless Sport.values.include? type

  @sport = type
end

#to_hashObject

Converting a session to a JSON-ized string.



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/speed.rb', line 72

def to_hash
  hash = {
    name: @name,
    sport:	@sport,
    average_heart_rate: @average_heart_rate,
    total_duration: @total_duration
  }

  hash[:info] = @info if @info != ''
  hash
end

#to_sObject

Converting a speed session to a string.



66
67
68
# File 'lib/speed.rb', line 66

def to_s
  "#{@sport} #{@info}"
end

#total_duration(duration) ⇒ Object

Adding a total duration to the object. Params:

duration

the duration in minutes



55
56
57
58
59
60
61
62
# File 'lib/speed.rb', line 55

def total_duration(duration)
  duration_str = duration.to_s
  duration_int = duration.to_s.to_i
  raise 'The given average heart rate is not an integer.' if duration_str != duration_str.to_i.to_s
  raise 'The duration has to be 1 minute at least.' if duration_int < 1

  @total_duration = duration_int
end