Module: Glossarist::Utilities::CommonFunctions
- Included in:
- ConceptSource, Designation::GrammarInfo, ManagedConcept
- Defined in:
- lib/glossarist/utilities/common_functions.rb
Instance Method Summary collapse
- #convert_keys_to_snake_case(hash) ⇒ Object
-
#slice_keys(hash, keys) ⇒ Object
Hash#slice is not available in Ruby 2.4 so we have to do this ourselves :( slice hash keys.
- #snake_case(str) ⇒ Object
-
#stringify_keys(hash) ⇒ Object
Hash#transform_keys is not available in Ruby 2.4 so we have to do this ourselves :( symbolize hash keys.
-
#symbolize_keys(hash) ⇒ Object
Hash#transform_keys is not available in Ruby 2.4 so we have to do this ourselves :( symbolize hash keys.
Instance Method Details
#convert_keys_to_snake_case(hash) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/glossarist/utilities/common_functions.rb', line 47 def convert_keys_to_snake_case(hash) result = {} hash.each_pair do |key, value| result[snake_case(key)] = if value.is_a?(Hash) convert_keys_to_snake_case(value) else value end end result end |
#slice_keys(hash, keys) ⇒ Object
Hash#slice is not available in Ruby 2.4 so we have to do this ourselves :( slice hash keys
39 40 41 42 43 44 45 |
# File 'lib/glossarist/utilities/common_functions.rb', line 39 def slice_keys(hash, keys) result = {} keys.each do |key| result[key] = hash[key] if hash.key?(key) end result end |
#snake_case(str) ⇒ Object
59 60 61 |
# File 'lib/glossarist/utilities/common_functions.rb', line 59 def snake_case(str) str.gsub(/([A-Z])/) { "_#{$1.downcase}" } end |
#stringify_keys(hash) ⇒ Object
Hash#transform_keys is not available in Ruby 2.4 so we have to do this ourselves :( symbolize hash keys
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/glossarist/utilities/common_functions.rb', line 24 def stringify_keys(hash) result = {} hash.each_pair do |key, value| result[key.to_s] = if value.is_a?(Hash) stringify_keys(value) else value end end result end |
#symbolize_keys(hash) ⇒ Object
Hash#transform_keys is not available in Ruby 2.4 so we have to do this ourselves :( symbolize hash keys
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/glossarist/utilities/common_functions.rb', line 9 def symbolize_keys(hash) result = {} hash.each_pair do |key, value| result[key.to_sym] = if value.is_a?(Hash) symbolize_keys(value) else value end end result end |