Sha256: d7aeede9fe00e1ebfb48fd00a28f154710b1710afdf110e6024087f90293eb97

Contents?: true

Size: 1.04 KB

Versions: 15

Compression:

Stored size: 1.04 KB

Contents

require "cgi"
require "objspace" rescue nil

module BetterErrors
  class ValueLargerThanConfiguredMaximum < StandardError; end

  class InspectableValue
    def initialize(value)
      @original_value = value
    end

    def to_html
      raise ValueLargerThanConfiguredMaximum unless value_small_enough_to_inspect?
      value_as_html
    end

    private

    attr_reader :original_value

    def value_as_html
      @value_as_html ||= CGI.escapeHTML(value)
    end

    def value
      @value ||= begin
        if original_value.respond_to? :inspect
          original_value.inspect
        else
          original_value
        end
      end
    end

    def value_small_enough_to_inspect?
      return true if BetterErrors.maximum_variable_inspect_size.nil?

      if defined?(ObjectSpace) && defined?(ObjectSpace.memsize_of) && ObjectSpace.memsize_of(value)
        ObjectSpace.memsize_of(value) <= BetterErrors.maximum_variable_inspect_size
      else
        value_as_html.length <= BetterErrors.maximum_variable_inspect_size
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
better_errors-2.10.1 lib/better_errors/inspectable_value.rb
better_errors-2.10.0 lib/better_errors/inspectable_value.rb
better_errors-2.10.0.beta2 lib/better_errors/inspectable_value.rb
better_errors-2.10.0.beta1 lib/better_errors/inspectable_value.rb
better_errors-2.9.1 lib/better_errors/inspectable_value.rb
better_errors-2.9.0 lib/better_errors/inspectable_value.rb
better_errors-2.8.3 lib/better_errors/inspectable_value.rb
better_errors-2.8.2 lib/better_errors/inspectable_value.rb
better_errors-2.8.1 lib/better_errors/inspectable_value.rb
better_errors-2.8.0 lib/better_errors/inspectable_value.rb
better_errors-2.7.1 lib/better_errors/inspectable_value.rb
better_errors-2.7.0 lib/better_errors/inspectable_value.rb
better_errors-2.6.0 lib/better_errors/inspectable_value.rb
better_errors-2.5.1 lib/better_errors/inspectable_value.rb
better_errors-2.5.0 lib/better_errors/inspectable_value.rb