# typed: ignore # Copyright (c) 2015 Sqreen. All Rights Reserved. # Please refer to our terms for more information: https://www.sqreen.com/terms.html require 'set' module Sqreen class BindingAccessor # Available final transformations module Transforms def flat_keys(value, max_iter = 1000) return nil if value.nil? seen = Set.new look_into = [value] keys = [] idx = 0 until look_into.empty? || max_iter <= idx idx += 1 val = look_into.pop next unless seen.add?(val.object_id) case val when Hash # convert symbols are converted to strings (ARB-60) keys.concat(val.keys.map { |x| sym_to_s(x) }) look_into.concat(val.values) when Array look_into.concat(val) else next if val.respond_to?(:seek) val.each { |v| look_into << v } if val.respond_to?(:each) end end keys end def flat_values(value, max_iter = 1000) return nil if value.nil? seen = Set.new look_into = [value] values = [] idx = 0 until look_into.empty? || max_iter <= idx idx += 1 val = look_into.shift next unless seen.add?(val.object_id) case val when Hash look_into.concat(val.values) when Array look_into.concat(val) else next if val.respond_to?(:seek) if val.respond_to?(:each) val.each { |v| look_into << v } else values << val end end end values end def concat_keys_and_values(value, max_size = nil) return nil if value.nil? values = Set.new max_size = max_size.to_i if max_size res = '' descend(value) do |x| next unless values.add?(x) x = x.to_s return res if max_size && res.size + x.size + 1 > max_size res << "\n" unless res.empty? res << x end res end private def sym_to_s(value) value.is_a?(Symbol) ? value.to_s : value end def descend(value, max_iter = 1000) seen = Set.new look_into = [value] idx = 0 until look_into.empty? || max_iter <= idx idx += 1 val = look_into.pop case val when Hash next unless seen.add?(val.object_id) look_into.concat(val.keys) look_into.concat(val.values) when Array next unless seen.add?(val.object_id) look_into.concat(val) else next if val.respond_to?(:seek) if val.respond_to?(:each) next unless seen.add?(val.object_id) val.each { |v| look_into << v } else yield val end end end end end end end