Class: DuckMap::FilterStack

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeFilterStack

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.

Parameters:

  • section (Symbol)

    The section of filter to update. :exclude or :include.

  • key (Symbol)

    The key of filter to update. :actions, :verbs, :names, :controllers.

Returns:

  • (Nil)


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_filtersNil

Clears all types (:actions, :verbs, :names, :controllers) for the #current_filter.

Returns:

  • (Nil)


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

Returns:

  • (Hash)


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_filterHash

Returns the current filter. The current filter is ALWAYS the last item in the stack.

Returns:

  • (Hash)


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.

Returns:

  • (NilClass)


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.

Parameters:

  • key (Symbol, String)

    The type of filter to update. :actions, :verbs, :names, :controllers.

  • value (String, Symbol, Array)

    A single or Array of items to be added to the filter section specified via key.

Returns:

  • (NilClass)


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: []}

Parameters:

  • key (Symbol, String)

    The type of filter to update. :actions, :verbs, :names, :controllers.

  • value (String, Symbol, Array)

    A single or Array of items to be added to the filter section specified via key.

Returns:

  • (NilClass)


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

#popHash

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.

Returns:

  • (Hash)


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

#pushHash

Pushes a copy of the current filter onto the end of the stack.

Returns:

  • (Hash)


58
59
60
# File 'lib/duck_map/filter_stack.rb', line 58

def push
  self.stack.push(copy_filter(self.current_filter))
end

#resetNilClass

Resets the stack.

Returns:

  • (NilClass)


34
35
36
# File 'lib/duck_map/filter_stack.rb', line 34

def reset
  self.stack = [copy_filter(DEFAULT_FILTER)]
end

#stackHash

A Hash containing all of the values for exlude sitemap_filters.

Returns:

  • (Hash)


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.

Returns:

  • (Nil)


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.

Parameters:

  • action (Symbol, String)

    The action to perform: :include or :exclude.

  • key (Symbol, String)

    The type of filter to update. :actions, :verbs, :names, :controllers.

  • value (String, Symbol, Array)

    A single or Array of items to be added to the filter section specified via key.

Returns:

  • (NilClass)


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