1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

Refactored AwesomePrint::Plugin

This commit is contained in:
Michael Dvorkin 2013-11-05 14:34:16 -08:00
parent e843081fed
commit 8130cf4043
3 changed files with 32 additions and 15 deletions

View file

@ -33,4 +33,5 @@ unless defined?(AwesomePrint::Inspector)
require File.dirname(__FILE__) + "/awesome_print/ext/no_brainer" if defined?(NoBrainer) require File.dirname(__FILE__) + "/awesome_print/ext/no_brainer" if defined?(NoBrainer)
require File.dirname(__FILE__) + "/awesome_print/ext/ripple" if defined?(Ripple) require File.dirname(__FILE__) + "/awesome_print/ext/ripple" if defined?(Ripple)
require File.dirname(__FILE__) + "/awesome_print/ext/sequel" if defined?(Sequel) require File.dirname(__FILE__) + "/awesome_print/ext/sequel" if defined?(Sequel)
# require File.dirname(__FILE__) + "/awesome_print/ext/example"
end end

View file

@ -10,7 +10,7 @@ module AwesomePrint
def awesome_radix(object) def awesome_radix(object)
# puts "example#awesome_radix(#{object.inspect})" # puts "example#awesome_radix(#{object.inspect})"
"#{object} (dec) 0#{object.to_s(8)} (oct) 0x#{object.to_s(16).upcase}" "#{object} (dec) 0#{object.to_s(8)} (oct) 0x#{object.to_s(16).upcase} (hex)"
end end
end end
end end

View file

@ -19,37 +19,53 @@ module AwesomePrint
end end
def register(mod) def register(mod)
puts "mod.instance_methods: #{mod.instance_methods}" #
# Make sure calling AwesomePrint::Plugin.register twice for the same
# plugin doesn't do any harm.
#
return nil if @list.include?(mod)
#
# The plugin *must* implement :cast(object, type) method.
#
unless mod.instance_methods.include?(:cast) unless mod.instance_methods.include?(:cast)
raise RuntimeError, "#{mod} plugin should define cast(object, type) instance method" raise RuntimeError, "#{mod} plugin should define cast(object, type) instance method"
end end
#
formatter = AwesomePrint::Formatter # Create the hook name from the plugin's module name, for example:
# ActiveRecord => "active_record". Once we have the hook name rename
# generic :cast method to unique :cast_<hook_name>.
#
hook = mod.name.gsub(/^.*::/, "").gsub(/(.)([A-Z])/, '\1_\2').downcase hook = mod.name.gsub(/^.*::/, "").gsub(/(.)([A-Z])/, '\1_\2').downcase
mod.send(:alias_method, :"cast_#{hook}", :cast) mod.send(:alias_method, :"cast_#{hook}", :cast)
mod.send(:remove_method, :cast) mod.send(:remove_method, :cast)
#
@list << mod # Add plugin's instance methods to AwesomePrint::Formatter, then hook
# formatter's :cast method.
#
formatter = AwesomePrint::Formatter
formatter.send(:include, mod) formatter.send(:include, mod)
#
puts "defining cast_with_#{hook}..." # The method chaining is done as follows:
#
# formatter.send(:alias_method, :"cast_without_#{hook}", :cast) # 1. Original :cast method becomes :cast_without_<hook_name>.
# 2. New :cast_with_<hook_name> method gets created dynamically. It calls
# plugin's :cast (renamed :cast_<hook_name> above) and if the return
# value is nil (i.e. no cast) it calls the original :cast from step 1.
# 3. Calling :cast now invokes :cast_with_<hook_name> from step 2.
#
chain_methods(formatter, hook) do chain_methods(formatter, hook) do
formatter.send(:define_method, :"cast_with_#{hook}") do |object, type| formatter.send(:define_method, :"cast_with_#{hook}") do |object, type|
puts "cast_with_#{hook}(#{object.inspect}, #{type.inspect})" send(:"cast_#{hook}", object, type) || send(:"cast_without_#{hook}", object, type)
cast = send(:"cast_#{hook}", object, type) || send(:"cast_without_#{hook}", object, type)
end end
end end
# formatter.send(:alias_method, :cast, :"cast_with_#{hook}") @list << mod
end end
private private
def chain_methods(formatter, hook) def chain_methods(formatter, hook)
formatter.send(:alias_method, :"cast_without_#{hook}", :cast) formatter.send(:alias_method, :"cast_without_#{hook}", :cast)
yield yield # <-- Define :"cast_with_#{hook}".
formatter.send(:alias_method, :cast, :"cast_with_#{hook}") formatter.send(:alias_method, :cast, :"cast_with_#{hook}")
end end
end end