ast-tdl 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2f857804cef121dd1c5f2a8e41ab0d27098517e03ee15c8316bc0e62d7707093
4
- data.tar.gz: 2235c16bf905edf99c00693477e5b9c0f037b084c39ca541bec49f00031f9cdb
3
+ metadata.gz: 1ebd58255ecf60ba24423a07251499fb473c2751363795b9058a99a015a5efae
4
+ data.tar.gz: f157c466e4feb81fa9ff3f50d65087f236c73083e854ed579f8a616888fa4c0b
5
5
  SHA512:
6
- metadata.gz: 62436ef8167db994e207f1309c7a41a9b022ceae7ca85f82f57b9f630134965859ceb845953b99c5858a3a16613961ba04507451252a27597da9702e86458655
7
- data.tar.gz: 10e94fd67a4301721b0b600b4786fe73d5527493a3c192d85e1f7d8c290fe376a79f4e3ebab62f63eba8748b7af025fbf59cd1d4203b608634e5f0ae31f644b5
6
+ metadata.gz: '08a3b71fd3c412338bb93d6c9aef954bd7a357e8e6dc190560b977ad7b7a0f9b4f4252ced3c5aedaf152ae3853023efc85b4663a2d60aaee24e264eab14ef6fa'
7
+ data.tar.gz: 6a4a15557718d2abf418b9076f8075344d16308a2478e56a0c90deed727896ade8b539f69a1de270e77e3b0f8a75fb57433860747a5064d901e4479d4a53b341
data/lib/ast-tdl.rb CHANGED
@@ -1 +1 @@
1
- require 'ast'
1
+ require_relative "./ast"
data/lib/ast.rb CHANGED
@@ -1,49 +1,82 @@
1
- require 'json'
1
+ require "json"
2
2
 
3
- require_relative 'classes'
4
- require_relative 'interval'
5
- require_relative 'session'
3
+ require_relative "classes"
4
+ require_relative "interval"
5
+ require_relative "session"
6
6
 
7
7
 
8
+ ##
9
+ # This module is intended to be used for building trainings
10
+ # from the domain specific language AST-TDL.
8
11
  module Ast
12
+ ##
13
+ # Building a new training from the domain specific language.
14
+ # Params:
15
+ # +name+:: the name of the training
16
+ # +block+:: training data
9
17
  def self.build(name, &block)
10
18
  training = Training.new(name)
11
19
  training.instance_eval(&block)
12
20
  return training
13
21
  end
14
22
 
23
+
24
+ ##
25
+ # This class represents a training with a name, sessions and intervals.
15
26
  class Training
27
+ ##
28
+ # Initialization method for the +Training+ class.
29
+ # Params:
30
+ # +name+:: the name of the training
16
31
  def initialize(name)
17
32
  @name = name
18
33
  @session = []
19
34
  @interval = []
20
35
  end
21
36
 
37
+ ##
38
+ # Building a new session from the domain specific language.
39
+ # Params:
40
+ # +name+:: the name of the session
41
+ # +block+:: session data
22
42
  def session(name, &block)
23
43
  training_type = Session.new(name)
24
44
  training_type.instance_eval(&block)
25
45
  @session << training_type
26
46
  end
27
47
 
48
+ ##
49
+ # Building a new interval from the domain specific language.
50
+ # Params:
51
+ # +name+:: the name of the interval
52
+ # +block+:: interval data
28
53
  def interval(name, &block)
29
54
  interval_data = Interval.new(name)
30
55
  interval_data.instance_eval(&block)
31
56
  @interval << interval_data
32
57
  end
33
58
 
59
+ ##
60
+ # Converting a training to a string.
34
61
  def to_s
35
62
  str = "#{@name} #{@session[0]}"
36
63
  end
37
64
 
65
+ ##
66
+ # Converting a training to a JSON-ized string.
38
67
  def json
39
68
  training_json = {
40
69
  name: @name,
41
- session: @session.collect { |x| x.to_hash },
42
- interval: @interval.collect { |x| x.to_hash },
70
+ session: @session.collect { |s| s.to_hash },
71
+ interval: @interval.collect { |i| i.to_hash },
43
72
  }
44
73
  training_json.to_json
45
74
  end
46
75
 
76
+ ##
77
+ # Saving a training to a JSON file.
78
+ # Params:
79
+ # +filename+:: the desired name of the file
47
80
  def save_to_file(filename)
48
81
  f = File.open(filename, "w")
49
82
  f.puts(json)
data/lib/classes.rb CHANGED
@@ -1,13 +1,22 @@
1
- require 'json'
1
+ require "json"
2
2
 
3
3
 
4
+ ##
5
+ # This class represents a sport with a type.
4
6
  class Sport
7
+ # The type of a sport.
5
8
  attr_reader :type
6
9
 
10
+ ##
11
+ # Initialization method for the +Sport+ class.
12
+ # Params:
13
+ # +type+:: the type of the sport
7
14
  def initialize(type)
8
15
  @type = type
9
16
  end
10
17
 
18
+ ##
19
+ # Converting +Sport+ class to a hash.
11
20
  def to_hash
12
21
  sport_json = {
13
22
  type: @type
@@ -15,13 +24,23 @@ class Sport
15
24
  end
16
25
  end
17
26
 
27
+
28
+ ##
29
+ # This class represents information with a message.
18
30
  class Info
31
+ # The info message.
19
32
  attr_reader :message
20
33
 
34
+ ##
35
+ # Initialization method for the +Info+ class.
36
+ # Params:
37
+ # +message+:: the info message
21
38
  def initialize(message)
22
39
  @message = message
23
40
  end
24
41
 
42
+ ##
43
+ # Converting +Info+ class to a hash.
25
44
  def to_hash
26
45
  info_json = {
27
46
  message: @message
@@ -29,13 +48,28 @@ class Info
29
48
  end
30
49
  end
31
50
 
32
- class Avhr
51
+
52
+ ##
53
+ # This class represents an average heart rate.
54
+ class AverageHeartRate
55
+ # An average heart rate.
33
56
  attr_reader :heart_rate
34
57
 
58
+ ##
59
+ # Initialization method for the +AverageHeartRate+ class.
60
+ # Params:
61
+ # +heart_rate+:: the average heart rate
35
62
  def initialize(heart_rate)
63
+ heart_rate = heart_rate.to_s.to_i
64
+ if heart_rate > 220
65
+ raise ArgumentError.new("Maximum heart rate is 220.")
66
+ end
67
+
36
68
  @heart_rate = heart_rate
37
69
  end
38
70
 
71
+ ##
72
+ # Converting +AverageHeartRate+ class to a hash.
39
73
  def to_hash
40
74
  info_json = {
41
75
  average_hr: @heart_rate
@@ -43,13 +77,23 @@ class Avhr
43
77
  end
44
78
  end
45
79
 
46
- class Td
80
+
81
+ ##
82
+ # This class represents a total duration of a session or an interval.
83
+ class TotalDuration
84
+ # The total duration in seconds.
47
85
  attr_reader :duration
48
86
 
87
+ ##
88
+ # Initialization method for the +TotalDuration+ class.
89
+ # Params:
90
+ # +duration+:: the total duration
49
91
  def initialize(duration)
50
92
  @duration = duration
51
93
  end
52
94
 
95
+ ##
96
+ # Converting +TotalDuration+ class to a hash.
53
97
  def to_hash
54
98
  td_json = {
55
99
  duration: @duration
@@ -57,30 +101,56 @@ class Td
57
101
  end
58
102
  end
59
103
 
60
- class AvhrRest
61
- attr_reader :avhr_rest
62
104
 
63
- def initialize(avhr_rest)
64
- @avhr_rest = avhr_rest
105
+ ##
106
+ # This class represents an average heart rate in the resting period.
107
+ class AverageHeartRateRest
108
+ # The average rest heart rate.
109
+ attr_reader :average_heart_rate_rest
110
+
111
+ ##
112
+ # Initialization method for the +AverageHeartRateRest+ class.
113
+ # Params:
114
+ # +average_heart_rate_rest+:: the average heart rate
115
+ def initialize(average_heart_rate_rest)
116
+ average_heart_rate_rest = average_heart_rate_rest.to_s.to_i
117
+ if average_heart_rate_rest > 220
118
+ raise ArgumentError.new("Maximum heart rate is 220.")
119
+ end
120
+
121
+ @average_heart_rate_rest = average_heart_rate_rest
65
122
  end
66
123
 
124
+ ##
125
+ # Converting +AverageHeartRateRest+ class to a hash.
67
126
  def to_hash
68
- avhr_json = {
69
- average_hr_rest: @avhr_rest
127
+ average_heart_rate_json = {
128
+ average_heart_rate_rest: @average_heart_rate_rest
70
129
  }
71
130
  end
72
131
  end
73
132
 
74
- class TdRest
75
- attr_reader :td_rest
76
133
 
77
- def initialize(td_rest)
78
- @td_rest = td_rest
134
+ ##
135
+ # This class represents a total duration of a session or an
136
+ # interval in the resting period.
137
+ class TotalDurationRest
138
+ # The total duration in seconds.
139
+ attr_reader :total_duration_rest
140
+
141
+ ##
142
+ # Initialization method for the +TotalDurationRest+ class.
143
+ # Params:
144
+ # +total_duration_rest+:: the total rest duration
145
+ def initialize(total_duration_rest)
146
+ @total_duration_rest = total_duration_rest
79
147
  end
80
148
 
149
+ ##
150
+ # Converting +TotalDurationRest+ class to a hash.
81
151
  def to_hash
82
- td_json = {
83
- duration_rest: @td_rest
152
+ total_duration_rest_json = {
153
+ total_duration_rest: @total_duration_rest
84
154
  }
85
155
  end
86
- end
156
+ end
data/lib/interval.rb CHANGED
@@ -1,51 +1,85 @@
1
+ ##
2
+ # This class represents an interval.
1
3
  class Interval
4
+ ##
5
+ # Initialization method for the +Interval+ class.
6
+ # Params:
7
+ # +name+:: the name of the interval
2
8
  def initialize(name)
3
9
  @name = []
4
10
  @sport = []
5
11
  @info = []
6
- @avhr = []
7
- @td = []
8
- @avhr_rest = []
9
- @td_rest = []
12
+ @average_heart_rate = []
13
+ @total_duration = []
14
+ @average_heart_rate_rest = []
15
+ @total_duration_rest = []
10
16
  end
11
17
 
18
+ ##
19
+ # Building a new Sport object.
20
+ # Params:
21
+ # +type+:: the type of the sport
12
22
  def sport(type)
13
23
  @sport << Sport.new(type)
14
24
  end
15
25
 
26
+ ##
27
+ # Building a new Info object.
28
+ # Params:
29
+ # +message+:: the info message
16
30
  def info(message)
17
31
  @info << Info.new(message)
18
32
  end
19
33
 
20
- def avhr(heart_rate)
21
- @avhr << Avhr.new(heart_rate)
34
+ ##
35
+ # Building a new AverageHeartRate object.
36
+ # Params:
37
+ # +heart_rate+:: the average heart rate in bpm
38
+ def average_heart_rate(heart_rate)
39
+ @average_heart_rate << AverageHeartRate.new(heart_rate)
22
40
  end
23
41
 
24
- def td(duration)
25
- @td << Td.new(duration)
42
+ ##
43
+ # Building a new TotalDuration object.
44
+ # Params:
45
+ # +duration+:: the duration in seconds
46
+ def total_duration(duration)
47
+ @total_duration << TotalDuration.new(duration)
26
48
  end
27
49
 
28
- def avhr_rest(heart_rate_rest)
29
- @avhr_rest << AvhrRest.new(heart_rate_rest)
50
+ ##
51
+ # Building a new AverageHeartRateRest object.
52
+ # Params:
53
+ # +heart_rate_rest+:: the average rest heart rate in bpm
54
+ def average_heart_rate_rest(heart_rate_rest)
55
+ @average_heart_rate_rest << AverageHeartRateRest.new(heart_rate_rest)
30
56
  end
31
57
 
32
- def td_rest(duration_rest)
33
- @td_rest << TdRest.new(duration_rest)
58
+ ##
59
+ # Building a new TotalDurationRest object.
60
+ # Params:
61
+ # +duration rest+:: the rest duration in seconds
62
+ def total_duration_rest(duration_rest)
63
+ @total_duration_rest << TotalDurationRest.new(duration_rest)
34
64
  end
35
65
 
66
+ ##
67
+ # Converting an interval to a string.
36
68
  def to_s
37
69
  str = "#{@sport[0].type} #{@info[0].message}"
38
70
  end
39
71
 
72
+ ##
73
+ # Converting an interval to a JSON-ized string.
40
74
  def to_hash
41
75
  interval_json = {
42
- name: @name.collect { |x| x.to_hash },
43
- sport: @sport.collect { |x| x.to_hash },
44
- info: @info.collect { |x| x.to_hash },
45
- average_hr: @avhr.collect { |x| x.to_hash },
46
- duration: @td.collect { |x| x.to_hash },
47
- average_hr_rest: @avhr_rest.collect { |x| x.to_hash },
48
- duration_rest: @td_rest.collect { |x| x.to_hash },
76
+ name: @name.collect { |x| x.to_hash },
77
+ sport: @sport.collect { |x| x.to_hash },
78
+ info: @info.collect { |x| x.to_hash },
79
+ average_heart_rate: @average_heart_rate.collect { |x| x.to_hash },
80
+ total_duration: @total_duration.collect { |x| x.to_hash },
81
+ average_heart_rate_rest: @average_heart_rate_rest.collect { |x| x.to_hash },
82
+ total_duration_rest: @total_duration_rest.collect { |x| x.to_hash },
49
83
  }
50
84
  end
51
85
  end
data/lib/session.rb CHANGED
@@ -1,42 +1,65 @@
1
- require 'json'
2
-
3
-
1
+ ##
2
+ # This class represents a session.
4
3
  class Session
4
+ ##
5
+ # Initialization method for the +Session+ class.
6
+ # Params:
7
+ # +name+:: the name of the session
5
8
  def initialize(name)
6
9
  @name = []
7
10
  @sport = []
8
11
  @info = []
9
- @avhr = []
10
- @td = []
12
+ @average_heart_rate = []
13
+ @total_duration = []
11
14
  end
12
15
 
16
+ ##
17
+ # Building a new Sport object.
18
+ # Params:
19
+ # +type+:: the type of the sport
13
20
  def sport(type)
14
21
  @sport << Sport.new(type)
15
22
  end
16
23
 
24
+ ##
25
+ # Building a new Info object.
26
+ # Params:
27
+ # +message+:: the info message
17
28
  def info(message)
18
29
  @info << Info.new(message)
19
30
  end
20
31
 
21
- def avhr(heart_rate)
22
- @avhr << Avhr.new(heart_rate)
32
+ ##
33
+ # Building a new AverageHeartRate object.
34
+ # Params:
35
+ # +heart_rate+:: the average heart rate in bpm
36
+ def average_heart_rate(heart_rate)
37
+ @average_heart_rate << AverageHeartRate.new(heart_rate)
23
38
  end
24
39
 
25
- def td(duration)
26
- @td << Td.new(duration)
40
+ ##
41
+ # Building a new TotalDuration object.
42
+ # Params:
43
+ # +duration+:: the duration in seconds
44
+ def total_duration(duration)
45
+ @total_duration << TotalDuration.new(duration)
27
46
  end
28
47
 
48
+ ##
49
+ # Converting a session to a string.
29
50
  def to_s
30
51
  str = "#{@sport[0].type} #{@info[0].message}"
31
52
  end
32
53
 
54
+ ##
55
+ # Converting a session to a JSON-ized string.
33
56
  def to_hash
34
57
  session_json = {
35
- name: @name.collect { |x| x.to_hash },
36
- sport: @sport.collect { |x| x.to_hash },
37
- info: @info.collect { |x| x.to_hash },
38
- average_hr: @avhr.collect { |x| x.to_hash },
39
- duration: @td.collect { |x| x.to_hash },
58
+ name: @name.collect { |x| x.to_hash },
59
+ sport: @sport.collect { |x| x.to_hash },
60
+ info: @info.collect { |x| x.to_hash },
61
+ average_heart_rate: @average_heart_rate.collect { |x| x.to_hash },
62
+ total_duration: @total_duration.collect { |x| x.to_hash },
40
63
  }
41
64
  end
42
65
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ast-tdl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - firefly-cpp