Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataMapper support #53

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--color
--format documentation
--backtrace
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ gem "rake"
gem "rcov"
gem "rspec", ">= 2.0.0.beta.12"
gem "jeweler"

gem 'dm-core'
gem 'dm-sqlite-adapter'
gem 'dm-transactions'
gem 'dm-migrations'
gem 'dm-validations'
76 changes: 76 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
GEM
remote: http://rubygems.org/
specs:
activemodel (3.0.0)
activesupport (= 3.0.0)
builder (~> 2.1.2)
i18n (~> 0.4.1)
activerecord (3.0.0)
activemodel (= 3.0.0)
activesupport (= 3.0.0)
arel (~> 1.0.0)
tzinfo (~> 0.3.23)
activesupport (3.0.0)
addressable (2.2.0)
arel (1.0.1)
activesupport (~> 3.0.0)
builder (2.1.2)
data_objects (0.10.2)
addressable (~> 2.1)
diff-lcs (1.1.2)
dm-core (1.0.0)
addressable (~> 2.1)
extlib (~> 0.9.15)
dm-do-adapter (1.0.0)
data_objects (~> 0.10.1)
dm-core (~> 1.0.0)
dm-migrations (1.0.0)
dm-core (~> 1.0.0)
dm-sqlite-adapter (1.0.0)
dm-do-adapter (~> 1.0.0)
do_sqlite3 (~> 0.10.2)
dm-transactions (1.0.0)
dm-core (~> 1.0.0)
dm-validations (1.0.0)
dm-core (~> 1.0.0)
do_sqlite3 (0.10.2)
data_objects (= 0.10.2)
extlib (0.9.15)
gemcutter (0.6.1)
git (1.2.5)
i18n (0.4.1)
jeweler (1.4.0)
gemcutter (>= 0.1.0)
git (>= 1.2.5)
rubyforge (>= 2.0.0)
json_pure (1.4.6)
mysql (2.8.1)
rake (0.8.7)
rcov (0.9.8)
rspec (2.0.0.beta.20)
rspec-core (= 2.0.0.beta.20)
rspec-expectations (= 2.0.0.beta.20)
rspec-mocks (= 2.0.0.beta.20)
rspec-core (2.0.0.beta.20)
rspec-expectations (2.0.0.beta.20)
diff-lcs (>= 1.1.2)
rspec-mocks (2.0.0.beta.20)
rubyforge (2.0.4)
json_pure (>= 1.1.7)
tzinfo (0.3.23)

PLATFORMS
ruby

DEPENDENCIES
activerecord
dm-core
dm-migrations
dm-sqlite-adapter
dm-transactions
dm-validations
jeweler
mysql
rake
rcov
rspec (>= 2.0.0.beta.12)
55 changes: 55 additions & 0 deletions lib/machinist/datamapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'dm-core'
require 'machinist'

module Machinist::DataMapper

class Blueprint < Machinist::Blueprint

def make!(attributes = {})
object = make(attributes)
object.reload if object.save
end

def box(object)
object.id
end

def unbox(id)
@class.get(id)
end

def outside_transaction
raise NotImplementedError, 'Disable object caching'
end

def lathe_class
Machinist::DataMapper::Lathe
end

end

class Lathe < Machinist::Lathe

def make_one_value(attribute, args)
if block_given?
raise_argument_error(attribute) unless args.empty?
yield
else # make an association
association = @klass.relationships[attribute.to_s]
raise_argument_error(attribute) unless association
association_klass = association.parent_model == @klass ? association.child_model : association.parent_model
association_klass.make(*args)
end
end

end

end

module Machinist::DataMapper::Extension
def blueprint_class
Machinist::DataMapper::Blueprint
end
end

DataMapper::Model.append_extensions(Machinist::Machinable, Machinist::DataMapper::Extension)
9 changes: 8 additions & 1 deletion machinist.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
s.authors = ["Pete Yandell"]
s.date = %q{2010-07-07}
s.date = %q{2010-09-04}
s.email = %q{pete@notahat.com}
s.extra_rdoc_files = [
"README.markdown"
]
s.files = [
".gitignore",
".rspec",
"Gemfile",
"Gemfile.lock",
"MIT-LICENSE",
"README.markdown",
"Rakefile",
Expand All @@ -32,6 +34,7 @@ Gem::Specification.new do |s|
"lib/machinist/active_record/lathe.rb",
"lib/machinist/blueprint.rb",
"lib/machinist/configuration.rb",
"lib/machinist/datamapper.rb",
"lib/machinist/exceptions.rb",
"lib/machinist/lathe.rb",
"lib/machinist/machinable.rb",
Expand All @@ -40,12 +43,14 @@ Gem::Specification.new do |s|
"machinist.gemspec",
"spec/active_record_spec.rb",
"spec/blueprint_spec.rb",
"spec/datamapper_spec.rb",
"spec/exceptions_spec.rb",
"spec/inheritance_spec.rb",
"spec/machinable_spec.rb",
"spec/shop_spec.rb",
"spec/spec_helper.rb",
"spec/support/active_record_environment.rb",
"spec/support/datamapper_environment.rb",
"spec/warehouse_spec.rb"
]
s.homepage = %q{http://github.com/notahat/machinist}
Expand All @@ -56,12 +61,14 @@ Gem::Specification.new do |s|
s.test_files = [
"spec/active_record_spec.rb",
"spec/blueprint_spec.rb",
"spec/datamapper_spec.rb",
"spec/exceptions_spec.rb",
"spec/inheritance_spec.rb",
"spec/machinable_spec.rb",
"spec/shop_spec.rb",
"spec/spec_helper.rb",
"spec/support/active_record_environment.rb",
"spec/support/datamapper_environment.rb",
"spec/warehouse_spec.rb"
]

Expand Down
134 changes: 134 additions & 0 deletions spec/datamapper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
require File.dirname(__FILE__) + '/spec_helper'
require 'support/datamapper_environment'

describe Machinist::DataMapper do
include DM

before(:each) do
Machinist.configuration.cache_objects = false # not implemented
Machinist::Shop.instance.reset!
empty_database!
end

def fake_a_test
t = DataMapper::Transaction.new
t.begin
Machinist.reset_before_test
yield
t.rollback
end

context "make" do
it "should return an unsaved object" do
DM::Post.blueprint { }
post = DM::Post.make
post.should be_a(DM::Post)
post.should be_new_record
end
end

context "make!" do
it "should make and save objects" do
DM::Post.blueprint { }
post = DM::Post.make!
post.should be_a(DM::Post)
post.should_not be_new_record
end

it "should not save an invalid object" do
DM::User.blueprint { }
DM::User.make!(:username => "").should be_false
end

#Currently not implemented
#it "should buy objects from the shop" do
#DM::Post.blueprint { }
#post_a, post_b = nil, nil
#fake_a_test { post_a = DM::Post.make! }
#fake_a_test { post_b = DM::Post.make! }
#post_a.should == post_b
#end

#it "should not buy objects from the shop if caching is disabled" do
#DM::Post.blueprint { }
#post_a, post_b = nil, nil
#fake_a_test { post_a = DM::Post.make! }
#fake_a_test { post_b = DM::Post.make! }
#post_a.should_not == post_b
#Machinist.configuration.cache_objects = true
#end
end

context "associations support" do
it "should handle belongs_to associations" do
DM::User.blueprint do
username { "user_#{sn}" }
end
DM::Post.blueprint do
author
end
post = DM::Post.make!
post.should be_a(DM::Post)
post.should_not be_new_record
post.author.should be_a(DM::User)
post.author.should_not be_new_record
end

it "should handle has_many associations" do
DM::Post.blueprint do
comments(3)
end
DM::Comment.blueprint { }
post = DM::Post.make!
post.should be_a(DM::Post)
post.should_not be_new_record
post.should have(3).comments
post.comments.each do |comment|
comment.should be_a(DM::Comment)
comment.should_not be_new_record
end
end

it "should handle habtm associations" do
DM::Post.blueprint do
tags(3)
end
DM::Tag.blueprint do
name { "tag_#{sn}" }
end
post = DM::Post.make!
post.should be_a(DM::Post)
post.should_not be_new_record
post.should have(3).tags
post.tags.each do |tag|
tag.should be_a(DM::Tag)
tag.should_not be_new_record
end
end

it "should handle overriding associations" do
DM::User.blueprint do
username { "user_#{sn}" }
end
DM::Post.blueprint do
author { DM::User.make!(:username => "post_author_#{sn}") }
end
post = DM::Post.make!
post.should be_a(DM::Post)
post.should_not be_new_record
post.author.should be_a(DM::User)
post.author.should_not be_new_record
post.author.username.should =~ /^post_author_\d+$/
end
end

context "error handling" do
it "should raise an exception for an attribute with no value" do
DM::User.blueprint { username }
lambda {
DM::User.make
}.should raise_error(ArgumentError)
end
end

end
55 changes: 55 additions & 0 deletions spec/support/datamapper_environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'dm-core'
require 'dm-migrations'
require 'dm-validations'
require 'dm-transactions'
require 'machinist/datamapper'

DataMapper.setup(:default, 'sqlite::memory:')

module DM

class User
include DataMapper::Resource

property :id, Serial
property :username, String, :required => true, :unique => true
end

class Post
include DataMapper::Resource

property :id, Serial
property :author_id, Integer
property :body, Text

belongs_to :author, 'User'
has n, :comments
has n, :tags, :through => Resource
end

class Comment
include DataMapper::Resource

property :id, Serial
property :body, Text

belongs_to :post
end

class Tag
include DataMapper::Resource

property :id, Serial
property :name, String

has n, :posts, :through => Resource
end

def empty_database!
DataMapper.auto_migrate!
[User, Post, Comment].each do |klass|
klass.clear_blueprints!
end
end

end