Skip to content

Instantly share code, notes, and snippets.

@amardaxini
Created May 4, 2011 07:07
Show Gist options
  • Save amardaxini/954860 to your computer and use it in GitHub Desktop.
Save amardaxini/954860 to your computer and use it in GitHub Desktop.
Active support concern handling
# Test1
module M
def foo
puts "M"
end
end
module N
include M
def foo
puts "N"
super
end
end
include N
Object
=> Object
>> foo
N
M
module A
module Concern
def self.extended(base)
puts "inside extended #{base}"
base.instance_variable_set("@_dependencies", [])
end
def append_features(base)
puts "inside append #{base}"
if base.instance_variable_defined?("@_dependencies")
base.instance_variable_get("@_dependencies") << self
return false
else
return false if base < self
@_dependencies.each { |dep| base.send(:include, dep) }
super
base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
base.send :include, const_get("InstanceMethods") if const_defined?("InstanceMethods")
base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block")
end
end
def included(base = nil, &block)
puts "inside included #{base}"
if base.nil?
@_included_block = block
else
super
end
end
end
end
module Foo
extend A::Concern
puts "Begin Foo"
included do
class_eval do
def self.method_injected_by_foo
puts "Injected Method"
end
end
end
end
module Bar
extend A::Concern
include Foo
puts "Begin BAr"
included do
puts "Calleing Injected Method frombar"
self.method_injected_by_foo
end
end
class Host
include Bar # works, Bar takes care now of its dependencies
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment