lib/formatted_duration.rb in duration-formatter-1.1.1 vs lib/formatted_duration.rb in duration-formatter-1.2.0
- old
+ new
@@ -1,12 +1,12 @@
require 'ruby-duration'
class FormattedDuration
- CONSTRAINTS = [:minutes, :hours, :days, :weeks]
- FORMATS = { weeks: '%w %~w', days: '%tdu', hours: '%thu', minutes: '%tmu' }
- BARE_FORMATS = { days: '%d %~d', hours: '%h %~h', minutes: '%m %~m' }
- BARE = { days: 7, hours: 24, minutes: 60 }
+ CONSTRAINTS = [:minutes, :hours, :days, :weeks].freeze
+ FORMATS = { weeks: '%w %~w', days: '%tdu', hours: '%thu', minutes: '%tmu' }.freeze
+ BARE_FORMATS = { days: '%d %~d', hours: '%h %~h', minutes: '%m %~m' }.freeze
+ BARE = { days: 7, hours: 24, minutes: 60 }.freeze
def initialize(minutes, constraint = :weeks)
@constraint = CONSTRAINTS.index(constraint)
@minutes = minutes
@hours = minutes / 60
@@ -14,11 +14,11 @@
@weeks = @days / 7
@output = []
end
def format
- append(:weeks, @weeks, '%w %~w') if @weeks > 0 && @constraint > 2
+ append(:weeks, @weeks, '%w %~w') if @weeks.positive? && @constraint > 2
add(:days, @days, 2)
add(:hours, @hours, 1)
add(:minutes, @minutes, 0)
@output.join(', ')
@@ -32,12 +32,12 @@
end
def add(unit, value, constraint)
bare = value % BARE[unit]
- if value > 0 && @constraint == constraint
+ if value.positive? && @constraint == constraint
append(unit, value, FORMATS[unit])
- elsif bare > 0 && @constraint > constraint - 1
+ elsif bare.positive? && @constraint > constraint - 1
append(unit, bare, BARE_FORMATS[unit])
end
end
end