lib/contextr/class_methods.rb in contextr-0.1.1 vs lib/contextr/class_methods.rb in contextr-0.1.9

- old
+ new

@@ -1,20 +1,20 @@ module ContextR # :nodoc: module ClassMethods # :nodoc: include MutexCode - def const_missing(const_name) - if const_name.to_s =~ /.*Layer$/ - self.const_set(const_name, Class.new(ContextR::Layer)) - else - super + def stored_core_methods + @stored_core_methods ||= Hash.new do |hash, key| + hash[key] = Hash.new end end - def stored_core_methods - @stored_core_methods ||= Hash.new do | hash, key | - hash[key] = Hash.new + def stored_module_definitions + @stored_module_definitions ||= Hash.new do |hash, key| + hash[key] = Hash.new do |hash, key| + hash[key] = Module.new + end end end def active_layers_as_classes Dynamic[:layers] @@ -23,28 +23,26 @@ def layered_do(layers, block) Dynamic.let({:layers => layers}, &block) end def layers_as_classes - constants.select { |l| l =~ /.+Layer$/ }.collect { |l| - l.scan(/(.+)Layer/).first.first.underscore.to_sym - } + @layers.values end def symbol_by_layer(lay) - lay.to_s.gsub( /^ContextR::(.*)Layer$/, '\1' ).underscore.to_sym + @layers.index(lay) end def layer_by_symbol(sym) - "ContextR::#{sym.to_s.camelize}Layer".constantize + @layers[sym] ||= ContextR::Layer.new end def call_methods_stack(stack, receiver, method_name, arguments, block) if stack.size == 1 stack.pop.call(*arguments, &block) else - stack.pop.send(method_name, *arguments) do | action, *rest_args | + stack.pop.__send__(method_name, *arguments) do | action, *rest_args | case action when :receiver receiver when :block block.call(*rest_args) @@ -60,11 +58,13 @@ def on_core_method_called(receiver, contextified_class, method_name, arguments, block) proxies = [] active_layers_as_classes.each do |layer| - proxies += layer.context_proxies(contextified_class, method_name) + proxies += layer.context_proxies(receiver, + contextified_class, + method_name) end.compact proxies << core_proxy(receiver, contextified_class, method_name) call_methods_stack(proxies.reverse, receiver, method_name, arguments, block) @@ -101,9 +101,13 @@ end end def meta_method?(method_name) method_name.to_s =~ /method_added(_with(out)?_contextr_listener)?/ + end + + def self.extended(base) + base.instance_variable_set(:@layers, {}) end end self.extend(ClassMethods) end