class Array

Array helpers

Array helpers

Chronify array helpers

Public Instance Methods

good?() click to toggle source

Tests if object is nil or empty

@return [Boolean] true if object is defined and has content

# File lib/doing/good.rb, line 36
def good?
  !nil? && !empty?
end
highlight_tags(color = 'cyan') click to toggle source

Hightlight @tags in string for console output

@param color [String] the color to highlight with

@return [Array] Array of highlighted @tags

# File lib/doing/array/tags.rb, line 36
def highlight_tags(color = 'cyan')
  to_tags.map { |t| Doing::Color.send(color.to_sym, t) }
end
log_tags(color = 'cyan') click to toggle source

Tag array for logging

@return [String] Highlighted tag array joined with comma

# File lib/doing/array/tags.rb, line 45
def log_tags(color = 'cyan')
  highlight_tags(color).join(', ')
end
nested_hash(value = nil) click to toggle source

Convert array to nested hash, setting last key to value

@param value The value to set

# File lib/doing/array/nested_hash.rb, line 11
def nested_hash(value = nil)
  hsh = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }
  hsh.dig(*self[0..-2])[fetch(-1)] = value
  hsh
end
tags_to_array() click to toggle source

Convert an array of @tags to plain strings

@return [Array] array of strings

# File lib/doing/array/tags.rb, line 13
def tags_to_array
  map(&:remove_at).map(&:strip)
end
time_string(format: :dhm) click to toggle source

Format [d, h, m] as string

@accept [Array] Array of [days, hours, minutes]

@param format [Symbol] The format, :dhm, :hm, :m, :clock, :natural @return [String] formatted string

# File lib/doing/chronify/array.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
    if d.zero? && h > 24
      d = (h / 24).floor
      h = h % 24
    end
    format('%<d>02d:%<h>02d:%<m>02d', d: d, h: h, m: m)
  when :dhm
    if d.zero? && h > 24
      d = (h / 24).floor
      h = h % 24
    end
    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() click to toggle source

Convert array of strings to array of @tags

@return [Array] Array of @tags

@example ['one', '@two', 'three'].to_tags # => ['@one', '@two', '@three']

# File lib/doing/array/tags.rb, line 24
def to_tags
  map(&:add_at)
end
utf8() click to toggle source

Force UTF-8 encoding of strings in array

@return [Array] Encoded lines

# File lib/doing/array/array.rb, line 12
def utf8
  c = self.class
  if String.method_defined? :force_encoding
    replace c.new(map(&:utf8))
  else
    self
  end
end