Skip to content
54 changes: 49 additions & 5 deletions lib/orm_adapter/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,33 @@ def column_names

# @see OrmAdapter::Base#get!
def get!(id)
klass.find(wrap_key(id))
scoped.find(wrap_key(id))
end

# @see OrmAdapter::Base#get
def get(id)
klass.where(klass.primary_key => wrap_key(id)).first
scoped.where(klass.primary_key => wrap_key(id)).first
end

# @see OrmAdapter::Base#find_first
def find_first(options = {})
conditions, order = extract_conditions!(options)
klass.where(conditions_to_fields(conditions)).order(*order_clause(order)).first
scoped.where(conditions_to_fields(conditions)).order(*order_clause(order)).first
end

# @see OrmAdapter::Base#find_all
def find_all(options = {})
conditions, order, limit, offset = extract_conditions!(options)
klass.where(conditions_to_fields(conditions)).order(*order_clause(order)).limit(limit).offset(offset).all
scoped.where(conditions_to_fields(conditions)).order(*order_clause(order)).limit(limit).offset(offset).all
end

def build(attributes = {})
scoped.build(attributes)
end

# @see OrmAdapter::Base#create!
def create!(attributes = {})
klass.create!(attributes)
scoped.create!(attributes)
end

# @see OrmAdapter::Base#destroy
Expand All @@ -41,6 +45,10 @@ def destroy(object)

protected

def scoped
klass.where(conditions_to_fields(@scope))
end

# Introspects the klass to convert and objects in conditions into foreign key and type fields
def conditions_to_fields(conditions)
fields = {}
Expand All @@ -65,6 +73,42 @@ def conditions_to_fields(conditions)
def order_clause(order)
order.map {|pair| "#{pair[0]} #{pair[1]}"}.join(",")
end

#class Collection
#
# attr_reader :collection
#
# def initialize(collection)
# @collection = collection
# end
#
# def klass
# collection.klass
# end
#
# def klass_adapter
# @klass_adapter ||= klass.to_adapter
# end
#
# def find_first(options = {})
# conditions, order = klass_adapter.send(:extract_conditions!, options)
# collection.scoped.where(klass_adapter.send(:conditions_to_fields, conditions)).order(*klass_adapter.send(:order_clause, order)).first
# end
#
# def find(options = {})
# conditions, order, limit, offset = klass_adapter.send(:extract_conditions!, options)
# collection.scoped.where(klass_adapter.send(:conditions_to_fields, conditions)).order(*klass_adapter.send(:order_clause, order)).limit(limit).offset(offset)
# end
#
# def build(attributes = {})
# collection.build(attributes)
# end
#
# def create!(attributes = {})
# collection.create!(attributes)
# end
#
#end
end
end

Expand Down
28 changes: 21 additions & 7 deletions lib/orm_adapter/adapters/data_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ def column_names
end

# @see OrmAdapter::Base#get!
def get!(id)
klass.get!(id)
def get!(*id)
get(*id) || raise(DataMapper::ObjectNotFoundError)
end

# @see OrmAdapter::Base#get
def get(id)
klass.get(id)
def get(*id)
if @scope.empty?
klass.get(*id)
else
primary_key_conditions = klass.key_conditions(klass.repository, klass.key(klass.repository.name).typecast(id)).update(:order => nil)
klass.first(@scope.merge(primary_key_conditions))
end
end

# @see OrmAdapter::Base#find_first
def find_first(options = {})
conditions, order = extract_conditions!(options)
klass.first :conditions => conditions, :order => order_clause(order)
klass.first(scoped_query.update(:conditions => conditions, :order => order_clause(order)))
end

# @see OrmAdapter::Base#find_all
Expand All @@ -34,12 +39,17 @@ def find_all(options = {})
opts = { :conditions => conditions, :order => order_clause(order) }
opts = opts.merge({ :limit => limit }) unless limit.nil?
opts = opts.merge({ :offset => offset }) unless offset.nil?
klass.all opts
klass.all(scoped_query.update(opts))
end

# @see OrmAdapter::Base#build
def build(attributes = {})
klass.new(@scope.merge(attributes))
end

# @see OrmAdapter::Base#create!
def create!(attributes = {})
klass.create(attributes)
klass.create(@scope.merge(attributes))
end

# @see OrmAdapter::Base#destroy
Expand All @@ -49,6 +59,10 @@ def destroy(object)

protected

def scoped_query
klass.all(@scope).query
end

def order_clause(order)
order.map {|pair| pair.first.send(pair.last)}
end
Expand Down
19 changes: 14 additions & 5 deletions lib/orm_adapter/adapters/mongo_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ def column_names

# @see OrmAdapter::Base#get!
def get!(id)
klass.find!(wrap_key(id))
scoped.find!(wrap_key(id))
end

# @see OrmAdapter::Base#get
def get(id)
klass.first({ :id => wrap_key(id) })
scoped.first({ :id => wrap_key(id) })
end

# @see OrmAdapter::Base#find_first
def find_first(conditions = {})
conditions, order = extract_conditions!(conditions)
conditions = conditions.merge(:sort => order) unless order.nil?
klass.first(conditions_to_fields(conditions))
scoped.first(conditions_to_fields(conditions))
end

# @see OrmAdapter::Base#find_all
Expand All @@ -35,12 +35,17 @@ def find_all(conditions = {})
conditions = conditions.merge(:sort => order) unless order.nil?
conditions = conditions.merge(:limit => limit) unless limit.nil?
conditions = conditions.merge(:offset => offset) unless limit.nil? || offset.nil?
klass.all(conditions_to_fields(conditions))
scoped.all(conditions_to_fields(conditions))
end

# @see OrmAdapter::Base#build
def build(attributes = {})
klass.new(@scope.merge(attributes))
end

# @see OrmAdapter::Base#create!
def create!(attributes = {})
klass.create!(attributes)
klass.create!(@scope.merge(attributes))
end

# @see OrmAdapter::Base#destroy
Expand All @@ -50,6 +55,10 @@ def destroy(object)

protected

def scoped
klass.where(conditions_to_fields(@scope))
end

# converts and documents to ids
def conditions_to_fields(conditions)
conditions.inject({}) do |fields, (key, value)|
Expand Down
19 changes: 14 additions & 5 deletions lib/orm_adapter/adapters/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,34 @@ def column_names

# @see OrmAdapter::Base#get!
def get!(id)
klass.find(wrap_key(id))
scoped.find(wrap_key(id))
end

# @see OrmAdapter::Base#get
def get(id)
klass.where(:_id => wrap_key(id)).first
scoped.where(:_id => wrap_key(id)).first
end

# @see OrmAdapter::Base#find_first
def find_first(options = {})
conditions, order = extract_conditions!(options)
klass.limit(1).where(conditions_to_fields(conditions)).order_by(order).first
scoped.limit(1).where(conditions_to_fields(conditions)).order_by(order).first
end

# @see OrmAdapter::Base#find_all
def find_all(options = {})
conditions, order, limit, offset = extract_conditions!(options)
klass.where(conditions_to_fields(conditions)).order_by(order).limit(limit).offset(offset)
scoped.where(conditions_to_fields(conditions)).order_by(order).limit(limit).offset(offset)
end

# @see OrmAdapter::Base#build
def build(attributes = {})
scoped.new(attributes)
end

# @see OrmAdapter::Base#create!
def create!(attributes = {})
klass.create!(attributes)
scoped.create!(attributes)
end

# @see OrmAdapter::Base#destroy
Expand All @@ -46,6 +51,10 @@ def destroy(object)

protected

def scoped
klass.where(conditions_to_fields(@scope))
end

# converts and documents to ids
def conditions_to_fields(conditions)
conditions.inject({}) do |fields, (key, value)|
Expand Down
9 changes: 7 additions & 2 deletions lib/orm_adapter/base.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module OrmAdapter
class Base
attr_reader :klass
attr_reader :klass, :scope

# Your ORM adapter needs to inherit from this Base class and its adapter
# will be registered. To create an adapter you should create an inner
Expand All @@ -14,8 +14,9 @@ def self.inherited(adapter)
super
end

def initialize(klass)
def initialize(klass, scope={})
@klass = klass
@scope = scope
end

# Get a list of column/property/field names
Expand Down Expand Up @@ -68,6 +69,10 @@ def find_all(options = {})
raise NotSupportedError
end

def build(attributes = {})
raise NotSupportedError
end

# Create a model using attributes
def create!(attributes = {})
raise NotSupportedError
Expand Down
5 changes: 3 additions & 2 deletions lib/orm_adapter/to_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module OrmAdapter
# Extend into a class that has an OrmAdapter
module ToAdapter
def to_adapter
@_to_adapter ||= self::OrmAdapter.new(self)
def to_adapter(scope = {})
self::OrmAdapter.new(self, scope)
end
end

end
4 changes: 1 addition & 3 deletions spec/orm_adapter/adapters/mongo_mapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ class Note
describe MongoMapper::Document::OrmAdapter do

before do
MongoMapper.database.collections.each do | coll |
coll.remove
end
MongoMapper.database.collections.each(&:remove)
end

it_should_behave_like "example app with orm_adapter" do
Expand Down
6 changes: 3 additions & 3 deletions spec/orm_adapter/adapters/mongoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
else

Mongoid.configure do |config|
config.master = Mongo::Connection.new.db('orm_adapter_spec')
config.connect_to( 'orm_adapter_spec' )
end

module MongoidOrmSpec
class User
include Mongoid::Document
field :name
field :rating
has_many_related :notes, :foreign_key => :owner_id, :class_name => 'MongoidOrmSpec::Note'
has_many :notes, :foreign_key => :owner_id, :class_name => 'MongoidOrmSpec::Note'
end

class Note
include Mongoid::Document
field :body, :default => "made by orm"
belongs_to_related :owner, :class_name => 'MongoidOrmSpec::User'
belongs_to :owner, :class_name => 'MongoidOrmSpec::User'
end

# here be the specs!
Expand Down
Loading