<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -62,6 +62,10 @@ module ContextR
         ContextR::EventMachine::on_method_added(self, name)
         super
       end
+      def include(modul)
+        modul.instance_method.each { |m| method_added(m) }
+        super
+      end
     end
   end
 end</diff>
      <filename>lib/contextr/event_machine.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,5 +1,17 @@
 module ContextR # :nodoc:
-  class Layer # :nodoc: all
+  class Layer
+    def activated
+      nil
+    end
+    def deactivated
+      nil
+    end
+    def inspect
+      &quot;ContextR::layer(:#{ContextR::symbol_by_layer(self)})&quot;
+    end
+    alias_method :to_s, :inspect
+
+  # :nodoc: all
     def definitions
       @definitions ||= {}
     end
@@ -65,7 +77,7 @@ module ContextR # :nodoc:
                                     version)
     end
 
-    def  register_callbacks(cclass, mmodule)
+    def register_callbacks(cclass, mmodule)
       {:on_wrapper_method_added =&gt; mmodule,
        :on_class_method_added   =&gt; cclass }.each do | callback, klass |
        ContextR::EventMachine.register(self, callback, </diff>
      <filename>lib/contextr/layer.rb</filename>
    </modified>
    <modified>
      <diff>@@ -18,7 +18,13 @@ module ContextR
       layers = layer_symbols.collect do |layer_symbol|
         layer_by_symbol(layer_symbol)
       end.reverse
-      layered_do(layers | active_layers_as_classes, block)
+      layers_being_activated = layers - active_layers_as_classes
+      layers_being_activated.each { |l| l.activated }
+
+      return_value = layered_do(layers | active_layers_as_classes, block)
+
+      layers_being_activated.each { |l| l.deactivated }
+      return_value
     end
     alias with_layer with_layers
 
@@ -40,7 +46,13 @@ module ContextR
       layers = layer_symbols.collect do |layer_symbol|
         layer_by_symbol(layer_symbol)
       end
-      layered_do(active_layers_as_classes - layers, block)
+      layers_being_deactivated = layers &amp; active_layers_as_classes
+      layers_being_deactivated.each { |l| l.deactivated }
+
+      return_value = layered_do(active_layers_as_classes - layers, block)
+
+      layers_being_deactivated.each { |l| l.activated }
+      return_value
     end
     alias without_layer without_layers
 
@@ -54,6 +66,35 @@ module ContextR
       layers_as_classes.collect { |layer| symbol_by_layer(layer) }
     end
 
+    # returns the layer, defined by the given name. If passed a block, it will
+    # instance_eval it on the layer and return its value instead. The latter
+    # may be used to define the activated and deactived methods for a layer.
+    #
+    #   ContextR::layer(:log) do
+    #     def logger
+    #       @logger ||= Logger.new
+    #     end
+    #     def activated
+    #       logger.log(&quot;Logging active&quot;)
+    #     end
+    #     def deactivated
+    #       logger.log(&quot;Logging inactive&quot;)
+    #     end
+    #   end
+    #
+    #   # will call activated before executing the block
+    #   # and deactivated afterwards
+    #   ContextR::with_layer(:log) do
+    #     1 + 1
+    #   end
+    #
+    def layer(name, &amp;block)
+      if block_given?
+        layer_by_symbol(name).instance_eval(&amp;block)
+      else
+        layer_by_symbol(name)
+      end
+    end
   end
   self.extend(PublicApi)
 end</diff>
      <filename>lib/contextr/public_api.rb</filename>
    </modified>
    <modified>
      <diff>@@ -238,6 +238,57 @@ describe &quot;ContextR&quot; do
       ContextR::layers.sort_by{ |s| s.to_s }.should include(layer)
     end
   end
+
+  it &quot;should return a layer on #layer(:name)&quot; do
+    ContextR::layer(:log).should == ContextR::layer_by_symbol(:log)
+  end
+  it &quot;should execute the block given to #layer(:name)&quot; do
+    lambda do
+      ContextR::layer(:log) do
+        def moo
+          &quot;moo&quot;
+        end
+      end.should == nil 
+    end.should_not raise_error
+
+    ContextR::layer(:log).moo == &quot;moo&quot;
+  end
+
+  it &quot;should execute activated for all layers on activation&quot; do
+    ContextR::layer(:log) do
+      def activated 
+        raise &quot;activated&quot;
+      end
+    end
+
+    lambda do
+      ContextR::with_layer(:log) { 1 + 1 }
+    end.should raise_error(RuntimeError, &quot;activated&quot;)
+
+    ContextR::layer(:log) do
+      def activated 
+        nil 
+      end
+    end
+  end
+
+  it &quot;should execute deactivated for all layers on deactivation&quot; do
+    ContextR::layer(:log) do
+      def deactivated 
+        raise &quot;deactivated&quot;
+      end
+    end
+
+    lambda do
+      ContextR::with_layer(:log) { 1 + 1 }
+    end.should raise_error(RuntimeError, &quot;deactivated&quot;)
+
+    ContextR::layer(:log) do
+      def deactivated 
+        nil 
+      end
+    end
+  end
 end
 
 describe &quot;ContextR&quot; do
@@ -252,3 +303,10 @@ describe &quot;ContextR&quot; do
     end
   end
 end
+
+describe ContextR::Layer do
+  it &quot;should give a nice, self-referencing output on inspect and to_s&quot; do
+    eval(ContextR::layer(:log).inspect).should == ContextR::layer(:log)
+    eval(ContextR::layer(:log).to_s).should == ContextR::layer(:log)
+  end
+end</diff>
      <filename>spec/contextr_spec.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>b4056b0c946b99631c6b0aa4d15f795b7768e778</id>
    </parent>
  </parents>
  <author>
    <name>Gregor Schmidt</name>
    <email>ruby@schmidtwisser.de</email>
  </author>
  <url>http://github.com/schmidt/contextr/commit/6bc09e3ad695287da58e236a06b6cca35804843b</url>
  <id>6bc09e3ad695287da58e236a06b6cca35804843b</id>
  <committed-date>2008-03-13T06:15:54-07:00</committed-date>
  <authored-date>2008-03-13T06:15:54-07:00</authored-date>
  <message>Added api to add behavior to layers
Added meta hooks on layers for activation and deactivation
  * currently this does not include repetitive activation and deactivation
Added specs to test the new behavior
Added hook for include in event_machine</message>
  <tree>033edd60b36fe8c67e17c3cc68438c2da4cd31f4</tree>
  <committer>
    <name>Gregor Schmidt</name>
    <email>ruby@schmidtwisser.de</email>
  </committer>
</commit>
