lib/much-plugin.rb in much-plugin-0.2.0 vs lib/much-plugin.rb in much-plugin-0.2.1

- old
+ new

@@ -1,25 +1,33 @@ require "much-plugin/version" module MuchPlugin - def self.included(receiver) receiver.class_eval{ extend ClassMethods } end module ClassMethods - - # install an included hook that first checks if this plugin's receiver mixin + # install an included block that first checks if this plugin's receiver mixin # has already been included. If it has not been, include the receiver mixin - # and run all of the `plugin_included` hooks + # and run all of the `plugin_included` blocks def included(plugin_receiver) return if plugin_receiver.include?(self.much_plugin_included_detector) plugin_receiver.send(:include, self.much_plugin_included_detector) - self.much_plugin_included_hooks.each do |hook| - plugin_receiver.class_eval(&hook) + self.much_plugin_included_blocks.each do |block| + plugin_receiver.class_eval(&block) end + + self.much_plugin_class_method_blocks.each do |block| + self.much_plugin_class_methods_module.class_eval(&block) + end + plugin_receiver.send(:extend, self.much_plugin_class_methods_module) + + self.much_plugin_instance_method_blocks.each do |block| + self.much_plugin_instance_methods_module.class_eval(&block) + end + plugin_receiver.send(:include, self.much_plugin_instance_methods_module) end # the included detector is an empty module that is only used to detect if # the plugin has been included or not, it doesn't add any behavior or # methods to the object receiving the plugin; we use `const_set` to name the @@ -29,14 +37,42 @@ @much_plugin_included_detector ||= Module.new.tap do |m| self.const_set("MuchPluginIncludedDetector", m) end end - def much_plugin_included_hooks; @much_plugin_included_hooks ||= []; end + def much_plugin_class_methods_module + @much_plugin_class_methods_module ||= Module.new.tap do |m| + self.const_set("MuchPluginClassMethods", m) + end + end - def plugin_included(&hook) - self.much_plugin_included_hooks << hook + def much_plugin_instance_methods_module + @much_plugin_instance_methods_module ||= Module.new.tap do |m| + self.const_set("MuchPluginInstanceMethods", m) + end end - end + def much_plugin_included_blocks + @much_plugin_included_blocks ||= [] + end + def much_plugin_class_method_blocks + @much_plugin_class_method_blocks ||= [] + end + + def much_plugin_instance_method_blocks + @much_plugin_instance_method_blocks ||= [] + end + + def plugin_included(&block) + self.much_plugin_included_blocks << block + end + + def plugin_class_methods(&block) + self.much_plugin_class_method_blocks << block + end + + def plugin_instance_methods(&block) + self.much_plugin_instance_method_blocks << block + end + end end