Sha256: 6f133b63f1480122475944d90fde933fe17e04f87fea26e94a0c1a3b0f9e8d87
Contents?: true
Size: 1.87 KB
Versions: 1
Compression:
Stored size: 1.87 KB
Contents
require 'mongoid' module Mongoid module Document module ClassMethods include OrmAdapter::ToAdapter end class OrmAdapter < ::OrmAdapter::Base # Do not consider these to be part of the class list def self.except_classes @@except_classes ||= [] end # Gets a list of the available models for this adapter def self.model_classes ObjectSpace.each_object(Class).to_a.select {|klass| klass.ancestors.include? Mongoid::Document} end # get a list of column names for a given class def column_names klass.fields.keys end # @see OrmAdapter::Base#get! def get!(id) klass.find(wrap_key(id)) end # @see OrmAdapter::Base#get def get(id) klass.where(:_id => wrap_key(id)).first end # @see OrmAdapter::Base#find_first def find_first(options) conditions, order = extract_conditions_and_order!(options) klass.limit(1).where(conditions_to_fields(conditions)).order_by(order).first end # @see OrmAdapter::Base#find_all def find_all(options) conditions, order = extract_conditions_and_order!(options) klass.where(conditions_to_fields(conditions)).order_by(order) end # @see OrmAdapter::Base#create! def create!(attributes) klass.create!(attributes) end # @see OrmAdapter::Base#destroy def destroy(object) object.destroy if valid_object?(object) end protected # converts and documents to ids def conditions_to_fields(conditions) conditions.inject({}) do |fields, (key, value)| if value.is_a?(Mongoid::Document) && klass.fields.keys.include?("#{key}_id") fields.merge("#{key}_id" => value.id) else fields.merge(key => value) end end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
orm_adapter-0.1.0 | lib/orm_adapter/adapters/mongoid.rb |