runby_pace 0.6.150 → 0.6.151

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
  SHA1:
3
- metadata.gz: 49cc12cba10d44c5bf4502da8d57bbad5cbbde8a
4
- data.tar.gz: 841d4456163ddcc77ba860ca6060cf773f1aef1c
3
+ metadata.gz: 71c401e8a77a8960c6c7114917cabf50a2c0d8ad
4
+ data.tar.gz: 423bbf069cd9fb50c76f23dfe02302750f9bf209
5
5
  SHA512:
6
- metadata.gz: f528ae26fc7c9a2a8b983955fc82e0fcee1ec898412f1c0393269ec10ff55eb3ac1b10747bba001da95a5bf9e5c50049cdac6ff3f2d0bf9bc6b3428dd14eb9bd
7
- data.tar.gz: 09a2177c4ccdd96dad0e8870d11b0a273a79f53ca1191a94ca063168413e29d7026607f2c0bf9cee8dd8f23efd3960dd5bde4360bf65fe2d2daec5de95ac6e08
6
+ metadata.gz: 5762c55f858d8c425b730501868f376c26b71369a58ddfb944593a4613b2d707dc7867d35a35788bcea58cf528567010c68f21758781a98b2c2546e2c1137988
7
+ data.tar.gz: 8f0daa76640f6cb19e16e1476acae567e918348f27a7e0f400f0c7f1d86187db4018ae778fbf9e0264d7abacc95b2fa4affc74443fd0ed0134f3f9713f18fa60
@@ -7,14 +7,14 @@ module Runby
7
7
 
8
8
  attr_reader :time_s, :hours_part, :minutes_part, :seconds_part
9
9
 
10
+ def self.new(time)
11
+ return time if time.is_a? RunbyTime
12
+ return RunbyTime.parse time if time.is_a?(String) || time.is_a?(Symbol)
13
+ super
14
+ end
15
+
10
16
  def initialize(time)
11
- if time.is_a?(String) || time.is_a?(Symbol)
12
- init_from_string time
13
- elsif time.is_a?(RunbyTime)
14
- init_from_clone time
15
- elsif time.is_a?(Hash)
16
- init_from_hash time
17
- end
17
+ init_from_parts time if time.is_a? RunbyTimeParser::TimeParts
18
18
  freeze
19
19
  end
20
20
 
@@ -62,7 +62,7 @@ module Runby
62
62
  end
63
63
 
64
64
  def to_s
65
- @time_s.sub(/^0/, '')
65
+ @time_s
66
66
  end
67
67
 
68
68
  def total_hours
@@ -126,25 +126,12 @@ module Runby
126
126
 
127
127
  private
128
128
 
129
- # @param [Hash] params
130
- def init_from_hash(params = {})
131
- @time_s = params.fetch :time_s, '00:00'
132
- @hours_part = params.fetch :hours_part, 0.0
133
- @minutes_part = params.fetch :minutes_part, 0.0
134
- @seconds_part = params.fetch :seconds_part, 0.0
135
- end
136
-
137
- # @param [RunbyTime] time
138
- def init_from_clone(time)
139
- @time_s = time.time_s
140
- @hours_part = time.hours_part
141
- @minutes_part = time.minutes_part
142
- @seconds_part = time.seconds_part
143
- end
144
-
145
- # @param [String] time
146
- def init_from_string(time)
147
- init_from_clone RunbyTime.parse time
129
+ # @param [RunbyTimeParser::TimeParts] parts
130
+ def init_from_parts(parts)
131
+ @time_s = parts.format
132
+ @hours_part = parts[:hours]
133
+ @minutes_part = parts[:minutes]
134
+ @seconds_part = parts[:seconds]
148
135
  end
149
136
  end
150
137
  end
@@ -5,41 +5,76 @@ module Runby
5
5
  class RunbyTimeParser
6
6
  def self.parse(str)
7
7
  time = str.to_s.strip.chomp
8
-
9
- if time.match?(/^\d?\d:\d\d:\d\d$/)
10
- parts = time.split(':')
11
- hours_part = parts[0].to_i
12
- minutes_part = parts[1].to_i
13
- seconds_part = parts[2].to_i
14
- elsif time.match?(/^\d?\d:\d\d$/)
15
- parts = time.split(':')
16
- hours_part = 0
17
- minutes_part = parts[0].to_i
18
- seconds_part = parts[1].to_i
19
- elsif time.match?(/^\d+$/)
20
- hours_part = 0
21
- minutes_part = time.to_i
22
- seconds_part = 0
23
- elsif time.match?(/^\d+[,. ]\d+$/)
24
- parts = time.split(/[,. ]/)
25
- hours_part = 0
26
- minutes_part = parts[0].to_i
27
- seconds_part = (parts[1].to_i / 10.0 * 60).to_i
8
+ if time_string?(time)
9
+ parts = TimeParts.new(time.split(':').reverse)
10
+ elsif integer?(time)
11
+ parts = extract_minutes_from_integer time
12
+ elsif decimal?(time)
13
+ parts = extract_minutes_from_decimal time
28
14
  else
29
15
  raise 'Invalid time format'
30
16
  end
17
+ RunbyTime.new(parts)
18
+ end
31
19
 
32
- raise 'Hours must be less than 24' if hours_part > 23
33
- raise 'Minutes must be less than 60 if hours are supplied' if hours_part.positive? && minutes_part > 59
34
- raise 'Minutes must be less than 99 if no hours are supplied' if hours_part.zero? && minutes_part > 99
35
- raise 'Seconds must be less than 60' if seconds_part > 59
36
- hours_part_formatted = ''
37
- if hours_part.positive?
38
- hours_part_formatted = "#{hours_part.to_s.rjust(2, '0')}:"
20
+ def self.decimal?(str)
21
+ str.match?(/^\d+[,. ]\d+$/)
22
+ end
23
+
24
+ def self.integer?(str)
25
+ str.match?(/^\d+$/)
26
+ end
27
+
28
+ def self.time_string?(str)
29
+ str.match?(/^\d?\d(:\d\d)+$/)
30
+ end
31
+
32
+ def self.extract_minutes_from_decimal(decimal_str)
33
+ decimal_parts = decimal_str.split(/[,. ]/)
34
+ minutes = decimal_parts[0].to_i
35
+ seconds = (decimal_parts[1].to_i / 10.0 * 60).to_i
36
+ TimeParts.new([seconds, minutes])
37
+ end
38
+
39
+ def self.extract_minutes_from_integer(integer_str)
40
+ TimeParts.new([0, integer_str.to_i])
41
+ end
42
+
43
+ # Encapsulates the parts of a time string
44
+ class TimeParts
45
+ def initialize(parts_array)
46
+ @keys = { seconds: 0, minutes: 1, hours: 2 }
47
+ @parts = Array.new(@keys.count, 0)
48
+ Range.new(0, parts_array.count - 1).each { |i| @parts[i] = parts_array[i] }
49
+ validate
50
+ freeze
51
+ end
52
+
53
+ def [](key)
54
+ i = @keys[key]
55
+ @parts[i].to_i
56
+ end
57
+
58
+ def format
59
+ time_f = +''
60
+ @parts.reverse_each do |part|
61
+ time_f << ':' << part.to_s.rjust(2, '0')
62
+ end
63
+ # Remove leading ':'
64
+ time_f.slice!(0)
65
+ # Remove leading '00:00...'
66
+ time_f.sub!(/^(?:00:)+(\d\d:\d\d)/, '\1') if time_f.length > 5
67
+ # If the time looks like 00:48, only show 0:48
68
+ time_f.slice!(0) if time_f.slice(0) == '0'
69
+ time_f
39
70
  end
40
- time_formatted = "#{hours_part_formatted}#{minutes_part.to_s.rjust(2, '0')}:#{seconds_part.to_s.rjust(2, '0')}"
41
71
 
42
- RunbyTime.new(time_s: time_formatted, hours_part: hours_part, minutes_part: minutes_part, seconds_part: seconds_part)
72
+ def validate
73
+ raise 'Hours must be less than 24' if self[:hours] > 23
74
+ raise 'Minutes must be less than 60 if hours are supplied' if self[:hours].positive? && self[:minutes] > 59
75
+ raise 'Minutes must be less than 99 if no hours are supplied' if self[:hours].zero? && self[:minutes] > 99
76
+ raise 'Seconds must be less than 60' if self[:seconds] > 59
77
+ end
43
78
  end
44
79
  end
45
- end
80
+ 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.150
4
+ version: 0.6.151
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ty Walls
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-07-13 00:00:00.000000000 Z
11
+ date: 2017-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -106,7 +106,7 @@ homepage: https://github.com/tygerbytes/runby-pace
106
106
  licenses:
107
107
  - MIT
108
108
  metadata:
109
- commit-hash: 54de432a4f35967353f0d104008c5402f357c237
109
+ commit-hash: bb461b8b253e567bb6123f62a963be7a875b3285
110
110
  post_install_message:
111
111
  rdoc_options: []
112
112
  require_paths: