Class: AwesomePrint::Formatters::ArrayFormatter

Inherits:
BaseFormatter show all
Defined in:
lib/awesome_print/formatters/array_formatter.rb

Constant Summary

Constants inherited from BaseFormatter

BaseFormatter::DEFAULT_LIMIT_SIZE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseFormatter

#align, #get_limit_size, #indent, #indentation, #indented, #limited, #method_tuple, #outdent, #should_be_limited?

Methods included from Colorize

#colorize

Constructor Details

#initialize(array, inspector) ⇒ ArrayFormatter

Returns a new instance of ArrayFormatter.



9
10
11
12
13
# File 'lib/awesome_print/formatters/array_formatter.rb', line 9

def initialize(array, inspector)
  @array = array
  @inspector = inspector
  @options = inspector.options
end

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



7
8
9
# File 'lib/awesome_print/formatters/array_formatter.rb', line 7

def array
  @array
end

#inspectorObject (readonly)

Returns the value of attribute inspector.



7
8
9
# File 'lib/awesome_print/formatters/array_formatter.rb', line 7

def inspector
  @inspector
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/awesome_print/formatters/array_formatter.rb', line 7

def options
  @options
end

Instance Method Details

#formatObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/awesome_print/formatters/array_formatter.rb', line 15

def format
  return "[]" if array == []

  if array.instance_variable_defined?('@__awesome_methods__')
    methods_array(array)
  elsif options[:multiline]
    width = (array.size - 1).to_s.size

    data = array.inject([]) do |arr, item|
      index = indent
      index << colorize("[#{arr.size.to_s.rjust(width)}] ", :array) if options[:index]
      indented do
        arr << (index << inspector.awesome(item))
      end
    end

    data = limited(data, width) if should_be_limited?
    "[\n" << data.join(",\n") << "\n#{outdent}]"
  else
    "[ " << array.map{ |item| inspector.awesome(item) }.join(", ") << " ]"
  end
end