Module: SirTracksAlot::FilterHelper

Included in:
Activity, Count
Defined in:
lib/sir_tracks_alot/filter_helper.rb

Instance Method Summary collapse

Instance Method Details

#extract_filter_options(options) ⇒ Object


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sir_tracks_alot/filter_helper.rb', line 50

def extract_filter_options(options)
  strings = {}
  matchers = {}
  arrays = {}
  
  options.each do |key, candidate|
    matchers[key] = candidate      if candidate.kind_of?(Regexp)  && !candidate.blank?
    strings[key]  = candidate.to_s if (candidate.kind_of?(String)) && !candidate.blank?
    arrays[key]   = candidate      if candidate.kind_of?(Array)   && !candidate.blank?
  end
  
  [strings, matchers, arrays]
end

#filter(filters, &block) ⇒ Object

Find activities that match attributes Strings are passed to Ohm Regular Expression filters match against retrieved attribute values

filter(:actor => 'user1', :target => //targets/d+/, :action => ['view', 'create'], :category => ['/root', '/other_root'])


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sir_tracks_alot/filter_helper.rb', line 8

def filter(filters, &block)      
  items = []
  all = []
  
  filters = [filters] unless filters.kind_of?(Array)
  
  filters.each do |options_for_find|
    strings, matchers, arrays = extract_filter_options(options_for_find)

    unless arrays.empty?
      (arrays.values.inject{|a, b| a.product b}).each do |combo|
        combo = [combo] unless combo.kind_of?(Array)
        terms = {}; combo.each{|c| terms[find_key_from_value(arrays, c)] = c}
        if terms.values.detect{|t| t.kind_of?(Regexp)}
          matchers.merge!(terms) 
          next 
        end
        
        all += find(terms.merge(strings)).to_a
      end          
    else 
      all += find(strings).to_a
    end

    all.each do |item|
      pass = true
      
      matchers.each do |key, matcher|
        pass = false if !matcher.match(item.send(key))
      end

      next unless pass
    
      # yield item if block_given?
    
      items << item
    end
  end
  
  items      
end