Froze rails gems
[depot.git] / vendor / rails / activerecord / lib / active_record / dynamic_finder_match.rb
1 module ActiveRecord
2 class DynamicFinderMatch
3 def self.match(method)
4 df_match = self.new(method)
5 df_match.finder ? df_match : nil
6 end
7
8 def initialize(method)
9 @finder = :first
10 case method.to_s
11 when /^find_(all_by|last_by|by)_([_a-zA-Z]\w*)$/
12 @finder = :last if $1 == 'last_by'
13 @finder = :all if $1 == 'all_by'
14 names = $2
15 when /^find_by_([_a-zA-Z]\w*)\!$/
16 @bang = true
17 names = $1
18 when /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/
19 @instantiator = $1 == 'initialize' ? :new : :create
20 names = $2
21 else
22 @finder = nil
23 end
24 @attribute_names = names && names.split('_and_')
25 end
26
27 attr_reader :finder, :attribute_names, :instantiator
28
29 def finder?
30 !@finder.nil? && @instantiator.nil?
31 end
32
33 def instantiator?
34 @finder == :first && !@instantiator.nil?
35 end
36
37 def bang?
38 @bang
39 end
40 end
41 end