Module: Mitrush
- Defined in:
- lib/mitrush.rb,
lib/mitrush/version.rb
Constant Summary collapse
- VERSION =
"0.2.0"
Class Method Summary collapse
- .deep_copy(input) ⇒ Object
- .deep_delete_keys(input, keys) ⇒ Object
- .deep_stringify_keys(input) ⇒ Object
- .deep_stringify_values(input) ⇒ Object
- .deep_symbolise_keys(input) ⇒ Object
- .delete_keys(hash, keys) ⇒ Object
- .rowify(table_row_hashes) ⇒ Object
- .snake_to_camel(snake) ⇒ Object
- .stringified_float?(value) ⇒ Boolean
- .stringified_integer?(value) ⇒ Boolean
- .tablify(table_rows, column_formats) ⇒ Object
- .update_nested_hash_array(input, manual_recursion = false, &block) ⇒ Object
- .valid_json?(json_string) ⇒ Boolean
Class Method Details
.deep_copy(input) ⇒ Object
26 27 28 |
# File 'lib/mitrush.rb', line 26 def self.deep_copy(input) Marshal.load(Marshal.dump(input)) end |
.deep_delete_keys(input, keys) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/mitrush.rb', line 30 def self.deep_delete_keys(input, keys) if input.is_a?(Array) input.each { |item| deep_delete_keys(item, keys) } elsif input.is_a?(Hash) delete_keys(input, keys) input.values.each { |value| deep_delete_keys(value, keys) if [Hash, Array].include?(value.class) } end end |
.deep_stringify_keys(input) ⇒ Object
43 44 45 |
# File 'lib/mitrush.rb', line 43 def self.deep_stringify_keys(input) update_nested_hash_array(input) { |key, _, new_kvp_hash| new_kvp_hash[:new_key] = key.to_s rescue key } end |
.deep_stringify_values(input) ⇒ Object
47 48 49 |
# File 'lib/mitrush.rb', line 47 def self.deep_stringify_values(input) update_nested_hash_array(input) { |_, value, new_kvp_hash| new_kvp_hash[:new_value] = value.to_s rescue value } end |
.deep_symbolise_keys(input) ⇒ Object
39 40 41 |
# File 'lib/mitrush.rb', line 39 def self.deep_symbolise_keys(input) update_nested_hash_array(input) { |key, _, new_kvp_hash| new_kvp_hash[:new_key] = key.to_sym rescue key } end |
.delete_keys(hash, keys) ⇒ Object
22 23 24 |
# File 'lib/mitrush.rb', line 22 def self.delete_keys(hash, keys) hash.delete_if { |key, _| keys.include?(key) } end |
.rowify(table_row_hashes) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/mitrush.rb', line 86 def self.rowify(table_row_hashes) table_row_string_array = table_row_hashes.map do |column_hash| column_string = column_hash[:string].is_a?(String) ? column_hash[:string].dup : column_hash[:string].to_s if column_string.length > column_hash[:width] - 3 column_string = column_string[0..column_hash[:width] - 3] end column_hash[:width].times do column_string = "#{column_string}#{column_hash[:spacer] || ' '}" break if column_string.length >= column_hash[:width] end column_string end table_row_string_array.join end |
.snake_to_camel(snake) ⇒ Object
5 6 7 |
# File 'lib/mitrush.rb', line 5 def self.snake_to_camel(snake) snake.split('_').map { |word| word.capitalize }.join end |
.stringified_float?(value) ⇒ Boolean
13 14 15 |
# File 'lib/mitrush.rb', line 13 def self.stringified_float?(value) /^\d+[.]\d+$/ === value end |
.stringified_integer?(value) ⇒ Boolean
9 10 11 |
# File 'lib/mitrush.rb', line 9 def self.stringified_integer?(value) /\A[-+]?\d+\z/ === value end |
.tablify(table_rows, column_formats) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/mitrush.rb', line 72 def self.tablify(table_rows, column_formats) table_string_array = table_rows.map do |table_row| unless table_row.length == column_formats.length raise 'table_row array lengths must each match column_formats array length' end table_row_hashes = [] table_row.each_with_index do |column_string, i| table_row_hashes << column_formats[i].update(string: column_string) end rowify(table_row_hashes) end table_string_array.join("\n") end |
.update_nested_hash_array(input, manual_recursion = false, &block) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/mitrush.rb', line 51 def self.update_nested_hash_array(input, manual_recursion=false, &block) if input.is_a?(Array) output = [] input.each { |item| output << update_nested_hash_array(item, manual_recursion, &block) } elsif input.is_a?(Hash) output = {} input.each do |key, value| hash_array = nil if value.is_a?(Array) || value.is_a?(Hash) && !manual_recursion hash_array = update_nested_hash_array(value, manual_recursion, &block) end new_kvp_hash = {new_key: key, new_value: value} yield(key, value, new_kvp_hash) output[new_kvp_hash[:new_key] || key] = hash_array || new_kvp_hash[:new_value] || value end else output = input end output end |
.valid_json?(json_string) ⇒ Boolean
17 18 19 20 |
# File 'lib/mitrush.rb', line 17 def self.valid_json?(json_string) return true if JSON.parse(json_string) rescue JSON::ParserError false end |