Class: Telebugs::Truncator
- Inherits:
-
Object
- Object
- Telebugs::Truncator
- Defined in:
- lib/telebugs/truncator.rb
Overview
This class is responsible for the truncation of too-big objects. Mainly, you should use it for simple objects such as strings, hashes, & arrays.
Constant Summary collapse
- ENCODING_OPTIONS =
The options for String#encode
{invalid: :replace, undef: :replace}.freeze
- TEMP_ENCODING =
The temporary encoding to be used when fixing invalid strings with
ENCODING_OPTIONS
"utf-16"
- SUPPORTED_ENCODINGS =
Encodings that are eligible for fixing invalid characters
[Encoding::UTF_8, Encoding::ASCII].freeze
- CIRCULAR =
What to append when something is a circular reference
"[Circular]"
- TRUNCATED =
What to append when something is truncated
"[Truncated]"
- CIRCULAR_TYPES =
The types that can contain references to itself
[Array, Hash, Set].freeze
Instance Method Summary collapse
-
#initialize(max_size) ⇒ Truncator
constructor
Maximum size of hashes, arrays and strings.
-
#reduce_max_size ⇒ Object
Reduces maximum allowed size of hashes, arrays, sets & strings by half.
-
#truncate(object, seen = Set.new) ⇒ Object
Performs deep truncation of arrays, hashes, sets & strings.
Constructor Details
#initialize(max_size) ⇒ Truncator
Maximum size of hashes, arrays and strings
27 28 29 |
# File 'lib/telebugs/truncator.rb', line 27 def initialize(max_size) @max_size = max_size end |
Instance Method Details
#reduce_max_size ⇒ Object
Reduces maximum allowed size of hashes, arrays, sets & strings by half.
43 44 45 |
# File 'lib/telebugs/truncator.rb', line 43 def reduce_max_size @max_size /= 2 end |
#truncate(object, seen = Set.new) ⇒ Object
Performs deep truncation of arrays, hashes, sets & strings. Uses a placeholder for recursive objects (‘[Circular]`).
33 34 35 36 37 38 39 40 |
# File 'lib/telebugs/truncator.rb', line 33 def truncate(object, seen = Set.new) if seen.include?(object.object_id) return CIRCULAR if CIRCULAR_TYPES.any? { |t| object.is_a?(t) } return object end truncate_object(object, seen << object.object_id) end |