Class: Stockboy::FilterChain

Inherits:
Hash
  • Object
show all
Defined in:
lib/stockboy/filter_chain.rb

Overview

A hash for executing items in order with callbacks

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(hash = nil) ⇒ Object

Initialize a new FilterChain with a hash of filters

Parameters:

  • hash (Hash{Symbol=>Filter}) (defaults to: nil)


11
12
13
# File 'lib/stockboy/filter_chain.rb', line 11

def self.new(hash=nil)
  super().replace(hash || {})
end

Instance Method Details

#keys_to_arraysHash{Symbol=>Array}

Returns Filter keys point to empty arrays.

Returns:

  • (Hash{Symbol=>Array})

    Filter keys point to empty arrays



36
37
38
# File 'lib/stockboy/filter_chain.rb', line 36

def keys_to_arrays
  Hash[keys.map { |k| [k, []] }]
end

#prepend(hash) ⇒ Object

Add filters to the front of the chain

Parameters:

  • hash (Hash{Symbol=>Filter})

    Filters to add



19
20
21
# File 'lib/stockboy/filter_chain.rb', line 19

def prepend(hash)
  replace hash.merge(self)
end

#resetHash{Symbol=>Array}

Call the reset callback on all filters that respond to it

Returns:

  • (Hash{Symbol=>Array})

    Filter keys point to empty arrays



27
28
29
30
31
32
# File 'lib/stockboy/filter_chain.rb', line 27

def reset
  each do |key, filter|
    filter.reset if filter.respond_to? :reset
  end
  keys_to_arrays
end