Class: Array

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

Overview

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



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

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



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

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)


58
59
60
61
62
63
64
# File 'lib/doing/array.rb', line 58

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 { |t| t.sub(/^@/, '') }
end

#to_tagsArray

Convert strings to @tags

Examples:

['one', '@two', 'three'].to_tags

=> ['@one', '@two', '@three']

Returns:

  • (Array)

    Array of @tags



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

def to_tags
  map { |t| t.sub(/^@?/, '@') }
end

#to_tags!Object



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

def to_tags!
  replace to_tags
end