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 #92

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
57 changes: 55 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,59 @@ GEM
arel (~> 2.0.10)
tzinfo (~> 0.3.23)
activesupport (3.0.9)
addressable (2.2.6)
arel (2.0.10)
bcrypt-ruby (2.1.4)
builder (2.1.2)
data_objects (0.10.6)
addressable (~> 2.1)
datamapper (1.1.0)
dm-aggregates (= 1.1.0)
dm-constraints (= 1.1.0)
dm-core (= 1.1.0)
dm-migrations (= 1.1.0)
dm-serializer (= 1.1.0)
dm-timestamps (= 1.1.0)
dm-transactions (= 1.1.0)
dm-types (= 1.1.0)
dm-validations (= 1.1.0)
diff-lcs (1.1.2)
dm-aggregates (1.1.0)
dm-core (~> 1.1.0)
dm-constraints (1.1.0)
dm-core (~> 1.1.0)
dm-core (1.1.0)
addressable (~> 2.2.4)
dm-do-adapter (1.1.0)
data_objects (~> 0.10.2)
dm-core (~> 1.1.0)
dm-migrations (1.1.0)
dm-core (~> 1.1.0)
dm-serializer (1.1.0)
dm-core (~> 1.1.0)
fastercsv (~> 1.5.4)
json (~> 1.4.6)
dm-sqlite-adapter (1.1.0)
dm-do-adapter (~> 1.1.0)
do_sqlite3 (~> 0.10.2)
dm-timestamps (1.1.0)
dm-core (~> 1.1.0)
dm-transactions (1.1.0)
dm-core (~> 1.1.0)
dm-types (1.1.0)
bcrypt-ruby (~> 2.1.4)
dm-core (~> 1.1.0)
fastercsv (~> 1.5.4)
json (~> 1.4.6)
stringex (~> 1.2.0)
uuidtools (~> 2.1.2)
dm-validations (1.1.0)
dm-core (~> 1.1.0)
do_sqlite3 (0.10.6)
data_objects (= 0.10.6)
fastercsv (1.5.4)
i18n (0.5.0)
mysql (2.8.1)
json (1.4.6)
rake (0.9.2)
rcov (0.9.9)
rdoc (3.6.1)
Expand All @@ -32,16 +80,21 @@ GEM
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
sqlite3 (1.3.4)
stringex (1.2.2)
tzinfo (0.3.28)
uuidtools (2.1.2)

PLATFORMS
ruby

DEPENDENCIES
activerecord
datamapper
dm-sqlite-adapter
machinist!
mysql
rake
rcov
rdoc
rspec
sqlite3
13 changes: 13 additions & 0 deletions lib/machinist/data_mapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'dm-core'
require 'machinist'
require 'machinist/data_mapper/blueprint'
require 'machinist/data_mapper/lathe'

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

DataMapper::Model.append_extensions(Machinist::Machinable)
DataMapper::Model.append_extensions(Machinist::DataMapperExtensions)
19 changes: 19 additions & 0 deletions lib/machinist/data_mapper/blueprint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Machinist::DataMapper
class Blueprint < Machinist::Blueprint

# Make and save an object.
def make!(attributes = {})
object = make(attributes)
if object.save
object.reload
else
raise "#{object.class.name} is not valid"
end
end

def lathe_class #:nodoc:
Machinist::DataMapper::Lathe
end

end
end
28 changes: 28 additions & 0 deletions lib/machinist/data_mapper/lathe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Machinist::DataMapper

class Lathe < Machinist::Lathe

def make_one_value(attribute, args) #:nodoc:
if block_given?
raise_argument_error(attribute) unless args.empty?
yield
else
make_association(attribute, args)
end
end

def make_association(attribute, args) #:nodoc:
association = @klass.relationships[attribute]
if association
if association.is_a?(DataMapper::Associations::ManyToOne::Relationship)
association.parent_model.make(*args)
else
association.child_model.make(*args)
end
else
raise_argument_error(attribute)
end
end

end
end
6 changes: 3 additions & 3 deletions lib/machinist/lathe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ def make_one_value(attribute, args) #:nodoc:
raise_argument_error(attribute) unless args.empty?
yield
end

def assign_attribute(key, value) #:nodoc:
@assigned_attributes[key.to_sym] = value
@object.send("#{key}=", value)
end

def attribute_assigned?(key) #:nodoc:
@assigned_attributes.has_key?(key.to_sym)
end

def raise_argument_error(attribute) #:nodoc:
raise ArgumentError.new("Invalid arguments to attribute #{attribute} in blueprint")
raise ArgumentError.new("Invalid arguments to attribute #{attribute} in blueprint")
end

end
Expand Down
4 changes: 3 additions & 1 deletion machinist.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]

s.add_development_dependency "activerecord"
s.add_development_dependency "mysql"
s.add_development_dependency "sqlite3"
s.add_development_dependency "rake"
s.add_development_dependency "rcov"
s.add_development_dependency "rspec"
s.add_development_dependency "rdoc"
s.add_development_dependency "datamapper"
s.add_development_dependency "dm-sqlite-adapter"
end
170 changes: 86 additions & 84 deletions spec/active_record_spec.rb
Original file line number Diff line number Diff line change
@@ -1,108 +1,110 @@
require File.dirname(__FILE__) + '/spec_helper'
require 'support/active_record_environment'

describe Machinist::ActiveRecord do
include ActiveRecordEnvironment
module ActiveRecordModels
describe Machinist::ActiveRecord do
include ActiveRecordEnvironment

before(:each) do
empty_database!
end

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

context "make!" do
it "makes and saves objects" do
Post.blueprint { }
post = Post.make!
post.should be_a(Post)
post.should_not be_new_record
before(:each) do
empty_database!
end

it "raises an exception for an invalid object" do
User.blueprint { }
lambda {
User.make!(:username => "")
}.should raise_error(ActiveRecord::RecordInvalid)
context "make" do
it "returns an unsaved object" do
Post.blueprint { }
post = Post.make
post.should be_a(Post)
post.should be_new_record
end
end
end

context "associations support" do
it "handles belongs_to associations" do
User.blueprint do
username { "user_#{sn}" }
context "make!" do
it "makes and saves objects" do
Post.blueprint { }
post = Post.make!
post.should be_a(Post)
post.should_not be_new_record
end
Post.blueprint do
author

it "raises an exception for an invalid object" do
User.blueprint { }
lambda {
User.make!(:username => "")
}.should raise_error(ActiveRecord::RecordInvalid)
end
post = Post.make!
post.should be_a(Post)
post.should_not be_new_record
post.author.should be_a(User)
post.author.should_not be_new_record
end

it "handles has_many associations" do
Post.blueprint do
comments(3)
context "associations support" do
it "handles belongs_to associations" do
User.blueprint do
username { "user_#{sn}" }
end
Post.blueprint do
author
end
post = Post.make!
post.should be_a(Post)
post.should_not be_new_record
post.author.should be_a(User)
post.author.should_not be_new_record
end
Comment.blueprint { }
post = Post.make!
post.should be_a(Post)
post.should_not be_new_record
post.should have(3).comments
post.comments.each do |comment|
comment.should be_a(Comment)
comment.should_not be_new_record
end
end

it "handles habtm associations" do
Post.blueprint do
tags(3)
it "handles has_many associations" do
Post.blueprint do
comments(3)
end
Comment.blueprint { }
post = Post.make!
post.should be_a(Post)
post.should_not be_new_record
post.should have(3).comments
post.comments.each do |comment|
comment.should be_a(Comment)
comment.should_not be_new_record
end
end
Tag.blueprint do
name { "tag_#{sn}" }

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

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

it "handles overriding associations" do
User.blueprint do
username { "user_#{sn}" }
context "error handling" do
it "raises an exception for an attribute with no value" do
User.blueprint { username }
lambda {
User.make
}.should raise_error(ArgumentError)
end
Post.blueprint do
author { User.make(:username => "post_author_#{sn}") }
end
post = Post.make!
post.should be_a(Post)
post.should_not be_new_record
post.author.should be_a(User)
post.author.should_not be_new_record
post.author.username.should =~ /^post_author_\d+$/
end
end

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

end
Loading