hour 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/hour.rb +26 -17
  3. data/lib/hour/version.rb +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cc30ebd848bc96facff31dfe72a600ee22fc2d63
4
- data.tar.gz: f66923f200c50f2f1b139da32cd0afaee15de05f
3
+ metadata.gz: 91b0152711820c46e57ddb8107ab885c7921f19e
4
+ data.tar.gz: ae4d3d2efaf9a738e01df44f0695e1dddb88cef7
5
5
  SHA512:
6
- metadata.gz: 2132974435122a4106b9973429a03f50ee9dac45275e3dfbc3944394a518863aff5f3c8d3ab18ceb67a007438afec20382e0f5722197e201be07eb8c42450f8e
7
- data.tar.gz: 3ba0c37f51861c0782d6809cf2ee3f1b85d5202153240f23897500e59597b3e8d0365a51f0533aead1f7451dcc9f0f5ab417f05f091b350741d6e5fa407575d1
6
+ metadata.gz: 47c38b819df5ee82bd2bb2e4c76ac612fc908df7b4797cc82433c5f694dbd941771e101f28b8012816edddc008b000aacf165aaf5cb3016a991b8139a202c282
7
+ data.tar.gz: 02d1bd54e9590e794ddd16d751b541ee8740e31dda7d0f7f863ad55d5e1e67f141f6269530112abb3c0c71c05e1443313cae273cfd8044aeff143ce8aa9c2a78
@@ -1,35 +1,44 @@
1
- require "hour/version"
1
+ require "hour/version"
2
2
 
3
3
  module Hour
4
4
  class Hour
5
- attr_reader :hours, :minutes, :to_seconds, :to_days, :to_hours_less_days, :to_s, :to_formatted_s, :to_a, :to_time
5
+ attr_reader :hours, :minutes, :to_seconds, :days, :hours_less_days, :to_s, :to_formatted_s, :to_a, :to_time
6
6
 
7
7
  def initialize(hours, minutes = 0)
8
- hours, minutes = hours.split(':').map(&:to_i) if hours.is_a? String
9
- minutes = [0, minutes, 59].sort[1] #clamp: constrains the assignment to the middle value
10
- @to_s = "#{'%02d' % hours}:#{'%02d' % minutes}"
11
- @to_a = [@hours = hours, @minutes = minutes]
12
- @to_seconds = ((hours * 60) + minutes) * 60
13
- @to_days = hours / 24
14
- @to_hours_less_days = hours - 24 * @to_days
15
- @to_formatted_s = "#{@to_days} day#{'s' unless @to_days == 1}, " +
16
- "#{@to_hours_less_days} hour#{'s' unless @to_hours_less_days == 1}, " +
17
- "#{minutes} minute#{'s' unless minutes == 1}"
8
+ hours, minutes = hours.split(':') if hours.is_a? String
9
+ @minutes = [0, minutes.to_i, 59].sort[1] # Constrains assignment to the middle value, w/o the #Clamp method
10
+ @to_a = [@hours = hours.to_i, @minutes]
11
+ @to_s = "#{'%02d' % @hours}:#{'%02d' % @minutes}" # Formats single-digits such that '1' will be '01'
12
+ @to_seconds = (@hours * 60 + @minutes) * 60
13
+ @days = @hours / 24 || 0
14
+ @hours_less_days = @hours - 24 * @days
15
+ @to_formatted_s = "#{@days} day#{'s' unless @days == 1}, " +
16
+ "#{@hours_less_days} hour#{'s' unless @hours_less_days == 1}, " +
17
+ "#{@minutes} minute#{'s' unless @minutes == 1}"
18
18
  @to_time = Time.new(
19
19
  0, #year
20
20
  1, #month
21
- 1 + @to_days, #day
22
- @to_hours_less_days, #hour
23
- minutes, #minute
21
+ 1 + @days, #day
22
+ @hours_less_days, #hour
23
+ @minutes, #minute
24
24
  0, #second
25
- 0 #UTC #TODO: gracefully handle both the new Time.zone format and the deprecated UTC
25
+ 0 #UTC #TODO: handle both Time.zone format & deprecated UTC default?
26
26
  )
27
27
  end
28
28
 
29
+ def on_this_day(day_elect = Time.now)
30
+ day_elect.change(day: day_elect.day + @days,
31
+ hour: @hours_less_days,
32
+ min: @minutes)
33
+ end
34
+
29
35
  def self.from_time(time_obj)
30
- hour = time_obj.hour + (time_obj.day - 1) * 24 if time_obj.year == 0
36
+ hour = time_obj.hour + (time_obj.day - 1) * 24 if time_obj.year == 0 # Note that we expect Time input to be year 0, month 1 (minimum values). Incrementing day to a value above 1 means "add 24 * x hours", where x is the number of days above 1, so that a day value of 1 and hour value of 4 will be interpreted as "4 hours", whereas a day value of 2 and an hour value of 4 will be interpreted as "28 hours".
31
37
  self.new(hour || time_obj.hour, time_obj.min)
32
38
  end
33
39
 
40
+ alias_method :to_days, :days # DEPRECATION: to_days
41
+ alias_method :to_hours_less_days, :hours_less_days # DEPRECATION: to_hours_less_days
42
+
34
43
  end
35
44
  end
@@ -1,3 +1,3 @@
1
1
  module Hour
2
- VERSION = "0.1.6"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hour
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - davegregg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-16 00:00:00.000000000 Z
11
+ date: 2017-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler