Class: ContextFilters::Filters
- Inherits:
-
Object
- Object
- ContextFilters::Filters
- Defined in:
- lib/context-filters/filters.rb
Overview
Store and apply filters using blocks
Instance Method Summary collapse
-
#apply(target, method, options = nil) ⇒ Object
applies matching filters to the given target method, also uses filters matching extra :target => target when options is a Hash.
-
#empty? ⇒ Boolean
True if there are any rules stored, false otherwise.
-
#filters ⇒ Object
Array of already defined filters.
-
#initialize ⇒ Filters
constructor
initialize the filters storage.
-
#select_filters(target, options) ⇒ Object
Select matching filters and filters including targets when options is a
Hash. -
#store(options = nil, &block) ⇒ Object
stores the block for given options, if the options have a block already the new one is added to the list.
Constructor Details
#initialize ⇒ Filters
initialize the filters storage
36 37 38 |
# File 'lib/context-filters/filters.rb', line 36 def initialize @filters = {} end |
Instance Method Details
#apply(target, method, options = nil) ⇒ Object
applies matching filters to the given target method, also uses filters matching extra :target => target when options is a Hash
56 57 58 |
# File 'lib/context-filters/filters.rb', line 56 def apply(target, method, = nil) select_filters(target, ).each{|block| target.send(method, &block) } end |
#empty? ⇒ Boolean
Returns true if there are any rules stored, false otherwise.
78 79 80 |
# File 'lib/context-filters/filters.rb', line 78 def empty? @filters.empty? end |
#filters ⇒ Object
Array of already defined filters
73 74 75 |
# File 'lib/context-filters/filters.rb', line 73 def filters @filters.keys end |
#select_filters(target, options) ⇒ Object
Select matching filters and filters including targets when options is a Hash
64 65 66 67 68 69 70 |
# File 'lib/context-filters/filters.rb', line 64 def select_filters(target, ) found = @filters.fetch(, []) if Hash === then found += @filters.fetch(.merge(:target => target), []) end found end |
#store(options = nil, &block) ⇒ Object
stores the block for given options, if the options have a block already the new one is added to the list
45 46 47 48 |
# File 'lib/context-filters/filters.rb', line 45 def store( = nil, &block) @filters[] ||= [] @filters[] << block end |