Class: DuckMap::FilterStack
- Inherits:
-
Object
- Object
- DuckMap::FilterStack
- Defined in:
- lib/duck_map/filter_stack.rb
Constant Summary collapse
- DEFAULT_FILTER =
{exclude: {actions: [], controllers: [], names: [], verbs: [:delete, :post, :put]}, include: {actions: [:index, :show], controllers: [], names: [], verbs: []}}
Instance Method Summary collapse
-
#clear_filter(section, key) ⇒ Nil
Clears a single type of filter.
-
#clear_filters ⇒ Nil
Clears all types (:actions, :verbs, :names, :controllers) for the #current_filter.
-
#copy_filter(filter) ⇒ Hash
Copies a filter.
-
#current_filter ⇒ Hash
Returns the current filter.
-
#current_filter=(value) ⇒ NilClass
Sets the entire Hash for the #current_filter.
-
#exclude_filter(key, value) ⇒ NilClass
Removes value(s) from the #current_filter.
-
#include_filter(key, value) ⇒ NilClass
Adds a value(s) to the #current_filter.
-
#initialize ⇒ FilterStack
constructor
A new instance of FilterStack.
-
#pop ⇒ Hash
Pops the last item from the stack.
-
#push ⇒ Hash
Pushes a copy of the current filter onto the end of the stack.
-
#reset ⇒ NilClass
Resets the stack.
-
#stack ⇒ Hash
A Hash containing all of the values for exlude sitemap_filters.
-
#stack=(value) ⇒ Nil
Sets the entire Hash for exclude sitemap_filters.
-
#update_filter(action, key, value) ⇒ NilClass
Adds or removes value(s) to or from the #current_filter.
Constructor Details
#initialize ⇒ FilterStack
Returns a new instance of FilterStack.
12 13 14 15 |
# File 'lib/duck_map/filter_stack.rb', line 12 def initialize super self.reset end |
Instance Method Details
#clear_filter(section, key) ⇒ Nil
Clears a single type of filter.
100 101 102 103 104 |
# File 'lib/duck_map/filter_stack.rb', line 100 def clear_filter(section, key) key = key.kind_of?(Symbol) ? key : key.to_sym self.current_filter[section][key] = [] return nil end |
#clear_filters ⇒ Nil
Clears all types (:actions, :verbs, :names, :controllers) for the #current_filter.
89 90 91 92 93 |
# File 'lib/duck_map/filter_stack.rb', line 89 def clear_filters self.current_filter = {exclude: {actions: [], verbs: [], names: [], controllers: []}, include: {actions: [], verbs: [], names: [], controllers: []}} return nil end |
#copy_filter(filter) ⇒ Hash
Copies a filter
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/duck_map/filter_stack.rb', line 41 def copy_filter(filter) buffer = {exclude: {}, include: {}} filter[:exclude].each do |part| buffer[:exclude][part[0]] = part[1].dup end filter[:include].each do |part| buffer[:include][part[0]] = part[1].dup end return buffer end |
#current_filter ⇒ Hash
Returns the current filter. The current filter is ALWAYS the last item in the stack.
73 74 75 |
# File 'lib/duck_map/filter_stack.rb', line 73 def current_filter return self.stack.last end |
#current_filter=(value) ⇒ NilClass
Sets the entire Hash for the #current_filter.
80 81 82 83 84 |
# File 'lib/duck_map/filter_stack.rb', line 80 def current_filter=(value) self.stack.pop self.stack.push(value) return nil end |
#exclude_filter(key, value) ⇒ NilClass
Removes value(s) from the #current_filter. Basically, the opposite of #include_filter.
112 113 114 115 |
# File 'lib/duck_map/filter_stack.rb', line 112 def exclude_filter(*args) args.insert(0, :exclude) return update_filter(*args) end |
#include_filter(key, value) ⇒ NilClass
Adds a value(s) to the #current_filter. The filter stack is implemented as an Array of Hashes. The #current_filter will always be a Hash of key/value pairs. Each key represents a type of filter:
The filter types are:
-
:actions
-
:verbs
-
:names
-
controllers
Each key has an associated Array of Strings or Symbols. The default filter is:
{actions: [:index, :show], verbs: [:get], names: [], controllers: []}
The default is to include all routes that have an :action of :index or :show or include routes that have a verb of :get. Basically, including urls that a search engine would crawl. The Array of values added to the filter can be mixed. However, the convention is to use Symbols for :actions and :verbs and use Strings for :names and :controllers. Values are automagically converted to Symbols if the key is :actions or :verbs.
include_filter(:actions, :edit) # => {actions: [:index, :show, :edit], verbs: [:get], names: [], controllers: []}
include_filter(:actions, [:edit, :update]) # => {actions: [:index, :show, :edit, :update], verbs: [:get], names: [], controllers: []}
include_filter(:actions, "edit") # => {actions: [:index, :show, :edit], verbs: [:get], names: [], controllers: []}
include_filter(:actions, ["edit", "update"]) # => {actions: [:index, :show, :edit], verbs: [:get], names: [], controllers: []}
include_filter(:actions, [:edit, "update"]) # => {actions: [:index, :show, :edit], verbs: [:get], names: [], controllers: []}
include_filter(:verbs, :post) # => {actions: [:index, :show], verbs: [:get, :post], names: [], controllers: []}
148 149 150 151 |
# File 'lib/duck_map/filter_stack.rb', line 148 def include_filter(*args) args.insert(0, :include) return update_filter(*args) end |
#pop ⇒ Hash
Pops the last item from the stack. However, the list can never be empty, so, it pops the last item ONLY if the stack has more than ONE item.
66 67 68 |
# File 'lib/duck_map/filter_stack.rb', line 66 def pop return self.stack.length > 1 ? self.stack.pop : self.current_filter end |
#push ⇒ Hash
Pushes a copy of the current filter onto the end of the stack.
58 59 60 |
# File 'lib/duck_map/filter_stack.rb', line 58 def push self.stack.push(copy_filter(self.current_filter)) end |
#reset ⇒ NilClass
Resets the stack.
34 35 36 |
# File 'lib/duck_map/filter_stack.rb', line 34 def reset self.stack = [copy_filter(DEFAULT_FILTER)] end |
#stack ⇒ Hash
A Hash containing all of the values for exlude sitemap_filters.
20 21 22 |
# File 'lib/duck_map/filter_stack.rb', line 20 def stack return @stack ||= [] end |
#stack=(value) ⇒ Nil
Sets the entire Hash for exclude sitemap_filters.
27 28 29 |
# File 'lib/duck_map/filter_stack.rb', line 27 def stack=(value) @stack = value end |
#update_filter(action, key, value) ⇒ NilClass
Adds or removes value(s) to or from the #current_filter. This method is called by #include_filter and #exclude_filter.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/duck_map/filter_stack.rb', line 160 def update_filter(*args) action = args.shift action = action.kind_of?(Symbol) ? action : action.to_sym key = args.shift key = key.kind_of?(Symbol) ? key : key.to_sym # list will always be concatenated to the target array list = [] # build the list array depending on what the user passed to the method args.each do |item| if item.kind_of?(Array) && item.any? list.concat(item) else list.push(item) end end # convert all of the items in the list to symbols # for :actions or :verbs if key.eql?(:actions) || key.eql?(:verbs) list.each_with_index do |item, index| unless item.kind_of?(Symbol) list[index] = item.to_sym end end end self.current_filter[action][key].concat(list) self.current_filter[action][key].uniq! opposite_action = action.eql?(:exclude) ? :include : :exclude self.current_filter[action][key].each do |value| #puts "action: #{action} key: #{key} value: #{value}" self.current_filter[opposite_action][key].delete(value) end #if action == :include ## now, simply concatenate the resulting list and make sure the final array is unique #self.current_filter[key].concat(list) #self.current_filter[key].uniq! #elsif action == :exclude #self.current_filter[key].reject! {|item| list.include?(item)} #end return nil end |