Module: RFacter::Util::Values Private
- Included in:
- Confine
- Defined in:
- lib/rfacter/util/values.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Helper methods for coercing items to JSON datatypes
Defined Under Namespace
Classes: DeepFreezeError, DeepMergeError
Class Method Summary collapse
- .convert(value) ⇒ Object private
-
.deep_freeze(value) ⇒ void
private
Duplicate and deeply freeze a given data structure.
-
.deep_merge(left, right, path = [], &block) ⇒ Object
private
Perform a deep merge of two nested data structures.
-
.flatten_structure(path, structure) ⇒ Hash
private
Flatten the given data structure to something that’s suitable to return as flat facts.
Class Method Details
.convert(value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
78 79 80 81 82 |
# File 'lib/rfacter/util/values.rb', line 78 def convert(value) value = value.to_s if value.is_a?(Symbol) value = value.downcase if value.is_a?(String) value end |
.deep_freeze(value) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Duplicate and deeply freeze a given data structure
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rfacter/util/values.rb', line 18 def deep_freeze(value) case value when Numeric, Symbol, TrueClass, FalseClass, NilClass # These are immutable values, we can safely ignore them value when String value.dup.freeze when Array value.map do |entry| deep_freeze(entry) end.freeze when Hash value.inject({}) do |hash, (key, value)| hash[deep_freeze(key)] = deep_freeze(value) hash end.freeze else raise DeepFreezeError, "Cannot deep freeze #{value}:#{value.class}" end end |
.deep_merge(left, right, path = [], &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Perform a deep merge of two nested data structures.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rfacter/util/values.rb', line 48 def deep_merge(left, right, path = [], &block) ret = nil if left.is_a? Hash and right.is_a? Hash ret = left.merge(right) do |key, left_val, right_val| path.push(key) merged = deep_merge(left_val, right_val, path) path.pop merged end elsif left.is_a? Array and right.is_a? Array ret = left.dup.concat(right) elsif right.nil? ret = left elsif left.nil? ret = right elsif left.nil? and right.nil? ret = nil else msg = "Cannot merge #{left.inspect}:#{left.class} and #{right.inspect}:#{right.class}" if not path.empty? msg << " at root" msg << path.map { |part| "[#{part.inspect}]" }.join end raise DeepMergeError, msg end ret end |
.flatten_structure(path, structure) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Flatten the given data structure to something that’s suitable to return as flat facts.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rfacter/util/values.rb', line 92 def flatten_structure(path, structure) results = {} if structure.is_a? Hash structure.each_pair do |name, value| new_path = "#{path}_#{name}".gsub(/\-|\//, '_') results.merge! flatten_structure(new_path, value) end elsif structure.is_a? Array structure.each_with_index do |value, index| new_path = "#{path}_#{index}" results.merge! flatten_structure(new_path, value) end else results[path] = structure end results end |