hour-ruby 0.0.2 → 0.0.3

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -1
  3. data/lib/hour.rb +38 -19
  4. data/spec/hour_spec.rb +21 -3
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0a1be05c48e54df485856e200079e23f03e93ccdc785b9c5f20253cab6ef1918
4
- data.tar.gz: 949992e1b0c54a94a17f964f4ad7114791ec95b83bd4ff3a8c6f15026be4ebb2
3
+ metadata.gz: 5b8bac3638cd0e4086e0929f7fc05aae9d6e165495dc9427a2a02a587f1b1f20
4
+ data.tar.gz: 2afec0e7ad09044471bf724c2d09070a3d251c7eb676538484f4f62ec3d29b8d
5
5
  SHA512:
6
- metadata.gz: 99b789a3f010f5c95acff8bfae7d8a71a88a68d18261287d53b8b01859cdc84c0ee99dd7229c4a4a9e2d2329e3a761bf0eb17074081497bf731231238b79d668
7
- data.tar.gz: 8bcba1a83adb64a842d9f4c51b802c003502239d93542b735508e7ef86670dd08db5de3e701c1653832d73915e5545a4cbc9f6c47cf3b455f74187bd0dbdf868
6
+ metadata.gz: 61261cd9e0c2e96aaa73b97f3429ed99c2a3a6880f6e9e7a32efba3fb22b8f517c4b3b3f3e6f2c5e05a52061063e213872366c2cb9ded37329d9c83cd1383f86
7
+ data.tar.gz: e242de3802e2e9b424968617b49900d280e0e95d18b002cf48c87551c42e1931ef70522753693afe94783ac0fae0991bc773e9bc8778cc838862b944c5d55fed
data/README.md CHANGED
@@ -28,10 +28,13 @@ puts "It's #{hour.to_s}!"
28
28
  puts "The system time is #{Hour.now}!"
29
29
  ```
30
30
 
31
+ # Development
32
+
33
+ Use `vagrant up` to start a VM. Once everything's ready, it's `vagrant ssh`. Upon login, you'll be connected to a tmux session. To sleep, it's `vagrant suspend`.
34
+
31
35
  # TODO
32
36
 
33
37
  - Rubocop.
34
- - Travis.
35
38
  - Release version 0.1.
36
39
 
37
40
  [Gem version]: https://rubygems.org/gems/hour-ruby
data/lib/hour.rb CHANGED
@@ -56,13 +56,13 @@ class Hour
56
56
  end
57
57
 
58
58
  # TODO: Test me and document me.
59
- def self.now
60
- self.from_time(Time.now)
59
+ def self.now(**opts)
60
+ self.from_time(Time.now, **opts)
61
61
  end
62
62
 
63
63
  # TODO: document and write tests.
64
- def self.from_time(time)
65
- self.new(time.hour, time.min, time.sec)
64
+ def self.from_time(time, s: true)
65
+ self.new(time.hour, time.min, s ? time.sec : false)
66
66
  end
67
67
 
68
68
  # Build an hour instance from an hour string.
@@ -71,17 +71,17 @@ class Hour
71
71
  # Hour.parse("1:00", "%h:%m?") # Will work with "1:00" or just "1".
72
72
  #
73
73
  # TODO: Implement me, test me and document me.
74
- def self.parse(serialised_hour, formatting_string = nil)
74
+ def self.parse(serialised_hour, s: true)
75
75
  args = serialised_hour.split(':').map(&:to_i)
76
76
 
77
- case args.length
78
- when 3
77
+ if args.length == 3 && s
79
78
  self.new(*args)
80
- when (0..2)
81
- # TODO: if formatting_string ...
82
- raise ArgumentError.new("If format is not H:M:S, formatting string must be provided.")
83
- when (4..Float::INFINITY) # Until we have infinite ranges like (4..) in Ruby 2.6.
84
- raise ArgumentError, "Too many arguments."
79
+ elsif args.length == 2 && !s
80
+ self.new(*(args << false))
81
+ elsif ((0..2).include?(args.length) && s) || ((0..1).include?(args.length) && !s)
82
+ raise ArgumentError, "Too few segments (#{args.inspect})."
83
+ elsif ((4..Float::INFINITY).include?(args.length) && s) || ((3..Float::INFINITY).include?(args.length) && !s)
84
+ raise ArgumentError, "Too many segments (#{args.inspect})."
85
85
  end
86
86
  end
87
87
 
@@ -95,7 +95,7 @@ class Hour
95
95
  raise ArgumentError.new("Use either minutes OR seconds, not both.")
96
96
  end
97
97
 
98
- if minutes > 0
98
+ if minutes > 0 || (minutes == 0 && seconds == 0)
99
99
  self.new(h: minutes / 60, m: minutes % 60)
100
100
  else
101
101
  self.from(minutes: seconds / 60) + self.new(s: seconds % 60)
@@ -121,7 +121,7 @@ class Hour
121
121
  raise ArgumentError.new("Minutes must be a number between 0 and 60.")
122
122
  end
123
123
 
124
- if @s > 60
124
+ if @s.respond_to?(:round) && @s > 60
125
125
  raise ArgumentError.new("Seconds must be a number between 0 and 60.")
126
126
  end
127
127
  end
@@ -132,7 +132,15 @@ class Hour
132
132
  def +(other)
133
133
  hours = @h + other.h + (@m + other.m + ((@s + other.s) / 60)) / 60
134
134
  minutes = (@m + other.m + ((@s + other.s) / 60)) % 60
135
- seconds = (@s + other.s) % 60
135
+
136
+ if @s && other.s
137
+ seconds = (@s + other.s) % 60
138
+ elsif (!@s) && (!other.s)
139
+ seconds = false
140
+ else
141
+ raise "TODO: how to resolve this?"
142
+ end
143
+
136
144
  self.class.new(hours, minutes, seconds)
137
145
  end
138
146
 
@@ -159,17 +167,28 @@ class Hour
159
167
  # Hour.new(m: 1, s: 10).seconds.value # => 10
160
168
  # Hour.new(1, 45, 10 ).seconds.total # => (1 * 60 * 60) + (45 * 60) + 10
161
169
  def seconds
162
- SecondUnit.new(self)
170
+ SecondUnit.new(self) if @s
163
171
  end
164
172
 
165
- # TODO: Add formatting string support.
166
- # TODO: Pad 0s. I. e. "#{self.hours}:#{format('%02d', self.minutes_over_the_hour)}"
173
+ # Returns string representation of the hour instance.
174
+ #
175
+ # Hour.new(m: 1, s: 10 ).to_s # => "1:10"
176
+ # Hour.new(1, 45, false).to_s # => "1:45"
177
+ #
178
+ # TODO: Allow formatting string (to format hours to 2 digits etc).
167
179
  def to_s(format = nil)
168
- "#{@h}:#{@m}:#{@s}"
180
+ [(@h unless @h.zero?), format('%02d', @m), (format('%02d', @s) if @s)].compact.join(':')
169
181
  end
170
182
 
171
183
  alias_method :inspect, :to_s
172
184
 
185
+ # Provisional.
186
+ def to_decimal
187
+ decimal = (@m / 60.0) + (@s / 3600.0)
188
+ "#{@h}.#{decimal}"
189
+ @h + decimal
190
+ end
191
+
173
192
  def to_time(today = Time.now)
174
193
  Time.new(today.year, today.month, today.day, self.hours, self.minutes_over_the_hour)
175
194
  end
data/spec/hour_spec.rb CHANGED
@@ -18,9 +18,27 @@ describe Hour do
18
18
  end
19
19
 
20
20
  describe "#to_s" do
21
- pending "implement me" #do
22
- # TODO
23
- #end
21
+ it "pads minutes and seconds with zeros" do
22
+ hour = Hour.new(1, 9, 5)
23
+ expect(hour.to_s).to eql('1:09:05')
24
+ end
25
+
26
+ it "omits hours if it's less than 60 minutes" do
27
+ hour = Hour.new(m: 52)
28
+ expect(hour.to_s).to eql('52:00')
29
+ end
30
+
31
+ context "with seconds disabled" do
32
+ it "returns %HH:%MM" do
33
+ hour = Hour.new(1, 9, false)
34
+ expect(hour.to_s).to eql('1:09')
35
+ end
36
+ end
37
+
38
+ context "with hours disabled" do
39
+ # This would be 90:00 for 1.5 hours.
40
+ pending "not implemented"
41
+ end
24
42
  end
25
43
 
26
44
  describe "#+" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hour-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - James C Russell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-21 00:00:00.000000000 Z
11
+ date: 2018-10-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: "."
14
14
  email: james@101ideas.cz