runby_pace 0.4.79 → 0.4.81

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NzlmMjQyY2NiMTU3ZDg2NzJlOWM1MzA1YjA5ZjFmNmE3MjBmMThmMg==
4
+ NmNmZDQzMjZjOWJmNGEzYWQzMzdkZjVmNDMyMTQ1ZWZkZjk3ZWMxZg==
5
5
  data.tar.gz: !binary |-
6
- NTIwZWQ5ODk2MGQ5N2Q4YTNlZWE2NzJhNzkyNGJmMjk1NTFiZDEyMA==
6
+ YjYzYTBjZTUwNjc4NGNlMGNlN2EyNWVkNmNjYjM4OWViZmIyYzFiMA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZWIxYjNmZjY4ZDA4MWZiNzg3NjA0NDNiNDI4NTNkNzE0NzJlMjQ4ZjJiNTk2
10
- OTMxNzc4NzBhNzIzMWM4MjMxOWMwMjg4Nzg0MDkwYTNmMGNkYmM4MzYwNTU4
11
- ZjBjMTY3OTRhMmMwNWJlOTE4ZWVjZGM2MDJjNjcwYWZjNzUzMzg=
9
+ ZTBhNmVjNjBiMmQyODgxYTliNzQ1YTkzMjEwMmZiNDM4MTFhZmVlYjhhNzBk
10
+ ODYxY2Y0ODdkNGE2YTcwM2QzOTMwZmMwMDMxZDJlN2MwYjgwOTllZmFkODE4
11
+ ZGNmYjYzZThiN2Y1NzVhOTk1MGJkNTEzMGJkMjNhMzI5MThiOWQ=
12
12
  data.tar.gz: !binary |-
13
- ZGQ0ZTY5YmFlMGQ5YjE2YmRhZGY5MTk0MzZkNDE2ZDA4OTJjZmZmNDAwZWE5
14
- MzMzOTY5ZjNiMWZlNWQ0NjBjMmY2ODYyNzljY2VkODZhMmY2NGIzZWM0NTJh
15
- NjAzZmE4NzU0YTk3NjcwZTNkYzFjZmM0ZmIwMWQ4ODNlYmMzZWY=
13
+ NGVmYmMzM2EyOTMxY2EyYWY4NjNkZDM0ZGZhZWE5ODgyNjkzMWNiYzQxMDhj
14
+ ZDViYjRiZGRlZmVjZDA0YzYzZWY4ZTA3NmIzZmRlOGM5YTBjNmEzMTNmZTU2
15
+ YWYyYTg4ZDE3OTU2NjZlZjFmNmEzYTczMTNlZGIwODI1ZTc3YzA=
@@ -0,0 +1,64 @@
1
+ module Runby
2
+ # Represents a distance (distance UOM and multiplier)
3
+ class Distance
4
+ attr_reader :uom, :multiplier
5
+ def initialize(distance_uom = :km, multiplier = 1)
6
+ # TODO: Test and cleanup
7
+ if distance_uom.is_a? Distance
8
+ return init_from_clone distance_uom
9
+ end
10
+
11
+ raise 'Invalid distance unit of measure' unless [String, Symbol].include? distance_uom.class
12
+ raise 'Invalid multiplier' unless multiplier.is_a?(Numeric)
13
+
14
+ if distance_uom.is_a? Symbol
15
+ raise "Unknown unit of measure #{distance_uom}" unless Runby::PaceUnits.known_uom? distance_uom
16
+ @uom = distance_uom
17
+ @multiplier = multiplier * 1.0
18
+ return
19
+ end
20
+
21
+ distance = Distance.parse distance_uom
22
+ @uom = distance.uom
23
+ @multiplier = distance.multiplier
24
+ end
25
+
26
+ def meters
27
+ kilometers = @multiplier * Runby::PaceUnits.distance_conversion_factor(@uom)
28
+ kilometers * 1000.0
29
+ end
30
+
31
+ def self.parse(str)
32
+ str = str.strip.chomp.downcase
33
+ # TODO: handle multipliers with commas/spaces
34
+ multiplier = str.scan(/[\d,.]+/).first.to_f
35
+ uom = str.scan(/[-_a-z ]+$/).first
36
+ # TODO: test V
37
+ raise "Unable to find distance unit in #{str}" if uom.nil?
38
+
39
+ parsed_uom = Runby::PaceUnits.parse_unit_of_measure uom
40
+ # TODO: test
41
+ raise "#{uom} is not recognized as a distance unit" if parsed_uom[:uom].nil?
42
+
43
+ self.new parsed_uom[:uom], parsed_uom[:factor] * multiplier
44
+ end
45
+
46
+ def to_s
47
+ "#{format('%g', @multiplier.round(2))} #{pluralized_uom}"
48
+ end
49
+
50
+ def pluralized_uom
51
+ uom_description = PaceUnits.description(@uom).downcase
52
+ if @multiplier > 1 then
53
+ uom_description += 's'
54
+ end
55
+ uom_description
56
+ end
57
+
58
+ private
59
+ def init_from_clone(distance)
60
+ @uom = distance.uom
61
+ @multiplier = distance.multiplier
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,14 @@
1
+ module Runby
2
+ # Represents a pace consisting of a distance and a time in which that distance was covered
3
+ class Pace
4
+ attr_reader :time, :distance
5
+ def initialize(time, distance = '1K')
6
+ @time = Runby::RunbyTime.parse(time)
7
+ @distance = Runby::Distance.new(distance)
8
+ end
9
+
10
+ def to_s
11
+ "#{time} per #{distance.pluralized_uom}"
12
+ end
13
+ end
14
+ end
@@ -3,22 +3,62 @@ module Runby
3
3
  # including the human-readable description of each unit
4
4
  # and the factor used to convert it to kilometers.
5
5
  class PaceUnits
6
- def self.description(units)
7
- descriptions[units]
6
+ def self.description(uom)
7
+ @@_distance_uom_definition[uom][:description]
8
+ end
9
+
10
+ def self.parse_unit_of_measure(description)
11
+ description = description.strip.chomp
12
+ found_uom = nil
13
+ found_uom_factor = 1
14
+ @@_distance_uom_definition.each do |uom, details|
15
+ if details[:synonyms].include? description
16
+ found_uom = uom
17
+ break
18
+ end
19
+ end
20
+ if found_uom.nil?
21
+ # Search the special UOMs
22
+ @@_distance_uom_definition_special.each_value do |details|
23
+ if details[:synonyms].include? description
24
+ found_uom = details[:uom]
25
+ found_uom_factor = details[:factor]
26
+ break
27
+ end
28
+ end
29
+ end
30
+ { uom: found_uom, factor: found_uom_factor }
8
31
  end
9
32
 
10
33
  def self.distance_conversion_factor(units)
11
34
  distance_conversion_factors[units]
12
35
  end
13
36
 
37
+ def self.known_uom?(symbol)
38
+ # TODO: test
39
+ @@_distance_uom_definition.has_key?(symbol) || @@_distance_uom_definition_special.has_key?(symbol)
40
+ end
41
+
14
42
  ### -- Private class methods --
15
43
 
16
- private_class_method def self.descriptions
17
- { km: 'Kilometers', mi: 'Miles' }
18
- end
44
+ @@_distance_uom_definition =
45
+ { km: { description: 'Kilometer', synonyms: %w(k km kms kilometer kilometers) },
46
+ m: { description: 'Meter', synonyms: %w(m meter meters) },
47
+ mi: { description: 'Mile', synonyms: %w(mi mile miles) },
48
+ ft: { description: 'Feet', synonyms: %w(ft foot feet) }
49
+ }
50
+
51
+ @@_distance_uom_definition_special =
52
+ {
53
+ marathon: { description: 'Marathon', uom: :mi, factor: 26.2, synonyms: %w(marathon) }
54
+ }
19
55
 
20
- private_class_method def self.distance_conversion_factors
21
- { km: 1.0, mi: 1.612903225806452 }
56
+ private_class_method
57
+ def self.distance_conversion_factors
58
+ { km: 1.0,
59
+ m: 0.001,
60
+ mi: 1.609344,
61
+ ft: 0.0003048 }
22
62
  end
23
63
  end
24
64
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: runby_pace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.79
4
+ version: 0.4.81
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ty Walls
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-30 00:00:00.000000000 Z
11
+ date: 2016-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -73,6 +73,8 @@ files:
73
73
  - bin/guard
74
74
  - bin/setup
75
75
  - lib/runby_pace.rb
76
+ - lib/runby_pace/distance.rb
77
+ - lib/runby_pace/pace.rb
76
78
  - lib/runby_pace/pace_data.rb
77
79
  - lib/runby_pace/pace_range.rb
78
80
  - lib/runby_pace/pace_units.rb
@@ -97,7 +99,7 @@ homepage: https://github.com/tygerbytes/runby-pace
97
99
  licenses:
98
100
  - MIT
99
101
  metadata:
100
- commit-hash: a96d09a79619f8e55898292d6faf25a1970f9193
102
+ commit-hash: ce69f37e02c05d0a2e6bff809c8bb152e4ec13cf
101
103
  post_install_message:
102
104
  rdoc_options: []
103
105
  require_paths: