Class: SolidErrors::Sanitizer
- Inherits:
-
Object
- Object
- SolidErrors::Sanitizer
- Defined in:
- lib/solid_errors/sanitizer.rb
Overview
Constant Summary collapse
- BASIC_OBJECT =
"#<BasicObject>".freeze
- DEPTH =
"[DEPTH]".freeze
- RAISED =
"[RAISED]".freeze
- RECURSION =
"[RECURSION]".freeze
- TRUNCATED =
"[TRUNCATED]".freeze
- MAX_STRING_SIZE =
65536
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(max_depth: 20) ⇒ Sanitizer
constructor
A new instance of Sanitizer.
- #sanitize(data, depth = 0, stack = nil) ⇒ Object
Constructor Details
#initialize(max_depth: 20) ⇒ Sanitizer
Returns a new instance of Sanitizer.
16 17 18 |
# File 'lib/solid_errors/sanitizer.rb', line 16 def initialize(max_depth: 20) @max_depth = max_depth end |
Class Method Details
.sanitize(data) ⇒ Object
11 12 13 14 |
# File 'lib/solid_errors/sanitizer.rb', line 11 def self.sanitize(data) @sanitizer ||= new @sanitizer.sanitize(data) end |
Instance Method Details
#sanitize(data, depth = 0, stack = nil) ⇒ Object
20 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/solid_errors/sanitizer.rb', line 20 def sanitize(data, depth = 0, stack = nil) return BASIC_OBJECT if basic_object?(data) if recursive?(data) return RECURSION if stack&.include?(data.object_id) stack = stack ? stack.dup : Set.new stack << data.object_id end case data when Hash return DEPTH if depth >= max_depth new_hash = {} data.each do |key, value| key = key.is_a?(Symbol) ? key : sanitize(key, depth + 1, stack) value = sanitize(value, depth + 1, stack) new_hash[key] = value end new_hash when Array, Set return DEPTH if depth >= max_depth data.to_a.map do |value| sanitize(value, depth + 1, stack) end when Numeric, TrueClass, FalseClass, NilClass data when String sanitize_string(data) else # all other objects klass = data.class begin data = String(data) rescue return RAISED end return "#<#{klass.name}>" if inspected?(data) sanitize_string(data) end end |