Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/planter/array.rb
Overview
Array helpers
Instance Method Summary collapse
-
#abbr_choices(default: nil) ⇒ Object
Convert an array of "(c)hoices" to abbrevation.
-
#choices_to_hash ⇒ Hash
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.
-
#deep_dup ⇒ Array
Deep duplicate an array of hashes or arrays.
-
#duplicates? ⇒ Boolean
test if array has duplicates.
-
#option_index(choice) ⇒ Integer
Find the index of a choice in an array of choices.
-
#stringify_keys ⇒ Array
Stringify keys in an array of hashes or arrays.
-
#symbolize_keys ⇒ Array
Symbolize keys in an array of hashes or arrays.
-
#symbolize_keys! ⇒ Array
Destructive version of #symbolize_keys.
-
#to_options(numeric) ⇒ Array
Convert an array of choices to a string with optional numbering.
-
#to_values ⇒ Array
Clean strings in an array by removing numbers and parentheses.
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.
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_hash ⇒ Hash
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
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_dup ⇒ Array
Deep duplicate an array of hashes or arrays
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
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
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_keys ⇒ Array
Stringify keys in an array of hashes or arrays
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_keys ⇒ Array
Symbolize keys in an array of hashes or arrays
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
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
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/planter/array.rb', line 41 def (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_values ⇒ Array
Clean strings in an array by removing numbers and parentheses
93 94 95 |
# File 'lib/planter/array.rb', line 93 def to_values map(&:clean_value) end |