aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorPaul McMahon <paul@mobalean.com>2012-01-28 10:32:33 +0900
committerPaul McMahon <paul@mobalean.com>2012-01-28 10:32:33 +0900
commit1be248e246948c43867e1d512502cbfdfcae3dd6 (patch)
treefdb6d967099313eebd283c905ecb586f766fe83c /activerecord/lib
parentb495a8dba8f479dfd43985936bc33369465bdbaf (diff)
downloadrails-1be248e246948c43867e1d512502cbfdfcae3dd6.tar.gz
rails-1be248e246948c43867e1d512502cbfdfcae3dd6.tar.bz2
rails-1be248e246948c43867e1d512502cbfdfcae3dd6.zip
Extract different DynamicFinderMatch subclasses
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/dynamic_finder_match.rb65
1 files changed, 39 insertions, 26 deletions
diff --git a/activerecord/lib/active_record/dynamic_finder_match.rb b/activerecord/lib/active_record/dynamic_finder_match.rb
index b309df9b1b..0cdc49ae14 100644
--- a/activerecord/lib/active_record/dynamic_finder_match.rb
+++ b/activerecord/lib/active_record/dynamic_finder_match.rb
@@ -6,33 +6,17 @@ module ActiveRecord
#
class DynamicFinderMatch
def self.match(method)
- finder = :first
- bang = false
- instantiator = nil
-
- case method.to_s
- when /^find_(all_|last_)?by_([_a-zA-Z]\w*)$/
- finder = :last if $1 == 'last_'
- finder = :all if $1 == 'all_'
- names = $2
- when /^find_by_([_a-zA-Z]\w*)\!$/
- bang = true
- names = $1
- when /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/
- instantiator = $1 == 'initialize' ? :new : :create
- names = $2
- else
- return nil
+ [ FindBy, FindByBang, FindOrInitializeCreateBy ].each do |klass|
+ o = klass.match(method.to_s)
+ return o if o
end
-
- new(finder, instantiator, bang, names.split('_and_'))
+ nil
end
- def initialize(finder, instantiator, bang, attribute_names)
+ def initialize(finder, names, instantiator = nil)
@finder = finder
@instantiator = instantiator
- @bang = bang
- @attribute_names = attribute_names
+ @attribute_names = names.split('_and_')
end
attr_reader :finder, :attribute_names, :instantiator
@@ -41,16 +25,45 @@ module ActiveRecord
@finder && !@instantiator
end
+ def creator?
+ @finder == :first && @instantiator == :create
+ end
+
def instantiator?
- @finder == :first && @instantiator
+ @instantiator
end
- def creator?
- @finder == :first && @instantiator == :create
+ def bang?
+ false
+ end
+ end
+
+ class FindBy < DynamicFinderMatch
+ def self.match(method)
+ if method =~ /^find_(all_|last_)?by_([_a-zA-Z]\w*)$/
+ new($1 == 'last_' ? :last : $1 == 'all_' ? :all : :first, $2)
+ end
+ end
+ end
+
+ class FindByBang < DynamicFinderMatch
+ def self.match(method)
+ if method =~ /^find_by_([_a-zA-Z]\w*)\!$/
+ new(:first, $1)
+ end
end
def bang?
- @bang
+ true
+ end
+ end
+
+ class FindOrInitializeCreateBy < DynamicFinderMatch
+ def self.match(method)
+ instantiator = nil
+ if method =~ /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/
+ new(:first, $2, $1 == 'initialize' ? :new : :create)
+ end
end
end
end