Class: Array

Inherits:
Object
  • Object
show all
Includes:
Philter::Base, Philter::Evaluation, Philter::Search
Defined in:
lib/philter/array.rb

Overview

Philter with passion

Instance Method Summary collapse

Instance Method Details

#philter(search = nil, options = {}, &block) ⇒ Object

Have yourself indulging in the pleasure of philtering



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
49
50
51
# File 'lib/philter/array.rb', line 10

def philter(search = nil, options = {}, &block)
  options = { get:   nil,
              debug: false }.merge(options)
  unless search
    puts philter_help if options[:debug]
    raise 'Specify search parameter!'
  end
  puts if options[:debug]
  puts "#{'-' * 15} Start debugging philter #{Philter.version} #{'-' * 15}" if options[:debug]
  items = self
  apply_block = true
  results =
    case search.class.name
    when 'Hash'
      print ' Search by Hash:' if options[:debug]
      phil_search_by_attributes(items, search, options)
    when 'String'
      print ' Search by String:' if options[:debug]
      operator = phil_get_operator(search)
      if operator
        phil_search_with_operator(items, search, options)
      else
        puts " #{search} with grep" if options[:debug]
        items.grep(search)
      end
    when 'Array'
      print ' Search by Array:' if options[:debug]
      phil_search_by_array(items, search, options)
    else
      puts " Search with grep: #{search}" if options[:debug]
      apply_block = false
      items.grep(search, &block)
    end

  puts "#{'-' * 15} End debugging philter #{Philter.version} #{'-' * 15}" if options[:debug]
  puts " #{results.size} item(s) found" if options[:debug]
  if block_given? && apply_block
    results.map { |item| yield(item) }
  else
    results
  end
end