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



30
31
32
33
# File 'lib/doing/array.rb', line 30

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

#log_tags ⇒ String

Tag array for logging

Returns:

  • (String) —

    Highlighted tag array joined with comma



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

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)


49
50
51
52
53
54
55
# File 'lib/doing/array.rb', line 49

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

#to_tags ⇒ Array

Convert strings to @tags

Examples:

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

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

Returns:

  • (Array) —

    Array of @tags



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

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

#to_tags! ⇒ Object



18
19
20
# File 'lib/doing/array.rb', line 18

def to_tags!
  replace to_tags
end