Module: Mongoid::FindBy::ClassMethods
- Defined in:
- lib/mongoid/find_by.rb
Instance Method Summary collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *args, &block) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mongoid/find_by.rb', line 11 def method_missing(method_id, *args, &block) conditions = {} bang = false case method_id.to_s when /^find_(all|last||first)_?by_([_a-zA-Z]\w*)(!?)$/ finder_type = $1.blank? ? :first : $1.to_sym bang = true if $3 == '!' tokens = $2.split(/_and_/) raise ArgumentError, 'Attributes and arguments does\'nt match' if tokens.size != args.size tokens.each_with_index do |attr, i| conditions[attr] = args[i] end result = find(finder_type, :conditions => conditions) if result.nil? and bang raise Mongoid::Errors::DocumentNotFound.new(self.class, args) else return result end else super end end |