Class: Fynd::Criteria

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/fynd/criteria.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*paths) ⇒ Criteria

Returns a new instance of Criteria.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fynd/criteria.rb', line 14

def initialize(*paths)
  @paths = paths
  @sieve = Sieve.new
  @sieve.collection = []      
  @sieve.conditions = {}
  @sieve.conditions['actions'] = {}
  @sieve.conditions['operators'] = {}
  @sieve.conditions['tests'] = {}
  
  # Flatten it and strip out non-unique files
  
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fynd/criteria.rb', line 50

def method_missing(symbol, *args, &block)
  if sieve.respond_to?(symbol)
    if ACTIONS.include?(symbol)
      # Only one action per expression
      sieve.conditions['action'] = { symbol.to_s => args[0] }
    elsif OPERATORS.include?(symbol)
      sieve.conditions['operations'].deep_merge!({ symbol.to_s => args[0] })
    elsif TESTS.include?(symbol)
      sieve.conditions['tests'].deep_merge!({ symbol.to_s => args[0] })
    else
      super(symbol, *args, &blocks)
    end
    self
  else
    super(symbol, *args, &blocks)
  end
end

Instance Attribute Details

#pathsObject

Returns the value of attribute paths.



10
11
12
# File 'lib/fynd/criteria.rb', line 10

def paths
  @paths
end

#sieveObject

Returns the value of attribute sieve.



10
11
12
# File 'lib/fynd/criteria.rb', line 10

def sieve
  @sieve
end

Instance Method Details

#filesObject



40
41
42
43
44
45
46
47
48
# File 'lib/fynd/criteria.rb', line 40

def files
  files = run
  
  if block_given?
    yield files
  else
    return files
  end
end

#runObject

def find(*paths)

where(:paths => paths)
self

end



32
33
34
35
36
37
38
# File 'lib/fynd/criteria.rb', line 32

def run
  @sieve.collection = paths.map do |path|
    Find.find(File.expand_path(path)).to_a
  end.flatten.uniq
  sieve.run
  return sieve.files
end