Class: GenesisRuby::Utils::Common
- Inherits:
-
Object
- Object
- GenesisRuby::Utils::Common
- Defined in:
- lib/genesis_ruby/utils/common.rb
Overview
Common Utils used all across the project
Class Method Summary collapse
-
.camel_to_snake_case(camel_cased_word) ⇒ Object
String conversion from CamelCase to snake_case.
-
.compact_array!(structure) ⇒ Object
Compact over given Array.
-
.compact_hash!(structure) ⇒ Object
Compact over given Hash.
-
.constant_values(reference) ⇒ Object
Retrieve all constant values defined in the given module or class.
-
.date_has_time?(value) ⇒ Boolean
Check if the given string contain time.
-
.deep_compact!(structure) ⇒ Object
Deep compact over the given nested structure.
-
.deep_compact_empty!(structure) ⇒ Object
Deep compact empty over the given nested hash.
-
.empty_compact_array!(structure) ⇒ Object
Remove empty elements from an Array.
-
.empty_compact_hash!(structure) ⇒ Object
Remove empty elements from an Hash.
-
.parse_date(date, formats) ⇒ Object
Parses string to DateTime.
-
.parse_json_string(value) ⇒ Object
Parse JSON string.
-
.snake_to_camel_case(snake_case_word, lower: true) ⇒ Object
String conversion from snake_case to CamelCase.
-
.valid_url?(url) ⇒ Boolean
Validates URL.
Class Method Details
.camel_to_snake_case(camel_cased_word) ⇒ Object
String conversion from CamelCase to snake_case
122 123 124 125 126 127 128 |
# File 'lib/genesis_ruby/utils/common.rb', line 122 def camel_to_snake_case(camel_cased_word) camel_cased_word.to_s.gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('-', '_') .downcase end |
.compact_array!(structure) ⇒ Object
Compact over given Array
26 27 28 29 30 31 32 |
# File 'lib/genesis_ruby/utils/common.rb', line 26 def compact_array!(structure) return unless structure.is_a? Array structure.reject! do |element| element.is_a?(Array) || element.is_a?(Hash) ? element.empty? : element.nil? end end |
.compact_hash!(structure) ⇒ Object
Compact over given Hash
35 36 37 38 39 40 41 |
# File 'lib/genesis_ruby/utils/common.rb', line 35 def compact_hash!(structure) return unless structure.is_a? Hash structure.reject! do |_key, value| value.is_a?(Array) || value.is_a?(Hash) ? value.empty? : value.nil? end end |
.constant_values(reference) ⇒ Object
Retrieve all constant values defined in the given module or class
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/genesis_ruby/utils/common.rb', line 73 def constant_values(reference) constant_values = [] reference.constants(false).each do |constant_value| value = reference.const_get constant_value constant_values.push value unless value.is_a?(Class) || value.is_a?(Module) end constant_values.sort end |
.date_has_time?(value) ⇒ Boolean
Check if the given string contain time
108 109 110 111 112 |
# File 'lib/genesis_ruby/utils/common.rb', line 108 def date_has_time?(value) return true if value =~ /(?:\d|[01]\d|2[0-3]):[0-5]\d:[0-5]\d/ false end |
.deep_compact!(structure) ⇒ Object
Deep compact over the given nested structure
15 16 17 18 19 20 21 22 23 |
# File 'lib/genesis_ruby/utils/common.rb', line 15 def deep_compact!(structure) structure.each do |element_key, element_value| deep_compact!(element_key) if element_key.is_a? Hash deep_compact!(element_value) if element_value.is_a?(Hash) || element_value.is_a?(Array) end compact_array! structure compact_hash! structure end |
.deep_compact_empty!(structure) ⇒ Object
Deep compact empty over the given nested hash
44 45 46 47 48 49 50 51 52 |
# File 'lib/genesis_ruby/utils/common.rb', line 44 def deep_compact_empty!(structure) structure.each do |element_key, element_value| deep_compact_empty!(element_key) if element_key.is_a? Hash deep_compact_empty!(element_value) if element_value.is_a?(Hash) || element_value.is_a?(Array) end empty_compact_array!(structure) empty_compact_hash!(structure) end |
.empty_compact_array!(structure) ⇒ Object
Remove empty elements from an Array
55 56 57 58 59 60 61 |
# File 'lib/genesis_ruby/utils/common.rb', line 55 def empty_compact_array!(structure) return unless structure.is_a? Array structure.reject! do |element| element.is_a?(Array) || element.is_a?(Hash) || element.is_a?(String) ? element.empty? : element.nil? end end |
.empty_compact_hash!(structure) ⇒ Object
Remove empty elements from an Hash
64 65 66 67 68 69 70 |
# File 'lib/genesis_ruby/utils/common.rb', line 64 def empty_compact_hash!(structure) return unless structure.is_a? Hash structure.reject! do |_key, value| value.is_a?(Array) || value.is_a?(Hash) || value.is_a?(String) ? value.empty? : value.nil? end end |
.parse_date(date, formats) ⇒ Object
Parses string to DateTime
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/genesis_ruby/utils/common.rb', line 95 def parse_date(date, formats) parsed_date = nil formats.each do |format| return DateTime.strptime(date, format) rescue StandardError parsed_date = nil end parsed_date end |
.parse_json_string(value) ⇒ Object
Parse JSON string
115 116 117 118 119 |
# File 'lib/genesis_ruby/utils/common.rb', line 115 def parse_json_string(value) JSON.parse value rescue StandardError => e raise InvalidArgumentError, "Given JSON string is invalid: #{e.}" end |
.snake_to_camel_case(snake_case_word, lower: true) ⇒ Object
String conversion from snake_case to CamelCase
131 132 133 134 135 136 137 |
# File 'lib/genesis_ruby/utils/common.rb', line 131 def snake_to_camel_case(snake_case_word, lower: true) string = snake_case_word.to_s.split(/_/).map(&:capitalize).join string.sub!(/^[[:alpha:]]/, &:downcase) if lower string end |
.valid_url?(url) ⇒ Boolean
Validates URL
86 87 88 89 90 91 92 |
# File 'lib/genesis_ruby/utils/common.rb', line 86 def valid_url?(url) uri = URI.parse(url) %w(http https).include?(uri.scheme) && !uri.host.nil? && !uri.host.to_s.empty? rescue StandardError false end |