Module: Tyrion::Querying::ClassMethods

Defined in:
lib/tyrion/querying.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tyrion/querying.rb', line 26

def method_missing(method, *args)
  if method.to_s =~ /^find_by_(.+)$/
    attr = $1
    arg = args.shift || raise("Provide at least one argument!")
    
    all.each do |doc|
      operator = if arg.is_a? Regexp
        :=~
      else
        :==
      end
      
      return doc if doc.attributes[attr].send(operator, arg)
    end
    
    nil
  else
    super
  end
end

Instance Method Details

#allObject



6
7
8
# File 'lib/tyrion/querying.rb', line 6

def all
  storage[klass_name].dup
end

#where(attributes = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tyrion/querying.rb', line 10

def where attributes = {}
  all.map do |doc|
    match = attributes.each_pair.map do |k, v|
      operator = if v.is_a? Regexp
        :=~
      else
        :==
      end
      
      true if doc.send(:read_attribute, k.to_s).send(operator, v)
    end.compact
    
    doc if match.count == attributes.count
  end.compact
end