X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=vendor%2Frails%2Factiverecord%2Flib%2Factive_record%2Fdynamic_finder_match.rb;fp=vendor%2Frails%2Factiverecord%2Flib%2Factive_record%2Fdynamic_finder_match.rb;h=8f9f05ce36b358b7bdd0546aa29a68a4b1960a22;hb=d115f2e23823271635bad69229a42cd8ac68debe;hp=0000000000000000000000000000000000000000;hpb=37cb670bf3ddde90b214e591f100ed4446469484;p=depot.git

diff --git a/vendor/rails/activerecord/lib/active_record/dynamic_finder_match.rb b/vendor/rails/activerecord/lib/active_record/dynamic_finder_match.rb
new file mode 100644
index 0000000..8f9f05c
--- /dev/null
+++ b/vendor/rails/activerecord/lib/active_record/dynamic_finder_match.rb
@@ -0,0 +1,41 @@
+module ActiveRecord
+  class DynamicFinderMatch
+    def self.match(method)
+      df_match = self.new(method)
+      df_match.finder ? df_match : nil
+    end
+
+    def initialize(method)
+      @finder = :first
+      case method.to_s
+      when /^find_(all_by|last_by|by)_([_a-zA-Z]\w*)$/
+        @finder = :last if $1 == 'last_by'
+        @finder = :all if $1 == 'all_by'
+        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
+        @finder = nil
+      end
+      @attribute_names = names && names.split('_and_')
+    end
+
+    attr_reader :finder, :attribute_names, :instantiator
+
+    def finder?
+      !@finder.nil? && @instantiator.nil?
+    end
+
+    def instantiator?
+      @finder == :first && !@instantiator.nil?
+    end
+
+    def bang?
+      @bang
+    end
+  end
+end