runby_pace 0.6.113 → 0.6.114

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 863f82385f5fe82d808f93f6eb35019cd214787d
4
- data.tar.gz: 264faa8e329286f77b6082e34ddb41eaa12ca527
3
+ metadata.gz: 466c5700e4c0a990f27205c500ee6838c752d79e
4
+ data.tar.gz: 6238b9f490e7d70b922a00f8d9d3fcc92824fa9d
5
5
  SHA512:
6
- metadata.gz: 5d5a3ec0560b51b42e05a606457c45f47a9d30ab02fca106b83293feddc0fb3d34cfae85b324e5d7ee7961eb3cd53cd414285d9f510676f9d7f20df9bfdadba8
7
- data.tar.gz: 3eca9c2755d6dc94a22f8eace340fe867fca958c01d6a3cdedf57df498667d1b46acb8a088b705d09af8c5ff7e68e4a07d76cc082f67c462a41423e97b918dfe
6
+ metadata.gz: b435b16123b83dc030788020e7ecd295bcd70fcfd52bfdfa4fc4c0a4db3fd511af58d89d03962dbb6e5824982f1af8c9f9b85046fc6d4bd6df237fbca423c926
7
+ data.tar.gz: 190c585deabe76e379dffa602a6d4af6b71b6ebb553e46e4eb4feb2c34d8cf06b3ed8d9b40907690de464eca7a42bae061e86c1dc5e107f82080dfe8c7ede10a
@@ -35,7 +35,8 @@ module Runby
35
35
 
36
36
  def self.parse(str)
37
37
  str = str.strip.chomp.downcase
38
- multiplier = str.scan(/[\d,.]+/).first.to_f
38
+ multiplier = str.scan(/[\d,.]+/).first
39
+ multiplier = multiplier.nil? ? 1 : multiplier.to_f
39
40
  uom = str.scan(/[-_a-z ]+$/).first
40
41
  raise "Unable to find distance unit in '#{str}'" if uom.nil?
41
42
 
@@ -65,8 +66,8 @@ module Runby
65
66
 
66
67
  def <=>(other)
67
68
  raise "Cannot compare Runby::Distance to #{other.class}" unless [Distance, String].include? other.class
68
- if (other.is_a?(String))
69
- return 0 if to_s == other.to_s || to_s(format: :long) == other.to_s(format: :long)
69
+ if other.is_a?(String)
70
+ return 0 if to_s == other || to_s(format: :long) == other
70
71
  return self <=> try_parse(other)[:distance]
71
72
  end
72
73
  kilometers <=> other.kilometers
@@ -6,11 +6,15 @@ module Runby
6
6
  attr_reader :time, :distance
7
7
 
8
8
  def initialize(time_or_pace, distance = '1K')
9
- if time_or_pace.is_a? Pace
10
- init_from_clone time_or_pace
11
- else
12
- @time = Runby::RunbyTime.new(time_or_pace)
13
- @distance = Runby::Distance.new(distance)
9
+ case time_or_pace
10
+ when Pace
11
+ return init_from_clone time_or_pace
12
+ when RunbyTime
13
+ return init_from_time time_or_pace, distance
14
+ when String
15
+ return init_from_string time_or_pace, distance
16
+ else
17
+ raise 'Invalid Time or Pace'
14
18
  end
15
19
  end
16
20
 
@@ -30,26 +34,49 @@ module Runby
30
34
  Runby::Speed.new distance
31
35
  end
32
36
 
37
+ # @param [String] str is either a long-form pace such as "10:00 per mile" or a short-form pace like "10:00 p/mi"
38
+ def self.parse(str)
39
+ str = str.to_s.strip.chomp
40
+ if str.match(/^(?<time>[:\d]*) ?(?: per |p\/)(?<distance>(?:[\d.]+ ?)?\w+)$/)
41
+ time = Runby::RunbyTime.new($~[:time])
42
+ distance = Runby::Distance.new($~[:distance])
43
+ Pace.new time, distance
44
+ else
45
+ raise "Invalid pace format (#{str})"
46
+ end
47
+ end
48
+
49
+ def self.try_parse(str)
50
+ pace, error_message = nil, warning_message = nil
51
+ begin
52
+ pace = Pace.parse str
53
+ rescue StandardError => ex
54
+ error_message = ex.message
55
+ end
56
+ { pace: pace, error: error_message, warning: warning_message }
57
+ end
58
+
33
59
  def <=>(other)
34
60
  if other.is_a? Pace
35
- return nil unless @distance == other.distance
61
+ raise 'Comparing paces of different distances is not currently supported' unless @distance == other.distance
36
62
  @time <=> other.time
37
63
  elsif other.is_a? RunbyTime
38
64
  @time <=> other.time
39
65
  elsif other.is_a? String
40
- # TODO: Parse as Pace when Pace.parse is available
41
- @time <=> RunbyTime.parse(other)
66
+ return 0 if to_s == other || to_s(format: :long) == other
67
+ return 0 if @time == other
68
+ self <=> try_parse(other)[:pace]
42
69
  end
43
70
  end
44
71
 
45
72
  def almost_equals?(other_pace, tolerance_time = '00:01')
46
73
  return @time.almost_equals?(other_pace, tolerance_time) if other_pace.is_a?(RunbyTime)
47
74
  if other_pace.is_a?(String)
48
- # TODO: Get parsing working
75
+ return @time.almost_equals?(other_pace, tolerance_time) if other_pace =~ /^\d\d:\d\d$/
49
76
  other_pace = Pace.parse(other_pace)
50
77
  end
51
78
  tolerance = RunbyTime.new(tolerance_time)
52
- self >= (other_pace - tolerance) && self <= (other_pace + tolerance)
79
+ (self - tolerance) <= other_pace && (self + tolerance) >= other_pace
53
80
  end
54
81
 
55
82
  # @param [Pace, RunbyTime] other
@@ -86,5 +113,21 @@ module Runby
86
113
  @time = other_pace.time
87
114
  @distance = other_pace.distance
88
115
  end
116
+
117
+ def init_from_string(string, distance = '1K')
118
+ pace = Pace.try_parse(string)
119
+ if pace[:pace]
120
+ @time = pace[:pace].time
121
+ @distance = pace[:pace].distance
122
+ return
123
+ end
124
+ @time = Runby::RunbyTime.new string
125
+ @distance = Runby::Distance.new distance
126
+ end
127
+
128
+ def init_from_time(time, distance)
129
+ @time = time
130
+ @distance = Runby::Distance.new distance
131
+ end
89
132
  end
90
133
  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.6.113
4
+ version: 0.6.114
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-11-22 00:00:00.000000000 Z
11
+ date: 2016-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -101,7 +101,7 @@ homepage: https://github.com/tygerbytes/runby-pace
101
101
  licenses:
102
102
  - MIT
103
103
  metadata:
104
- commit-hash: 989e6cd501d99bece9b7fc8a4e44e569b4d89d3b
104
+ commit-hash: d6e06c6e669965e0c1225bab6f06d671dca85bb4
105
105
  post_install_message:
106
106
  rdoc_options: []
107
107
  require_paths: