Class: SimpleConsole::Filter
- Inherits:
-
Object
- Object
- SimpleConsole::Filter
- Defined in:
- lib/filter.rb,
lib/init.rb,
lib/simpleconsole.rb
Overview
This class keeps track of filters - much like Rails filters for ActionController.
Sample Usage
before_filter = Filter.new
before_filter.add_filter :first
before_filter.add_filter :second, :only => [:use_me]
before_filter.add_filter :third, :except => [:use_me]
before_filter.filter_for(:use_me)
# => Returns [:first, :second]
Instance Method Summary collapse
-
#add_filter(method_to_call, options = nil) ⇒ Object
Adds a method call to the current filter.
-
#filter_for(method_name) ⇒ Object
Returns a list of the methods in the filter for method_name.
-
#initialize ⇒ Filter
constructor
Initializes the filter.
Constructor Details
#initialize ⇒ Filter
Initializes the filter
16 17 18 |
# File 'lib/filter.rb', line 16 def initialize @filter_chain = Hash.new end |
Instance Method Details
#add_filter(method_to_call, options = nil) ⇒ Object
Adds a method call to the current filter. Options include:
-
:except => Array
-
:only => Array
Sample Usage
chain = Filter.new
chain.add_filter(:first)
chain.add_filter(:second, :only => [:use_me])
chain.add_filter(:third, :except => [:dont_use_me])
# => Adds :first to all, :second only to :use_me, and :third to all except :dont_use_me
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/filter.rb', line 31 def add_filter(method_to_call, = nil) if == nil @filter_chain[method_to_call.to_sym] = true elsif .has_key?(:only) @filter_chain[method_to_call.to_sym] = Hash.new @filter_chain[method_to_call.to_sym][:only] = [:only] elsif .has_key?(:except) @filter_chain[method_to_call.to_sym] = Hash.new @filter_chain[method_to_call.to_sym][:except] = [:except] end end |
#filter_for(method_name) ⇒ Object
Returns a list of the methods in the filter for method_name.
Example Usage
chain = Filter.new
chain.add_filter(:first, :second)
chain.add_filter(:third, :fourth, :only => [:call_me])
chain.add_filter(:fifth, :sixth, :except => [:call_me])
filter_for(:call_me)
# => [:first, :second, :third, :fourth, :fifth]
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/filter.rb', line 54 def filter_for(method_name) methods = Array.new @filter_chain.each do |method_to_call, option| if option == true methods << method_to_call elsif option.has_key?(:only) && option[:only].include?(method_name.to_sym) methods << method_to_call elsif option.has_key?(:except) && !option[:except].include?(method_name.to_sym) methods << method_to_call end end return methods.uniq end |