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

- old
+ new

@@ -1,7 +1,9 @@ -module ContextR - module ClassMethods +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 @@ -12,12 +14,26 @@ @stored_core_methods ||= Hash.new do | hash, key | hash[key] = Hash.new end end + def active_layers_as_classes + Dynamic[:layers] + end + + 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 + } + end + def symbol_by_layer(lay) - lay.to_s.gsub( /^ContextR::(.*)Layer$/, '\1' ).underscore + lay.to_s.gsub( /^ContextR::(.*)Layer$/, '\1' ).underscore.to_sym end def layer_by_symbol(sym) "ContextR::#{sym.to_s.camelize}Layer".constantize end @@ -43,11 +59,11 @@ end def on_core_method_called(receiver, contextified_class, method_name, arguments, block) proxies = [] - layers.each do |layer| + active_layers_as_classes.each do |layer| proxies += layer.context_proxies(contextified_class, method_name) end.compact proxies << core_proxy(receiver, contextified_class, method_name) call_methods_stack(proxies.reverse, receiver, @@ -60,9 +76,12 @@ end def observe_core_method(klass, method_name, version) only_once do klass.class_eval(%Q{ + if self.instance_methods.include?("#{method_name}") + undef_method("#{method_name}") + end def #{method_name}(*arguments, &block) ContextR::on_core_method_called( self, ObjectSpace._id2ref(#{klass.object_id}), :#{method_name},