Class: Array

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

Overview

Array helpers

Instance Method Summary collapse

Instance Method Details

#abbr_choices(default: nil) ⇒ Object

Convert an array of "(c)hoices" to abbrevation. If a default character is provided it will be highlighted. Output is a color template, unprocessed.

Examples:

["(c)hoice", "(o)ther"].abbr_choices #=> "[c/o]"

Parameters:

  • default (String) (defaults to: nil)

    The (unprocessed) color templated output string



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/planter/array.rb', line 14

def abbr_choices(default: nil)
  return default.nil? ? '' : "{xdw}[{xbc}#{default}{dw}]{x}" if all? { |c| c.to_i.positive? }

  chars = join(' ').scan(/\((?:(.)\.?)\)/).map { |c| c[0] }

  return default.nil? ? '' : "{xdw}[{xbc}#{default}{dw}]{x}" if chars.all? { |c| c.to_i.positive? }

  die('Array contains duplicates', :input) if chars.duplicates?

  out = String.new
  out << '{xdw}['
  out << chars.map do |c|
    if default && c.downcase == default.downcase
      "{xbc}#{c}"
    else
      "{xbw}#{c}"
    end
  end.join('{dw}/')
  out << '{dw}]{x}'
end

#choices_to_hashHash

Convert an array of choices to a hash

  • If the array contains hashes, they are converted to key/value pairs
  • If the array contains strings, they are used as both key and value

Returns:

  • (Hash)

    Hash of choices



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/planter/array.rb', line 76

def choices_to_hash
  hash = {}
  each do |c|
    if c.is_a?(Hash)
      hash[c.keys.first.to_s] = c.values.first.to_s
    else
      hash[c.to_s] = c.to_s
    end
  end

  hash
end

#deep_dupArray

Deep duplicate an array of hashes or arrays

Returns:

  • (Array)

    Deep duplicated array



144
145
146
# File 'lib/planter/array.rb', line 144

def deep_dup
  map { |v| v.is_a?(Hash) || v.is_a?(Array) ? v.deep_dup : v.dup }
end

#duplicates?Boolean

test if array has duplicates

Returns:

  • (Boolean)


55
56
57
# File 'lib/planter/array.rb', line 55

def duplicates?
  uniq.size != size
end

#option_index(choice) ⇒ Integer

Find the index of a choice in an array of choices

Parameters:

  • choice (String)

    The choice to find

Returns:

  • (Integer)

    Index of the choice



65
66
67
68
# File 'lib/planter/array.rb', line 65

def option_index(choice)
  index = find_index { |c| c.to_s.match(/\((.+)\)/)[1].strip.sub(/\.$/, '') == choice }
  index || false
end

#stringify_keysArray

Stringify keys in an array of hashes or arrays

Returns:

  • (Array)

    Array with nested hash keys stringified



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/planter/array.rb', line 102

def stringify_keys
  each_with_object([]) do |v, arr|
    arr << if v.is_a?(Hash)
             v.stringify_keys
           elsif v.is_a?(Array)
             v.map { |x| x.is_a?(Hash) || x.is_a?(Array) ? x.stringify_keys : x }
           else
             v
           end
  end
end

#symbolize_keysArray

Symbolize keys in an array of hashes or arrays

Returns:

  • (Array)

    Array with nested hash keys symbolized



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/planter/array.rb', line 119

def symbolize_keys
  each_with_object([]) do |v, arr|
    arr << if v.is_a?(Hash)
             v.symbolize_keys
           elsif v.is_a?(Array)
             v.map { |x| x.is_a?(Hash) || x.is_a?(Array) ? x.symbolize_keys : x }
           else
             v
           end
  end
end

#symbolize_keys!Array

Destructive version of #symbolize_keys

Returns:

  • (Array)

    Array with symbolized keys



136
137
138
# File 'lib/planter/array.rb', line 136

def symbolize_keys!
  replace deep_dup.symbolize_keys
end

#to_options(numeric) ⇒ Array

Convert an array of choices to a string with optional numbering

Parameters:

  • numeric (Boolean)

    Include numbering

Returns:

  • (Array)

    Array of choices



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/planter/array.rb', line 41

def to_options(numeric)
  die('Array contains duplicates', :input) if duplicates?

  map.with_index do |c, i|
    # v = c.to_s.match(/\(?([a-z]|\d+\.?)\)?/)[1].strip
    if numeric
      "(#{i + 1}). #{c.to_s.sub(/^\(?\d+\.?\)? +/, '')}"
    else
      c
    end
  end
end

#to_valuesArray

Clean strings in an array by removing numbers and parentheses

Returns:

  • (Array)

    Array with cleaned strings



93
94
95
# File 'lib/planter/array.rb', line 93

def to_values
  map(&:clean_value)
end