Sha256: d0e3a85462dd2bacfdf220b4bcc63ca0d17a472051d28ff771dbdc0e569faa50

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

module Helium
  class Console
    define_formatter_for Object do
      def call
        return format_inline_with_truncation if force_inline?
        format_as_table
      end

    private

      def format_as_table
        table = Table.new(runner: light_black('| '), after_key: light_black(": "), format_keys: false)

        object.instance_variables.each do |inst|
          table.row(magenta(inst.to_s), object.instance_variable_get(inst))
        end

        [
          table_header,
          format(table),
        ].reject(&:empty?).join($/)
      end

      def format_inline_with_truncation
        "#{object.class.name}##{object.object_id.to_s(16)}"
      end

      def format_inline_with_no_truncation
        joined = nil

        object.each do |key, value|
          return unless simple?(value)

          formatted_key = format(key, level: 3, max_with: 15)
          formatted_value = format(value, level: 3, max_width: 15)
          formatted = "#{formatted_key} => #{formatted_value}"

          joined = [joined, formatted].compact.join(", ")

          return if joined.length > max_width - 4
        end
        joined = " #{joined} " if joined
        ["{", joined, "}"].compact.join
      end

      def table_header
        type = case object
          when Class then :class
          when Module then :module
          else :instance
        end
        "#{light_black('#')} #{light_yellow(type == :instance ? object.class.name : object.name)} #{type}"
      end

      def force_inline?
        level > 2
      end

      def all_symbol?
        object.keys.all? { |key| key.is_a? Symbol }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
helium-console-0.1.5 lib/helium/console/registry/object.rb