Skip to content

Commit

Permalink
Allow any object to override awesome print via #ai
Browse files Browse the repository at this point in the history
Bug fix: Duplicate options before mutating it
-----------------------------------------------------------
On branch master - Wed 12 Apr 2017 15:18:51 PDT by matrinox <[email protected]>
  • Loading branch information
matrinox committed Apr 18, 2017
1 parent 551bb9c commit efbbad8
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 15 deletions.
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) }.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) }
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)
end

def pre_ruby19_syntax(key, value, width)
align(key, width) << colorize(' => ', :hash) << inspector.awesome(value)
align(key, width) << colorize(' => ', :hash) << value.ai(@options)
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)
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)
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/awesome_print/inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def printable(object)
# keys.
#---------------------------------------------------------------------------
def merge_options!(options = {})
options = options.dup
@options[:color].merge!(options.delete(:color) || {})
@options.merge!(options)
end
Expand Down

0 comments on commit efbbad8

Please sign in to comment.