1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
require 'dm-core'
module DataMapper
module Model
include OrmAdapter::ToAdapter
end
module Resource
class OrmAdapter < ::OrmAdapter::Base
# get a list of column names for a given class
def column_names
klass.properties.map(&:name)
end
# @see OrmAdapter::Base#get!
def get!(id)
klass.get!(id)
end
# @see OrmAdapter::Base#get
def get(id)
klass.get(id)
end
# @see OrmAdapter::Base#find_first
def find_first(options = {})
conditions, order = extract_conditions!(options)
klass.first :conditions => conditions, :order => order_clause(order)
end
# @see OrmAdapter::Base#find_all
def find_all(options = {})
conditions, order, limit, offset = extract_conditions!(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
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
def order_clause(order)
order.map {|pair| pair.first.send(pair.last)}
end
end
end
end
|