class AwesomePrint::Formatters::ArrayFormatter

Attributes

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

Public Class Methods

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

Public Instance Methods

format() click to toggle source
# File lib/awesome_print/formatters/array_formatter.rb, line 14
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/awesome_print/formatters/array_formatter.rb, line 56
def array_prefix(iteration, width)
  generic_prefix(iteration, width)
end
find_method(name) click to toggle source
# File lib/awesome_print/formatters/array_formatter.rb, line 110
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/awesome_print/formatters/array_formatter.rb, line 48
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/awesome_print/formatters/array_formatter.rb, line 68
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/awesome_print/formatters/array_formatter.rb, line 101
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/awesome_print/formatters/array_formatter.rb, line 126
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/awesome_print/formatters/array_formatter.rb, line 60
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/awesome_print/formatters/array_formatter.rb, line 26
def methods_array?
  array.instance_variable_defined?('@__awesome_methods__')
end
multiline_array() click to toggle source
# File lib/awesome_print/formatters/array_formatter.rb, line 38
def multiline_array
  data = unless should_be_limited?
           generate_printable_array
         else
           limited(generate_printable_array, width(array))
         end

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

  return name_and_args[0].map(&:size).max, name_and_args[1].map(&:size).max
end
simple_array() click to toggle source
# File lib/awesome_print/formatters/array_formatter.rb, line 30
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/awesome_print/formatters/array_formatter.rb, line 97
def tuple_prefix(iteration, width)
  generic_prefix(iteration, width, ' ')
end
tuple_template(item) click to toggle source
# File lib/awesome_print/formatters/array_formatter.rb, line 76
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/awesome_print/formatters/array_formatter.rb, line 87
def tuples
  @tuples ||= array.map { |name| generate_tuple(name) }
end
width(items) click to toggle source
# File lib/awesome_print/formatters/array_formatter.rb, line 134
def width(items)
  (items.size - 1).to_s.size
end