forked from notahat/machinist
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic DataMapper support with tests.
Still to do: make_unsaved, plan, make, methods on associations. Does DataMapper have protected attributes?
- Loading branch information
Showing
2 changed files
with
89 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|