Module: MongoScript::ORM::MongoidAdapter::ClassMethods
- Defined in:
- lib/mongoscript/orm/mongoid_adapter.rb
Instance Method Summary collapse
-
#build_multiquery_parameters(criteria) ⇒ Object
Turn a Mongoid::Criteria into a hash useful for multiquery.
-
#database ⇒ Object
The MongoDB database.
-
#processable_into_parameters?(object) ⇒ Boolean
Answers whether a given non-hash object can be turned into a hash suitable for database queries.
-
#rehydrate(klass, data) ⇒ Object
Turn a raw hash returned by the MongoDB driver into a regular Ruby object.
-
#resolve_arguments(args) ⇒ Object
This resolves an array of Javascript arguments into hashes that can be used with MongoScript.execute.
-
#resolve_complex_criteria(hash) ⇒ Object
Recursively make sure any Mongoid complex critiera (like :_id.in => …) are expanded into regular hashes (see criteria_helpers.rb in Mongoid).
Instance Method Details
#build_multiquery_parameters(criteria) ⇒ Object
Turn a Mongoid::Criteria into a hash useful for multiquery.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mongoscript/orm/mongoid_adapter.rb', line 60 def build_multiquery_parameters(criteria) if criteria.is_a?(Mongoid::Criteria) opts = criteria..dup # make sure the sort options are in a Mongo-compatible format opts[:sort] = Mongo::Support::array_as_sort_parameters(opts[:sort] || []) { :selector => criteria.selector, :collection => criteria.collection.name, # used for rehydration :klass => criteria.klass, # fields are specified as a second parameter to the db[collection].find JS call :fields => opts[:fields], # everything in the options besides fields should be a modifier # i.e. a function that can be applied via a method to a db[collection].find query :modifiers => opts.tap { |o| o.delete(:fields) } } end end |
#database ⇒ Object
The MongoDB database.
8 9 10 |
# File 'lib/mongoscript/orm/mongoid_adapter.rb', line 8 def database Mongoid::Config.database end |
#processable_into_parameters?(object) ⇒ Boolean
Answers whether a given non-hash object can be turned into a hash suitable for database queries. Currently, it checks if the object is a Mongoid::Criteria.
84 85 86 |
# File 'lib/mongoscript/orm/mongoid_adapter.rb', line 84 def processable_into_parameters?(object) object.is_a?(Mongoid::Criteria) end |
#rehydrate(klass, data) ⇒ Object
Turn a raw hash returned by the MongoDB driver into a regular Ruby object. For this adapter, it a Mongoid document.
19 20 21 |
# File 'lib/mongoscript/orm/mongoid_adapter.rb', line 19 def rehydrate(klass, data) Mongoid::Factory.from_db(klass, data) end |
#resolve_arguments(args) ⇒ Object
This resolves an array of Javascript arguments into hashes that can be used with MongoScript.execute. In particular, it turns Mongoid complex criteria (_id.in => …) into regular Mongo-style hashes.
31 32 33 |
# File 'lib/mongoscript/orm/mongoid_adapter.rb', line 31 def resolve_arguments(args) args.map {|arg| arg.is_a?(Hash) ? resolve_complex_criteria(arg) : arg} end |
#resolve_complex_criteria(hash) ⇒ Object
this doesn’t (yet) check for circular dependencies, so don’t use them!
Recursively make sure any Mongoid complex critiera (like :_id.in => …) are expanded into regular hashes (see criteria_helpers.rb in Mongoid). The built-in function only goes one level in, which doesn’t work for hashes containing multiple levels with Mongoid helpers. (Am I missing where Mongoid handles this?)
47 48 49 50 51 52 53 |
# File 'lib/mongoscript/orm/mongoid_adapter.rb', line 47 def resolve_complex_criteria(hash) result = {} hash..each_pair do |k, v| result[k] = v.is_a?(Hash) ? resolve_complex_criteria(v) : v end result end |