Sha256: 4a4cf9a6cdd0367df12533ecb4236c68b5a58fc405710718e89c6629919ef26d

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

class Module
  #  Adds context-dependent behaviour to instances. 
  #
  #    class Klass
  #      def name
  #        "Klass"
  #      end
  #
  #      in_layer :hello do
  #        def name
  #          "Hello from #{super}.\n"
  #        end
  #      end
  #    end
  #    
  #    k = Klass.new
  #    k.name                    #=> "Klass.\n"
  #    ContextR::with_layer :hello do
  #      k.name                  #=> "Hello from Klass.\n"
  #    end
  #    k.name                    #=> "Klass.\n"
  #
  # Note: in_layer automatically generates the inner module
  # and attaches it to the given layer. It is guaranteed, that the inner module
  # used for method definitons will always be the same for any layer x class
  # combination.
  def in_layer(layer_symbol, &block)
    extension = ContextR::stored_module_definitions[layer_symbol][self]

    return_value = extension.module_eval(&block) if block_given?

    ContextR::layer_by_symbol(layer_symbol).add_method_collection(self, 
                                                                  extension) 

    block_given? ? return_value : extension
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
contextr-1.0.3 lib/contextr/core_ext/module.rb
contextr-1.0.0 lib/contextr/core_ext/module.rb
contextr-1.0.1 lib/contextr/core_ext/module.rb
contextr-1.0.2 lib/contextr/core_ext/module.rb