Module: SubclassableCallbacks
- Included in:
- Ubiquitously::Base
- Defined in:
- lib/ubiquitously/extensions/callbacks.rb
Overview
use this if you want to create before and after filters around a commonly used method, but still want to define the method in subclasses.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
- .included(base) ⇒ Object
-
.override(*classes) ⇒ Object
ObjectSpace.each_object(Class).to_a for all of them.
Class Method Details
.included(base) ⇒ Object
7 8 9 |
# File 'lib/ubiquitously/extensions/callbacks.rb', line 7 def self.included(base) base.extend ClassMethods end |
.override(*classes) ⇒ Object
ObjectSpace.each_object(Class).to_a for all of them
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ubiquitously/extensions/callbacks.rb', line 38 def self.override(*classes) classes = ObjectSpace.each_object(Class).to_a if classes.blank? classes.flatten.each do |object| if object.ancestors.include?(SubclassableCallbacks) object.class_eval do methods = object.subclassable_callbacks last_index = object.superclass.ancestors.index(SubclassableCallbacks) if last_index && last_index > 0 superclasses = object.superclass.ancestors[0..last_index - 1] superclass_methods = superclasses.select do |clazz| clazz.respond_to?(:subclassable_callbacks) end.map(&:subclassable_callbacks) methods = methods.concat(superclass_methods).flatten.uniq end methods.each do |name| next unless object.overrides?(name) alias_method "subclass_#{name}", "#{name}" alias_method "implementation_#{name}", "subclass_#{name}" alias_method "#{name}", "superclass_#{name}" end end end end end |