views:

192

answers:

1

This regards the ruby ORM library DataMapper.

This wiki describes how to use the in_memory adapter for DataMapper. The proper database adapters saves an incrementing, unique id on each model instance - in_memory doesn't seem to do that as shown by the following snippet:

require 'rubygems'
require 'dm-core'

DataMapper.setup(:in_memory, :adapter => 'in_memory')

class Foo
    include DataMapper::Resource

    def self.default_repository_name;:in_memory;end
    def self.auto_migrate_down!(rep);end
    def self.auto_migrate_up!(rep);end
    def self.auto_upgrade!(rep);end

    property :id, Serial
    property :name, Text
end

f = Foo.new
f.name = "foo"
f.save

puts f.inspect

The results of the inspect is:

#<Foo id=nil name="foo">

Had I used another adapter to connect to, for instance, a sqlite database id would've been set to '1'.

I would like to refer to my models via id, because I can't guarantee uniqueness on other attribuets. Is there a way of making the in_memory adapter save an incrementing, unique id attribute for its models?

A: 

As far as I can tell, auto-incrementing is not supported for the DM in-memory adapter (I suspect that adapter doesn't get much love) but you could fake it pretty easily:

before :save, :increment_id
def increment_id
  self.id ||= begin
    last_foo = (Foo.all.sort_by { |r| r.id }).last
    (last_foo ? last_foo.id : 0).succ
  end
end

I don't think I'd recommend this though. One probably much better alternative would be to use a SQLite in-memory database instead.

Bob Aman