Module: GetaroundUtils::Utils::DeepKeyValue
- Defined in:
- lib/getaround_utils/utils/deep_key_value.rb
Class Method Summary collapse
- .flattify(value, result = {}, path = [], max_length = 512, max_depth = 5) ⇒ Object
- .serialize(data) ⇒ Object
Class Method Details
.flattify(value, result = {}, path = [], max_length = 512, max_depth = 5) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/getaround_utils/utils/deep_key_value.rb', line 21 def self.flattify(value, result = {}, path = [], max_length = 512, max_depth = 5) if path.length > max_depth result[path.join(".")] = '"..."' return result end case value when Array value.each.with_index(0) do |v, i| flattify(v, result, path + [i], max_length, max_depth) end when Hash value.each do |k, v| flattify(v, result, path + [k], max_length, max_depth) end when Numeric result[path.join(".")] = value.to_s when String value = if value =~ /^".*"$/ value.length >= max_length ? %{#{value[0...max_length]} ..."} : value else value.length >= max_length ? %{#{value[0...max_length]} ...}.inspect : value.inspect end result[path.join(".")] = value else flattify(value.to_s, result, path, max_length, max_depth) end result end |
.serialize(data) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/getaround_utils/utils/deep_key_value.rb', line 5 def self.serialize(data) case data when Array flattify(data).map { |key, value| "#{key}=#{value}" }.join(' ') when Hash flattify(data).map { |key, value| "#{key}=#{value}" }.join(' ') when Numeric data.to_s when String data =~ /^".*"$/ ? data : data.inspect else data.to_s.inspect end end |