Skip to content

Commit

Permalink
Basic DataMapper support with tests.
Browse files Browse the repository at this point in the history
Still to do: make_unsaved, plan, make, methods on associations.

Does DataMapper have protected attributes?
  • Loading branch information
notahat committed May 28, 2009
1 parent e81c8ab commit bb09cd6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 15 deletions.
34 changes: 19 additions & 15 deletions lib/machinist/data_mapper.rb
@@ -1,26 +1,30 @@
require 'machinist'
require 'machinist/blueprints'
require 'dm-core'

module Machinist
module DataMapperExtensions
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
def make(*args, &block)
lathe = Lathe.run(Machinist::DataMapperAdapter, self.new, *args)
lathe.object.save!
lathe.object.reload
lathe.object(&block)
end
end
end

class DataMapperAdapter
def self.has_association?(object, attribute)
false
object.class.relationships.has_key?(attribute)
end

def self.class_for_association(object, attribute)
association = object.class.relationships[attribute]
association && association.parent_model
end
end

module DataMapperExtensions
def make(*args, &block)
lathe = Lathe.run(Machinist::DataMapperAdapter, self.new, *args)
lathe.object.save || raise("Save failed")
lathe.object.reload
lathe.object(&block)
end
end

end

DataMapper::Model.append_extensions(Machinist::Blueprints::ClassMethods)
DataMapper::Model.append_extensions(Machinist::DataMapperExtensions)
70 changes: 70 additions & 0 deletions spec/data_mapper_spec.rb
@@ -0,0 +1,70 @@
require File.dirname(__FILE__) + '/spec_helper'
require 'machinist/data_mapper'
require 'ruby-debug'

module MachinistDataMapperSpecs

class Person
include DataMapper::Resource
property :id, Serial
property :name, String
property :type, String
property :password, String
property :admin, Boolean, :default => false
end

class Post
include DataMapper::Resource
property :id, Serial
property :title, String
property :body, Text
property :published, Boolean, :default => true
has n, :comments
end

class Comment
include DataMapper::Resource
property :id, Serial
property :post_id, Integer
property :author_id, Integer
belongs_to :post
belongs_to :author, :class_name => "Person", :child_key => [:author_id]
end

describe Machinist, "DataMapper adapter" do
before(:suite) do
DataMapper.setup(:default, "sqlite3::memory:")
DataMapper.auto_migrate!
end

before(:each) do
Person.clear_blueprints!
Post.clear_blueprints!
Comment.clear_blueprints!
end

describe "make method" do
it "should save the constructed object" do
Person.blueprint { }
person = Person.make
person.should_not be_new_record
end

it "should create an object through belongs_to association" do
Post.blueprint { }
Comment.blueprint { post }
Comment.make.post.class.should == Post
end

it "should create an object through belongs_to association with a class_name attribute" do
Person.blueprint { }
Comment.blueprint { author }
Comment.make.author.class.should == Person
end

it "should raise an exception if the object can't be saved"
end

end
end

0 comments on commit bb09cd6

Please sign in to comment.