Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/doing/array.rb,
lib/doing/array_chronify.rb

Overview

Chronify array helpers

Direct Known Subclasses

Doing::Items, Doing::Note

Instance Method Summary collapse

Instance Method Details

#highlight_tags(color = 'cyan') ⇒ String

Hightlight @tags in string for console output

Parameters:

  • color (String) (defaults to: 'cyan')

    the color to highlight with

Returns:

  • (String)

    string with @tags highlighted



40
41
42
43
# File 'lib/doing/array.rb', line 40

def highlight_tags(color = 'cyan')
  tag_color = Doing::Color.send(color)
  to_tags.map { |t| "#{tag_color}#{t}" }
end

#log_tagsString

Tag array for logging

Returns:

  • (String)

    Highlighted tag array joined with comma



50
51
52
# File 'lib/doing/array.rb', line 50

def log_tags
  highlight_tags.join(', ')
end

#nested_hash(value) ⇒ Object

Convert array to nested hash, setting last key to value

Parameters:

  • value

    The value to set

Raises:

  • (StandardError)


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_arrayArray

Convert an @tags to plain strings

Returns:

  • (Array)

    array of strings



13
14
15
# File 'lib/doing/array.rb', line 13

def tags_to_array
  map(&:remove_at)
end

#time_string(format: :dhm) ⇒ String

Format [d, h, m] as string

Parameters:

  • format (Symbol) (defaults to: :dhm)

    The format, :dhm, :hm, :m, :clock, :natural

Returns:

  • (String)

    formatted string

Raises:

  • (InvalidArgument)


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_tagsArray

Convert strings to @tags

Examples:

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

Returns:

  • (Array)

    Array of @tags



24
25
26
# File 'lib/doing/array.rb', line 24

def to_tags
  map(&:add_at)
end

#to_tags!Object



28
29
30
# File 'lib/doing/array.rb', line 28

def to_tags!
  replace to_tags
end