Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/doing/array.rb,
lib/doing/array_chronify.rb
Overview
Chronify array helpers
Direct Known Subclasses
Instance Method Summary collapse
-
#highlight_tags(color = 'cyan') ⇒ String
Hightlight @tags in string for console output.
-
#log_tags ⇒ String
Tag array for logging.
-
#nested_hash(value) ⇒ Object
Convert array to nested hash, setting last key to value.
-
#tags_to_array ⇒ Array
Convert an @tags to plain strings.
-
#time_string(format: :dhm) ⇒ String
Format [d, h, m] as string.
-
#to_tags ⇒ Array
Convert strings to @tags.
- #to_tags! ⇒ Object
Instance Method Details
#highlight_tags(color = 'cyan') ⇒ String
Hightlight @tags in string for console output
40 41 42 43 |
# File 'lib/doing/array.rb', line 40 def (color = 'cyan') tag_color = Doing::Color.send(color) .map { |t| "#{tag_color}#{t}" } end |
#log_tags ⇒ String
Tag array for logging
50 51 52 |
# File 'lib/doing/array.rb', line 50 def .join(', ') end |
#nested_hash(value) ⇒ Object
Convert array to nested hash, setting last key to value
59 60 61 62 63 64 65 |
# File 'lib/doing/array.rb', line 59 def nested_hash(value) raise StandardError, 'Value can not be nil' if value.nil? hsh = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) } hsh.dig(*self[0..-2])[self.fetch(-1)] = value hsh end |
#tags_to_array ⇒ Array
Convert an @tags to plain strings
13 14 15 |
# File 'lib/doing/array.rb', line 13 def map(&:remove_at) end |
#time_string(format: :dhm) ⇒ String
Format [d, h, m] as string
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/doing/array_chronify.rb', line 15 def time_string(format: :dhm) raise InvalidArgument, 'Invalid array, must be [d,h,m]' unless count == 3 d, h, m = self case format when :clock format('%<d>02d:%<h>02d:%<m>02d', d: d, h: h, m: m) when :dhm output = [] output.push(format('%<d>dd', d: d)) if d.positive? output.push(format('%<h>dh', h: h)) if h.positive? output.push(format('%<m>dm', m: m)) if m.positive? output.join(' ') when :hm h += d * 24 if d.positive? format('%<h> 4dh %<m>02dm', h: h, m: m) when :m h += d * 24 if d.positive? m += h * 60 if h.positive? format('%<m> 4dm', m: m) when :natural human = [] human.push(format('%<d>d %<s>s', d: d, s: 'day'.to_p(d))) if d.positive? human.push(format('%<h>d %<s>s', h: h, s: 'hour'.to_p(h))) if h.positive? human.push(format('%<m>d %<s>s', m: m, s: 'minute'.to_p(m))) if m.positive? human.join(', ') when :speech human = [] human.push(format('%<d>d %<s>s', d: d, s: 'day'.to_p(d))) if d.positive? human.push(format('%<h>d %<s>s', h: h, s: 'hour'.to_p(h))) if h.positive? human.push(format('%<m>d %<s>s', m: m, s: 'minute'.to_p(m))) if m.positive? last = human.pop case human.count when 2 human.join(', ') + ", and #{last}" when 1 "#{human[0]} and #{last}" when 0 last end end end |
#to_tags ⇒ Array
Convert strings to @tags
24 25 26 |
# File 'lib/doing/array.rb', line 24 def map(&:add_at) end |
#to_tags! ⇒ Object
28 29 30 |
# File 'lib/doing/array.rb', line 28 def replace end |