Class: ContextFilters::GlobalContext
- Inherits:
-
Object
- Object
- ContextFilters::GlobalContext
- Defined in:
- lib/context-filters/global_context.rb
Overview
builds list of filters and provides dsl for building nested context and allows evaluating filters on methods in the current context
Direct Known Subclasses
Instance Attribute Summary collapse
-
#context ⇒ Array
readonly
private
The context stack.
-
#priority_filters ⇒ PriorityFilters
readonly
private
Shared list of filters.
Instance Method Summary collapse
-
#evaluate_filters(target, method) { ... } ⇒ Object
evaluates all matching filters for given context, allows to do extra work for
priority.nil?or on the end of the priorities,. -
#filter(priority, options = nil, &block) ⇒ Object
defines new filter for given
priorityandoptions. -
#in_context(options, &block) {|GlobalContext| ... } ⇒ Object
starts new context.
-
#initialize(priority_filters = nil, context = [], options = nil) ⇒ GlobalContext
constructor
initialize new GlobalContext, works in two modes: 1.
Constructor Details
#initialize(priority_filters = nil, context = [], options = nil) ⇒ GlobalContext
initialize new GlobalContext, works in two modes:
-
start totally new context, takes one param - array of priorities,
nilto use one anonymous priority -
build sub context, params: global_filters_list, parents_context, value to add to context
32 33 34 35 36 37 38 |
# File 'lib/context-filters/global_context.rb', line 32 def initialize(priority_filters = nil, context = [], = nil) if ContextFilters::PriorityFilters === priority_filters then @priority_filters = priority_filters else @priority_filters = ContextFilters::PriorityFilters.new(priority_filters) end @context = context.dup + [] end |
Instance Attribute Details
#context ⇒ Array (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the context stack.
15 16 17 |
# File 'lib/context-filters/global_context.rb', line 15 def context @context end |
#priority_filters ⇒ PriorityFilters (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns shared list of filters.
19 20 21 |
# File 'lib/context-filters/global_context.rb', line 19 def priority_filters @priority_filters end |
Instance Method Details
#evaluate_filters(target, method) { ... } ⇒ Object
evaluates all matching filters for given context, allows to do extra work for priority.nil? or on the end of the priorities,
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/context-filters/global_context.rb', line 62 def evaluate_filters(target, method) local_called = false @priority_filters.each do |priority, filters| @context.each { || filters.apply(target, method, ) } if priority.nil? && block_given? && !local_called yield local_called = true end end yield if block_given? && !local_called end |
#filter(priority, options = nil, &block) ⇒ Object
defines new filter for given priority and options
46 47 48 |
# File 'lib/context-filters/global_context.rb', line 46 def filter(priority, = nil, &block) @priority_filters.store(priority, , &block) end |
#in_context(options, &block) {|GlobalContext| ... } ⇒ Object
starts new context
54 55 56 |
# File 'lib/context-filters/global_context.rb', line 54 def in_context(, &block) self.class.new(@priority_filters, @context, ).tap(&block) end |