Skip to content
Browse files

Extract different DynamicFinderMatch subclasses

  • Loading branch information...
1 parent b495a8d commit 1be248e246948c43867e1d512502cbfdfcae3dd6 @pwim pwim committed Jan 28, 2012
Showing with 39 additions and 26 deletions.
  1. +39 −26 activerecord/lib/active_record/dynamic_finder_match.rb
View
65 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 @@ def finder?
@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

0 comments on commit 1be248e

Please sign in to comment.
Something went wrong with that request. Please try again.