Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/awesome_print/core_ext/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
module Kernel

def ai(options = {})
ap = AwesomePrint::Inspector.new(options)
awesome = ap.awesome self
inspector = AwesomePrint::Inspector.new(options.merge(top_layer: false))
awesome = inspector.awesome self
if options[:html]
awesome = "<pre>#{awesome}</pre>"
awesome = "<pre>#{awesome}</pre>" unless options[:top_layer] == false
awesome = awesome.html_safe if defined? ActiveSupport
end
awesome
Expand Down
16 changes: 8 additions & 8 deletions lib/awesome_print/ext/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ def self.included(base)
# Add ActiveRecord class names to the dispatcher pipeline.
#------------------------------------------------------------------------------
def cast_with_active_record(object, type)
cast = cast_without_active_record(object, type)
return cast if !defined?(::ActiveRecord::Base)

if object.is_a?(::ActiveRecord::Base)
cast = :active_record_instance
if !defined?(::ActiveRecord::Base)
cast_without_active_record(object, type)
elsif object.is_a?(::ActiveRecord::Base)
:active_record_instance
elsif object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base)
cast = :active_record_class
:active_record_class
elsif type == :activerecord_relation || object.class.ancestors.include?(::ActiveRecord::Relation)
cast = :array
:array
else
cast_without_active_record(object, type)
end
cast
end

private
Expand Down
4 changes: 2 additions & 2 deletions lib/awesome_print/formatters/array_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def simple_array
if options[:multiline]
multiline_array
else
'[ ' << array.map { |item| inspector.awesome(item) }.join(', ') << ' ]'
'[ ' << array.map { |item| item.ai(@options.merge(current_indentation: inspector.current_indentation)) }.join(', ') << ' ]'
end
end

Expand All @@ -48,7 +48,7 @@ def multiline_array
def generate_printable_array
array.map.with_index do |item, index|
array_prefix(index, width(array)).tap do |temp|
indented { temp << inspector.awesome(item) }
indented { temp << item.ai(@options.merge(current_indentation: inspector.current_indentation)) }
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/awesome_print/formatters/hash_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def printable_keys

keys.map! do |key|
plain_single_line do
[inspector.awesome(key), hash[key]]
[key.ai(@options), hash[key]]
end
end
end
Expand All @@ -84,11 +84,11 @@ def symbol?(key)

def ruby19_syntax(key, value, width)
key[0] = ''
align(key, width - 1) << colorize(': ', :hash) << inspector.awesome(value)
align(key, width - 1) << colorize(': ', :hash) << value.ai(@options.merge(current_indentation: inspector.current_indentation))
end

def pre_ruby19_syntax(key, value, width)
align(key, width) << colorize(' => ', :hash) << inspector.awesome(value)
align(key, width) << colorize(' => ', :hash) << value.ai(@options.merge(current_indentation: inspector.current_indentation))
end

def plain_single_line
Expand Down
2 changes: 1 addition & 1 deletion lib/awesome_print/formatters/object_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def format
end

indented do
key << colorize(' = ', :hash) + inspector.awesome(object.instance_variable_get(var))
key << colorize(' = ', :hash) + object.instance_variable_get(var).ai(@options.merge(current_indentation: inspector.current_indentation))
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/awesome_print/formatters/struct_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def format
end

indented do
key << colorize(' = ', :hash) + inspector.awesome(struct.send(var))
key << colorize(' = ', :hash) + struct.send(var).ai(@options.merge(current_indentation: inspector.current_indentation))
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/awesome_print/indentator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ class Indentator

attr_reader :shift_width, :indentation

def initialize(indentation)
@indentation = indentation
def initialize(indentation, current = nil)
@indentation = current ? current : indentation
@shift_width = indentation.freeze
end

Expand Down
25 changes: 14 additions & 11 deletions lib/awesome_print/inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ class Inspector

def initialize(options = {})
@options = {
indent: 4, # Number of spaces for indenting.
index: true, # Display array indices.
html: false, # Use ANSI color codes rather than HTML.
multiline: true, # Display in multiple lines.
plain: false, # Use colors.
raw: false, # Do not recursively format instance variables.
sort_keys: false, # Do not sort hash keys.
sort_vars: true, # Sort instance variables.
limit: false, # Limit arrays & hashes. Accepts bool or int.
ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output.
indent: 4, # Number of spaces for indenting.
current_indentation: nil, # The current amount of indentation.
index: true, # Display array indices.
html: false, # Use ANSI color codes rather than HTML.
multiline: true, # Display in multiple lines.
plain: false, # Use colors.
raw: false, # Do not recursively format instance variables.
sort_keys: false, # Do not sort hash keys.
sort_vars: true, # Sort instance variables.
limit: false, # Limit arrays & hashes. Accepts bool or int.
ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output.
color: {
args: :pale,
array: :white,
Expand Down Expand Up @@ -51,7 +52,8 @@ def initialize(options = {})
merge_options!(options)

@formatter = AwesomePrint::Formatter.new(self)
@indentator = AwesomePrint::Indentator.new(@options[:indent].abs)

@indentator = AwesomePrint::Indentator.new(@options[:indent].abs, @options[:current_indentation])
Thread.current[AP] ||= []
end

Expand Down Expand Up @@ -134,6 +136,7 @@ def printable(object)
# keys.
#---------------------------------------------------------------------------
def merge_options!(options = {})
options = options.dup
@options[:color].merge!(options.delete(:color) || {})
@options.merge!(options)
end
Expand Down