class HumanDuration::Duration
docs to do
Public Class Methods
add_time(value, name, plural = true)
click to toggle source
# File lib/human_duration.rb, line 60 def self.add_time(value, name, plural = true) return if value < 1 && $type != 'full' $output_buffer += if plural format('%<value>s %<name>s%<plural>s', value: value, name: name, plural: pluralize(value)) else format('%<value>s %<name>s', value: value, name: name) end $item_count -= 1 $output_buffer += comma_or_not($item_count) end
add_time_long()
click to toggle source
# File lib/human_duration.rb, line 89 def self.add_time_long $time_values.reverse_each do |name, value| add_time(value, name) end end
add_time_short()
click to toggle source
# File lib/human_duration.rb, line 95 def self.add_time_short $time_values.reverse_each do |name, value| add_time(value, SHORT_NAME_MAP[name], false) end end
comma_or_not(count)
click to toggle source
# File lib/human_duration.rb, line 52 def self.comma_or_not(count) return ', ' if count > 1 return ' & ' if count == 1 && $type == 'short' return ' and ' if count == 1 '' end
count_items()
click to toggle source
# File lib/human_duration.rb, line 83 def self.count_items $time_values.each do |_name, value| $item_count += 1 if value.positive? || $type == 'full' end end
display_type(type = 'compact')
click to toggle source
# File lib/human_duration.rb, line 21 def self.display_type(type = 'compact') raise ArgumentError.new('Invalid type - Valid options are:- compact, short and full') unless VALID_TYPES.include? type $type = type end
generate_output()
click to toggle source
# File lib/human_duration.rb, line 101 def self.generate_output if $type == 'short' add_time_short else add_time_long end end
human_duration(seconds)
click to toggle source
# File lib/human_duration.rb, line 27 def self.human_duration(seconds) reset return 'negative' if seconds.negative? return 'now' if seconds.zero? split_seconds(seconds) count_items generate_output $output_buffer end
pluralize(number)
click to toggle source
# File lib/human_duration.rb, line 46 def self.pluralize(number) return 's' unless number == 1 '' end
reset()
click to toggle source
# File lib/human_duration.rb, line 40 def self.reset $time_values = {} $item_count = 0 $output_buffer = '' end
split_seconds(seconds)
click to toggle source
# File lib/human_duration.rb, line 72 def self.split_seconds(seconds) SPLIT_TYPES.map do |count, name| if seconds.positive? seconds, n = seconds.divmod(count) $time_values[name] = n else $time_values[name] = 0 end end end