Module: RailsTracepointStack::Truncator
- Defined in:
- lib/rails_tracepoint_stack/truncator.rb
Overview
Shrinks an already-serialized value so one fat argument (a big payload, a long SQL string, a loaded association) cannot dominate the output. Runs over the plain structures LogFormatter.safe_value produces, so it only ever sees strings, numbers, booleans, nil, arrays and hashes.
Constant Summary collapse
- ELLIPSIS =
"…".freeze
Class Method Summary collapse
- .call(value, limits) ⇒ Object
- .truncate_array(value, limits) ⇒ Object
- .truncate_hash(value, limits) ⇒ Object
- .truncate_string(value, limits) ⇒ Object
Class Method Details
.call(value, limits) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/rails_tracepoint_stack/truncator.rb', line 9 def self.call(value, limits) case value when String then truncate_string(value, limits) when Array then truncate_array(value, limits) when Hash then truncate_hash(value, limits) else value end end |
.truncate_array(value, limits) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/rails_tracepoint_stack/truncator.rb', line 25 def self.truncate_array(value, limits) max = limits.max_collection_size kept = (max.nil? || value.length <= max) ? value : value.first(max) result = kept.map { |item| call(item, limits) } return result if kept.length == value.length result << "(+#{value.length - kept.length} more)" end |
.truncate_hash(value, limits) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rails_tracepoint_stack/truncator.rb', line 35 def self.truncate_hash(value, limits) max = limits.max_collection_size dropped = (max.nil? || value.size <= max) ? 0 : value.size - max kept = dropped.zero? ? value : value.first(max).to_h result = kept.each_with_object({}) do |(key, item), memo| memo[key] = call(item, limits) end return result if dropped.zero? result[ELLIPSIS] = "(+#{dropped} more)" result end |
.truncate_string(value, limits) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/rails_tracepoint_stack/truncator.rb', line 18 def self.truncate_string(value, limits) max = limits.max_string_length return value if max.nil? || value.length <= max "#{value[0, max]}#{ELLIPSIS} (#{value.length} chars)" end |