parent
8bf8dac886
commit
e95ec569d1
@ -1,28 +1,50 @@ |
|||||||
module Report::InheritedNamespace |
module Report::InheritedNamespace |
||||||
|
NESTED_NAMESPACES = %w[Validation Filter GroupBy Result] |
||||||
|
|
||||||
|
|
||||||
def self.activate |
def self.activate |
||||||
[Report, Report::Validation, Report::Filter, Report::GroupBy, Report::Result].each do |klass| |
Report.extend self |
||||||
extend_object klass |
NESTED_NAMESPACES.each { |n| n.extend self } |
||||||
end |
|
||||||
end |
end |
||||||
|
|
||||||
def const_missing(name, *) |
def const_missing(name, *) |
||||||
|
puts "const_missing called on #{self.name}" |
||||||
super |
super |
||||||
rescue NameError => error |
rescue NameError => error |
||||||
raise error unless respond_to? :superclass and superclass != self |
raise error unless respond_to? :superclass and superclass != self |
||||||
begin |
load_constant name, error |
||||||
zuper = superclass.const_get(name) |
|
||||||
case zuper |
|
||||||
when Class then const_set name, Class.new(zuper).extend(Report::InheritedNamespace) |
|
||||||
when Module then const_set name, Module.new { include zuper }.extend(Report::InheritedNamespace) |
|
||||||
else const_set name, zuper |
|
||||||
end |
|
||||||
rescue NameError |
|
||||||
raise error |
|
||||||
end |
|
||||||
end |
end |
||||||
|
|
||||||
def inherited(klass) |
def inherited(klass) |
||||||
super |
super |
||||||
|
propagate klass |
||||||
|
end |
||||||
|
|
||||||
|
def included(klass) |
||||||
|
super |
||||||
|
propagate klass |
||||||
|
end |
||||||
|
|
||||||
|
def propagate(klass) |
||||||
klass.extend Report::InheritedNamespace |
klass.extend Report::InheritedNamespace |
||||||
|
NESTED_NAMESPACES.each do |name| |
||||||
|
if file = ActiveSupport::Dependencies.search_for_file("#{klass.name}::#{name}".underscore) |
||||||
|
require_or_load file |
||||||
|
else |
||||||
|
const_missing name |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def load_constant(name, error = nil) |
||||||
|
puts "#{self.name} #{name}" |
||||||
|
zuper = superclass.const_get(name) |
||||||
|
case zuper |
||||||
|
when Class then const_set name, Class.new(zuper).extend(Report::InheritedNamespace) |
||||||
|
when Module then const_set name, Module.new { include zuper }.extend(Report::InheritedNamespace) |
||||||
|
else const_set name, zuper |
||||||
|
end |
||||||
|
rescue NameError, ArgumentError |
||||||
|
raise error |
||||||
end |
end |
||||||
end |
end |
||||||
|
Loading…
Reference in new issue