mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Better plugin registration
This commit is contained in:
parent
525845bafe
commit
765042f7f1
2 changed files with 43 additions and 2 deletions
|
@ -14,3 +14,5 @@ module AwesomePrint
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
AwesomePrint::Plugin.register(AwesomePrint::Radix)
|
||||
|
|
|
@ -4,8 +4,12 @@
|
|||
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
||||
#------------------------------------------------------------------------------
|
||||
module AwesomePrint
|
||||
class Plugin
|
||||
def self.add(mod)
|
||||
module Plugin
|
||||
extend self
|
||||
|
||||
@list = []; attr_reader :list
|
||||
|
||||
def add(mod)
|
||||
formatter = AwesomePrint::Formatter
|
||||
formatter.send(:include, mod)
|
||||
|
||||
|
@ -13,5 +17,40 @@ module AwesomePrint
|
|||
formatter.send(:alias_method, :"cast_without_#{hook}", :cast)
|
||||
formatter.send(:alias_method, :cast, :"cast_with_#{hook}")
|
||||
end
|
||||
|
||||
def register(mod)
|
||||
puts "mod.instance_methods: #{mod.instance_methods}"
|
||||
unless mod.instance_methods.include?(:cast)
|
||||
raise RuntimeError, "#{mod} plugin should define cast(object, type) instance method"
|
||||
end
|
||||
|
||||
formatter = AwesomePrint::Formatter
|
||||
hook = mod.name.gsub(/^.*::/, "").gsub(/(.)([A-Z])/, '\1_\2').downcase
|
||||
|
||||
mod.send(:alias_method, :"cast_#{hook}", :cast)
|
||||
mod.send(:remove_method, :cast)
|
||||
|
||||
@list << mod
|
||||
formatter.send(:include, mod)
|
||||
|
||||
puts "defining cast_with_#{hook}..."
|
||||
|
||||
# formatter.send(:alias_method, :"cast_without_#{hook}", :cast)
|
||||
chain_methods(formatter, hook) do
|
||||
formatter.send(:define_method, :"cast_with_#{hook}") do |object, type|
|
||||
puts "cast_with_#{hook}(#{object.inspect}, #{type.inspect})"
|
||||
cast = send(:"cast_#{hook}", object, type) || send(:"cast_without_#{hook}", object, type)
|
||||
end
|
||||
end
|
||||
# formatter.send(:alias_method, :cast, :"cast_with_#{hook}")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def chain_methods(formatter, hook)
|
||||
formatter.send(:alias_method, :"cast_without_#{hook}", :cast)
|
||||
yield
|
||||
formatter.send(:alias_method, :cast, :"cast_with_#{hook}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue