Module: MongoScript::ORM::MongoidAdapter::ClassMethods

Defined in:
lib/mongoscript/orm/mongoid_adapter.rb

Instance Method Summary collapse

Instance Method Details

#build_multiquery_parameters(criteria) ⇒ Object

Turn a Mongoid::Criteria into a hash useful for multiquery.

Parameters:

  • criteria

    any Mongoid::Criteria object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mongoscript/orm/mongoid_adapter.rb', line 57

def build_multiquery_parameters(criteria)
  if criteria.is_a?(Mongoid::Criteria)
    opts = criteria.options.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

#databaseObject



12
13
14
# File 'lib/mongoscript/orm/mongoid_adapter.rb', line 12

def database
  Mongoid::Config.database
end

#processable_into_parameters?(object) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/mongoscript/orm/mongoid_adapter.rb', line 76

def processable_into_parameters?(object)
  object.is_a?(Mongoid::Criteria)
end

#rehydrate(klass, data) ⇒ Object



16
17
18
# File 'lib/mongoscript/orm/mongoid_adapter.rb', line 16

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.



28
29
30
# File 'lib/mongoscript/orm/mongoid_adapter.rb', line 28

def resolve_arguments(args)
  args.map {|arg| arg.is_a?(Hash) ? resolve_complex_criteria(arg) : arg}
end

#resolve_complex_criteria(hash) ⇒ Object

Note:

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?)



44
45
46
47
48
49
50
# File 'lib/mongoscript/orm/mongoid_adapter.rb', line 44

def resolve_complex_criteria(hash)
  result = {}
  hash.expand_complex_criteria.each_pair do |k, v|
    result[k] = v.is_a?(Hash) ? resolve_complex_criteria(v) : v
  end
  result
end