Module: ContextR::PublicApi
- Included in:
- ContextR
- Defined in:
- lib/contextr/public_api.rb
Instance Method Summary collapse
-
#active_layers ⇒ Object
returns all currently active layers in their activation order.
-
#layer(name, &block) ⇒ Object
returns the layer, defined by the given name.
-
#layers ⇒ Object
returns all layers that where defined, but are not neccessarily active.
-
#with_layers(*layer_symbols, &block) ⇒ Object
(also: #with_layer)
allows the explicit activation of layers within a block context.
-
#without_layers(*layer_symbols, &block) ⇒ Object
(also: #without_layer)
allows the explicit deactivation of layers within a block context.
Instance Method Details
#active_layers ⇒ Object
returns all currently active layers in their activation order
60 61 62 |
# File 'lib/contextr/public_api.rb', line 60 def active_layers active_layers_as_classes.collect { |layer| symbol_by_layer(layer) } end |
#layer(name, &block) ⇒ Object
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("Logging active")
end
def deactivated
logger.log("Logging inactive")
end
end
# will call activated before executing the block
# and deactivated afterwards
ContextR::with_layer(:log) do
1 + 1
end
91 92 93 94 95 96 97 |
# File 'lib/contextr/public_api.rb', line 91 def layer(name, &block) if block_given? layer_by_symbol(name).instance_eval(&block) else layer_by_symbol(name) end end |
#layers ⇒ Object
returns all layers that where defined, but are not neccessarily active
65 66 67 |
# File 'lib/contextr/public_api.rb', line 65 def layers layers_as_classes.collect { |layer| symbol_by_layer(layer) } end |
#with_layers(*layer_symbols, &block) ⇒ Object Also known as: with_layer
allows the explicit activation of layers within a block context
ContextR::with_layers(:foo, :bar) do
ContextR::active_layers # => [:default, :foo, :bar]
ContextR::with_layers(:baz) do
ContextR::active_layers # => [:default, :foo, :bar, :baz]
end
end
:call-seq:
with_layers(layer_name, ...) { ... }
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/contextr/public_api.rb', line 17 def with_layers(*layer_symbols, &block) layers = layer_symbols.collect do |layer_symbol| layer_by_symbol(layer_symbol) end.reverse 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 |
#without_layers(*layer_symbols, &block) ⇒ Object Also known as: without_layer
allows the explicit deactivation of layers within a block context
ContextR::with_layers(:foo, :bar) do
ContextR::active_layers # => [:default, :foo, :bar]
ContextR::without_layers(:foo) do
ContextR::active_layers # => [:default, :bar]
end
end
:call-seq:
without_layers(layer_name, ...) { ... }
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/contextr/public_api.rb', line 45 def without_layers(*layer_symbols, &block) layers = layer_symbols.collect do |layer_symbol| layer_by_symbol(layer_symbol) end layers_being_deactivated = layers & 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 |