Class: HumanDuration::Duration
- Inherits:
-
Object
- Object
- HumanDuration::Duration
- Defined in:
- lib/human_duration.rb
Overview
docs to do
Class Method Summary collapse
- .add_time(value, name, plural = true) ⇒ Object
- .add_time_long ⇒ Object
- .add_time_short ⇒ Object
- .comma_or_not(count) ⇒ Object
- .count_items ⇒ Object
- .display_type(type = 'compact') ⇒ Object
- .generate_output ⇒ Object
- .human_duration(seconds) ⇒ Object
- .pluralize(number) ⇒ Object
- .reset ⇒ Object
- .split_seconds(seconds) ⇒ Object
Class Method Details
.add_time(value, name, plural = true) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# 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 ⇒ Object
89 90 91 92 93 |
# 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 ⇒ Object
95 96 97 98 99 |
# 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) ⇒ Object
52 53 54 55 56 57 58 |
# 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 ⇒ Object
83 84 85 86 87 |
# 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') ⇒ Object
21 22 23 24 25 |
# 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 ⇒ Object
101 102 103 104 105 106 107 |
# 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) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# 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) ⇒ Object
46 47 48 49 50 |
# File 'lib/human_duration.rb', line 46 def self.pluralize(number) return 's' unless number == 1 '' end |
.reset ⇒ Object
40 41 42 43 44 |
# File 'lib/human_duration.rb', line 40 def self.reset $time_values = {} $item_count = 0 $output_buffer = '' end |
.split_seconds(seconds) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# 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 |