Class: ActiveMongoid::FinderProxy
- Inherits:
-
Object
- Object
- ActiveMongoid::FinderProxy
- Defined in:
- lib/active_mongoid/finder_proxy.rb
Instance Attribute Summary collapse
-
#__target ⇒ Object
Returns the value of attribute __target.
-
#__target_class ⇒ Object
Returns the value of attribute __target_class.
Instance Method Summary collapse
- #find(*args) ⇒ Object
-
#initialize(target) ⇒ FinderProxy
constructor
A new instance of FinderProxy.
- #method_missing(name, *args, &block) ⇒ Object
- #where(opts = :chain, *rest) ⇒ Object
Constructor Details
#initialize(target) ⇒ FinderProxy
Returns a new instance of FinderProxy.
11 12 13 14 |
# File 'lib/active_mongoid/finder_proxy.rb', line 11 def initialize(target) @__target = target @__target_class = target.respond_to?(:klass) ? target.klass : target end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/active_mongoid/finder_proxy.rb', line 45 def method_missing(name, *args, &block) resp = __target.send(name, *args, &block) if resp == __target_class || (resp.is_a?(ActiveRecord::Relation) && resp.klass == __target_class) FinderProxy.new(resp) else resp end end |
Instance Attribute Details
#__target ⇒ Object
Returns the value of attribute __target.
7 8 9 |
# File 'lib/active_mongoid/finder_proxy.rb', line 7 def __target @__target end |
#__target_class ⇒ Object
Returns the value of attribute __target_class.
8 9 10 |
# File 'lib/active_mongoid/finder_proxy.rb', line 8 def __target_class @__target_class end |
Instance Method Details
#find(*args) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/active_mongoid/finder_proxy.rb', line 16 def find(*args) key = args.flatten.first if !key.is_a?(Fixnum) && (key.is_a?(::ActiveMongoid::BSON::ObjectId) || ::ActiveMongoid::BSON::ObjectId.legal?(key)) where({_id: key.to_s}).first.tap do |obj| raise ActiveRecord::RecordNotFound unless obj end else FinderProxy.new(__target.send(:find, *args)) end end |
#where(opts = :chain, *rest) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/active_mongoid/finder_proxy.rb', line 27 def where(opts = :chain, *rest) if opts && opts.is_a?(Hash) bson_opts = opts.select{|k,v| v.is_a?(::ActiveMongoid::BSON::ObjectId)} if bson_opts[:id] opts.delete(:id) bson_opts[:_id] = bson_opts.delete(:id) end bson_opts.each do |k,v| bson_opts[k] = v.to_s end opts.merge!(bson_opts) end FinderProxy.new(__target.send(:where, opts, *rest)) end |