Module: Facter::Utils
- Defined in:
- lib/facter/util/utils.rb
Class Method Summary collapse
- .deep_stringify_keys(object) ⇒ Object
-
.sort_hash_by_key(hash, recursive: true, &block) ⇒ Object
Sort nested hash.
- .split_user_query(user_query) ⇒ Object
- .try_to_bool(value) ⇒ Object
- .try_to_int(value) ⇒ Object
Class Method Details
.deep_stringify_keys(object) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/facter/util/utils.rb', line 20 def self.deep_stringify_keys(object) case object when Hash object.each_with_object({}) do |(key, value), result| result[key.to_s] = deep_stringify_keys(value) end when Array object.map { |e| deep_stringify_keys(e) } else object end end |
.sort_hash_by_key(hash, recursive: true, &block) ⇒ Object
Sort nested hash.
6 7 8 9 10 11 12 13 |
# File 'lib/facter/util/utils.rb', line 6 def self.sort_hash_by_key(hash, recursive: true, &block) hash.keys.sort(&block).each_with_object(hash.class.new) do |key, seed| seed[key] = hash[key] seed[key] = sort_hash_by_key(seed[key], recursive: true, &block) if recursive && seed[key].is_a?(Hash) seed end end |
.split_user_query(user_query) ⇒ Object
15 16 17 18 |
# File 'lib/facter/util/utils.rb', line 15 def self.split_user_query(user_query) queries = user_query.split('.') queries.map! { |query| /^[0-9]+$/.match?(query) ? query.to_i : query } end |
.try_to_bool(value) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/facter/util/utils.rb', line 33 def self.try_to_bool(value) case value.to_s when 'true' true when 'false' false else value end end |
.try_to_int(value) ⇒ Object
44 45 46 47 48 |
# File 'lib/facter/util/utils.rb', line 44 def self.try_to_int(value) Integer(value) rescue ArgumentError, TypeError value end |