class AmazingPrint::Formatters::ArrayFormatter

Attributes

array[R]
inspector[R]
options[R]

Public Class Methods

new(array, inspector) click to toggle source
# File lib/amazing_print/formatters/array_formatter.rb, line 10
def initialize(array, inspector)
  @array = array
  @inspector = inspector
  @options = inspector.options
end

Public Instance Methods

format() click to toggle source
# File lib/amazing_print/formatters/array_formatter.rb, line 16
def format
  if array.length.zero?
    '[]'
  elsif methods_array?
    methods_array
  else
    simple_array
  end
end

Private Instance Methods

array_prefix(iteration, width) click to toggle source
# File lib/amazing_print/formatters/array_formatter.rb, line 58
def array_prefix(iteration, width)
  generic_prefix(iteration, width)
end
find_method(name) click to toggle source
# File lib/amazing_print/formatters/array_formatter.rb, line 112
def find_method(name)
  object = array.instance_variable_get('@__awesome_methods__')

  meth = begin
    object.method(name)
         rescue NameError, ArgumentError
           nil
  end

  meth || begin
    object.instance_method(name)
          rescue NameError
            nil
  end
end
generate_printable_array() click to toggle source
# File lib/amazing_print/formatters/array_formatter.rb, line 50
def generate_printable_array
  array.map.with_index do |item, index|
    array_prefix(index, width(array)).tap do |temp|
      indented { temp << inspector.awesome(item) }
    end
  end
end
generate_printable_tuples() click to toggle source
# File lib/amazing_print/formatters/array_formatter.rb, line 70
def generate_printable_tuples
  tuples.map.with_index do |item, index|
    tuple_prefix(index, width(tuples)).tap do |temp|
      indented { temp << tuple_template(item) }
    end
  end
end
generate_tuple(name) click to toggle source
# File lib/amazing_print/formatters/array_formatter.rb, line 103
def generate_tuple(name)
  meth = case name
         when Symbol, String
           find_method(name)
         end

  meth ? method_tuple(meth) : [name.to_s, '(?)', '?']
end
generic_prefix(iteration, width, padding = '') click to toggle source
# File lib/amazing_print/formatters/array_formatter.rb, line 128
def generic_prefix(iteration, width, padding = '')
  if options[:index]
    indent + colorize("[#{iteration.to_s.rjust(width)}] ", :array)
  else
    indent + padding
  end
end
methods_array() click to toggle source
# File lib/amazing_print/formatters/array_formatter.rb, line 62
def methods_array
  array.map!(&:to_s).sort!

  data = generate_printable_tuples.join("\n")

  "[\n#{data}\n#{outdent}]"
end
methods_array?() click to toggle source
# File lib/amazing_print/formatters/array_formatter.rb, line 28
def methods_array?
  array.instance_variable_defined?('@__awesome_methods__')
end
multiline_array() click to toggle source
# File lib/amazing_print/formatters/array_formatter.rb, line 40
def multiline_array
  data = if should_be_limited?
           limited(generate_printable_array, width(array))
         else
           generate_printable_array
         end

  %([\n#{data.join(",\n")}\n#{outdent}])
end
name_and_args_width() click to toggle source
# File lib/amazing_print/formatters/array_formatter.rb, line 93
def name_and_args_width
  name_and_args = tuples.transpose

  [name_and_args[0].map(&:size).max, name_and_args[1].map(&:size).max]
end
simple_array() click to toggle source
# File lib/amazing_print/formatters/array_formatter.rb, line 32
def simple_array
  if options[:multiline]
    multiline_array
  else
    '[ ' + array.map { |item| inspector.awesome(item) }.join(', ') + ' ]'
  end
end
tuple_prefix(iteration, width) click to toggle source
# File lib/amazing_print/formatters/array_formatter.rb, line 99
def tuple_prefix(iteration, width)
  generic_prefix(iteration, width, ' ')
end
tuple_template(item) click to toggle source
# File lib/amazing_print/formatters/array_formatter.rb, line 78
def tuple_template(item)
  name_width, args_width = name_and_args_width

  [
    colorize(item[0].rjust(name_width), :method),
    colorize(item[1].ljust(args_width), :args),
    ' ',
    colorize(item[2], :class)
  ].join
end
tuples() click to toggle source
# File lib/amazing_print/formatters/array_formatter.rb, line 89
def tuples
  @tuples ||= array.map { |name| generate_tuple(name) }
end
width(items) click to toggle source
# File lib/amazing_print/formatters/array_formatter.rb, line 136
def width(items)
  (items.size - 1).to_s.size
end