Class: Interval

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

Overview

This class represents an interval.

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Interval

Initialization method for the Interval class. Params:

name

the name of the interval



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/interval.rb', line 12

def initialize(name)
  @name = name
  @sport = ''
  @info = ''
  @speed_duration = 0
  @recovery_duration = 0
  @speed_heart_rate = 0
  @recovery_heart_rate = 0
  @repetitions = 0
  @type = ''
end

Instance Method Details

#info(message) ⇒ Object

Adding an info message to the object. Params:

message

the info message



38
39
40
# File 'lib/interval.rb', line 38

def info(message)
  @info = message
end

#recovery_duration(recovery_duration) ⇒ Object

Adding a recovery duration of an interval to the object. Params:

recovery_duration

the duration of the recovery segment in minutes



59
60
61
62
63
64
65
66
# File 'lib/interval.rb', line 59

def recovery_duration(recovery_duration)
  recovery_duration_str = recovery_duration.to_s
  recovery_duration_int = recovery_duration.to_s.to_i
  raise 'The given recovery duration is not an integer.' if recovery_duration_str != recovery_duration_str.to_i.to_s
  raise 'The recovery duration has to be 1 minute at least.' if recovery_duration_int < 1

  @recovery_duration = recovery_duration_int
end

#recovery_heart_rate(recovery_heart_rate) ⇒ Object

Adding a recovery heart rate of an interval to the object. Params:

recovery_heart_rate

the average speed heart rate in bpm



85
86
87
88
89
90
91
92
# File 'lib/interval.rb', line 85

def recovery_heart_rate(recovery_heart_rate)
  recovery_hr_str = recovery_heart_rate.to_s
  recovery_hr_int = recovery_heart_rate.to_s.to_i
  raise 'The given average heart rate is not an integer.' if recovery_hr_str != recovery_hr_str.to_i.to_s
  raise 'The recovery heart rate should be between 60 and 205 bpm.' if recovery_hr_int < 60 || recovery_hr_int > 205

  @recovery_heart_rate = recovery_hr_int
end

#repetitions(repetitions) ⇒ Object

Adding a number of repetitions to the object. Params:

repetitions

number of interval repetitions



98
99
100
101
102
103
104
105
# File 'lib/interval.rb', line 98

def repetitions(repetitions)
  repetitions_str = repetitions.to_s
  repetitions_int = repetitions.to_s.to_i
  raise 'The given number of repetitions is not an integer.' if repetitions_str != repetitions_str.to_i.to_s
  raise 'The minimum number of repetitions is 1.' if repetitions_int < 1

  @repetitions = repetitions_int
end

#speed_duration(speed_duration) ⇒ Object

Adding a speed duration of an interval to the object. Params:

speed_duration

the duration of the speed segment in minutes



46
47
48
49
50
51
52
53
# File 'lib/interval.rb', line 46

def speed_duration(speed_duration)
  speed_duration_str = speed_duration.to_s
  speed_duration_int = speed_duration.to_s.to_i
  raise 'The given speed duration is not an integer.' if speed_duration_str != speed_duration_str.to_i.to_s
  raise 'The speed duration has to be 1 minute at least.' if speed_duration_int < 1

  @speed_duration = speed_duration_int
end

#speed_heart_rate(speed_heart_rate) ⇒ Object

Adding a speed heart rate of an interval to the object. Params:

speed_heart_rate

the average speed heart rate in bpm



72
73
74
75
76
77
78
79
# File 'lib/interval.rb', line 72

def speed_heart_rate(speed_heart_rate)
  speed_hr_str = speed_heart_rate.to_s
  speed_hr_int = speed_heart_rate.to_s.to_i
  raise 'The given average heart rate is not an integer.' if speed_hr_str != speed_hr_str.to_i.to_s
  raise 'The speed heart rate should be between 60 and 205 bpm.' if speed_hr_int < 60 || speed_hr_int > 205

  @speed_heart_rate = speed_hr_int
end

#sport(type) ⇒ Object

Adding a sport to the object. Params:

type

the type of the sport



28
29
30
31
32
# File 'lib/interval.rb', line 28

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 an interval to a JSON-ized string.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/interval.rb', line 123

def to_hash
  hash = {
    name: @name,
    sport: @sport,
    info: @info,
    speed_duration: @speed_duration,
    recovery_duration: @recovery_duration,
    speed_heart_rate: @speed_heart_rate,
    recovery_heart_rate: @recovery_heart_rate,
    repetitions: @repetitions,
    type: @type
  }

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

#to_sObject

Converting an interval to a string.



117
118
119
# File 'lib/interval.rb', line 117

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

#type(type) ⇒ Object

Adding an interval type to the object. Params:

type

interval type



111
112
113
# File 'lib/interval.rb', line 111

def type(type)
  @type = type
end