machinist-dm 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +38 -0
- data/Rakefile +1 -0
- data/lib/machinist-dm.rb +1 -0
- data/lib/machinist/data_mapper.rb +15 -0
- data/lib/machinist/data_mapper/blueprint.rb +26 -0
- data/lib/machinist/data_mapper/lathe.rb +24 -0
- data/lib/machinist/data_mapper/version.rb +5 -0
- data/machinist-dm.gemspec +30 -0
- data/spec/data_mapper_spec.rb +104 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/support/data_mapper_environment.rb +54 -0
- metadata +146 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# DataMapper Support for Machinist 2
|
2
|
+
|
3
|
+
This gem provides the needed blueprint and lathe for using Machinist 2 in
|
4
|
+
DataMapper projects. For details about Machinist, you should refer to the main
|
5
|
+
project on github:
|
6
|
+
|
7
|
+
- https://github.com/notahat/machinist
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Via rubygems:
|
12
|
+
|
13
|
+
gem install machinist-dm
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
If you're using Bundler, machinist-dm should be automatically included. To
|
18
|
+
include it explicitly:
|
19
|
+
|
20
|
+
require 'machinist/data_mapper'
|
21
|
+
|
22
|
+
Once it's included, usage is exactly the same as documented in the main
|
23
|
+
Machinist project.
|
24
|
+
|
25
|
+
## Credits
|
26
|
+
|
27
|
+
- [Pete Yandell, Machinist Author](https://github.com/notahat)
|
28
|
+
- [Emmanuel Gomez, Contributor](https://github.com/emmanuel)
|
29
|
+
|
30
|
+
## Issues
|
31
|
+
|
32
|
+
Please file issues in the Bug Tracker:
|
33
|
+
|
34
|
+
- https://github.com/d11wtq/machinist-dm/issues
|
35
|
+
|
36
|
+
Have fun with Machinist!
|
37
|
+
|
38
|
+
Chris Corbyn (aka Chippie)
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/machinist-dm.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'machinist/data_mapper'
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'dm-core'
|
2
|
+
require 'machinist'
|
3
|
+
require 'machinist/data_mapper/version'
|
4
|
+
require 'machinist/data_mapper/blueprint'
|
5
|
+
require 'machinist/data_mapper/lathe'
|
6
|
+
|
7
|
+
module Machinist::DataMapper
|
8
|
+
module BlueprintExtension
|
9
|
+
def blueprint_class
|
10
|
+
Machinist::DataMapper::Blueprint
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
DataMapper::Model.append_extensions(Machinist::Machinable, Machinist::DataMapper::BlueprintExtension)
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Machinist::DataMapper
|
2
|
+
class Blueprint < Machinist::Blueprint
|
3
|
+
# Make and save an object.
|
4
|
+
def make!(attributes = {})
|
5
|
+
object = make(attributes)
|
6
|
+
object.raise_on_save_failure = true
|
7
|
+
object.save && object.reload
|
8
|
+
end
|
9
|
+
|
10
|
+
def box(object)
|
11
|
+
object.key
|
12
|
+
end
|
13
|
+
|
14
|
+
def unbox(key)
|
15
|
+
@klass.get(*key)
|
16
|
+
end
|
17
|
+
|
18
|
+
def outside_transaction
|
19
|
+
yield # FIXME: This is not actually implemented
|
20
|
+
end
|
21
|
+
|
22
|
+
def lathe_class #:nodoc:
|
23
|
+
Machinist::DataMapper::Lathe
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Machinist::DataMapper
|
2
|
+
|
3
|
+
class Lathe < Machinist::Lathe
|
4
|
+
|
5
|
+
def make_one_value(attribute, args) #:nodoc:
|
6
|
+
if block_given?
|
7
|
+
raise_argument_error(attribute) unless args.empty?
|
8
|
+
yield
|
9
|
+
else
|
10
|
+
make_association(attribute, args)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def make_association(attribute, args) #:nodoc:
|
15
|
+
association = @klass.relationships[attribute]
|
16
|
+
if association
|
17
|
+
association.target_model.make(*args)
|
18
|
+
else
|
19
|
+
raise_argument_error(attribute)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "machinist/data_mapper/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "machinist-dm"
|
7
|
+
s.version = Machinist::DataMapper::VERSION
|
8
|
+
s.authors = ["Chris Corbyn"]
|
9
|
+
s.email = ["chris@w3style.co.uk"]
|
10
|
+
s.homepage = "https://github.com/d11wtq/machinist-dm"
|
11
|
+
s.summary = %q{DataMapper support for Machinist 2}
|
12
|
+
s.description = %q{Allows Machinist 2 to operate with DataMapper instead of ActiveRecord}
|
13
|
+
|
14
|
+
s.rubyforge_project = "machinist-dm"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "activesupport"
|
22
|
+
s.add_dependency "i18n"
|
23
|
+
s.add_dependency "machinist", ">= 2.0.0.beta2"
|
24
|
+
|
25
|
+
s.add_development_dependency "dm-core", ">= 1.1.0"
|
26
|
+
s.add_development_dependency "dm-migrations"
|
27
|
+
s.add_development_dependency "dm-validations"
|
28
|
+
s.add_development_dependency "dm-mysql-adapter"
|
29
|
+
s.add_development_dependency "rspec", "~> 2.6"
|
30
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
require 'support/data_mapper_environment'
|
3
|
+
|
4
|
+
describe Machinist::DataMapper do
|
5
|
+
DM = DataMapperEnvironment
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
DataMapperEnvironment.empty_database!
|
9
|
+
end
|
10
|
+
|
11
|
+
context "make" do
|
12
|
+
it "should return an unsaved object" do
|
13
|
+
DM::Post.blueprint { }
|
14
|
+
post = DM::Post.make
|
15
|
+
post.should be_a(DM::Post)
|
16
|
+
post.should be_new
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "make!" do
|
21
|
+
it "should make and save objects" do
|
22
|
+
DM::Post.blueprint { }
|
23
|
+
post = DM::Post.make!
|
24
|
+
post.should be_a(DM::Post)
|
25
|
+
post.should_not be_new
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should raise an exception for an invalid object" do
|
29
|
+
DM::User.blueprint { }
|
30
|
+
expect { DM::User.make!(:username => "") }.to
|
31
|
+
raise_error(DataMapper::SaveFailureError)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "associations support" do
|
36
|
+
it "should handle belongs_to associations" do
|
37
|
+
DM::User.blueprint do
|
38
|
+
username { "user_#{sn}" }
|
39
|
+
end
|
40
|
+
DM::Post.blueprint do
|
41
|
+
author
|
42
|
+
end
|
43
|
+
post = DM::Post.make!
|
44
|
+
post.should be_a(DM::Post)
|
45
|
+
post.should_not be_new
|
46
|
+
post.author.should be_a(DM::User)
|
47
|
+
post.author.should_not be_new
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should handle has_many associations" do
|
51
|
+
DM::Post.blueprint do
|
52
|
+
comments(3)
|
53
|
+
end
|
54
|
+
DM::Comment.blueprint { }
|
55
|
+
post = DM::Post.make!
|
56
|
+
post.should be_a(DM::Post)
|
57
|
+
post.should_not be_new
|
58
|
+
post.should have(3).comments
|
59
|
+
post.comments.each do |comment|
|
60
|
+
comment.should be_a(DM::Comment)
|
61
|
+
comment.should_not be_new
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should handle habtm associations" do
|
66
|
+
DM::Post.blueprint do
|
67
|
+
tags(3)
|
68
|
+
end
|
69
|
+
DM::Tag.blueprint do
|
70
|
+
name { "tag_#{sn}" }
|
71
|
+
end
|
72
|
+
post = DM::Post.make!
|
73
|
+
post.should be_a(DM::Post)
|
74
|
+
post.should_not be_new
|
75
|
+
post.should have(3).tags
|
76
|
+
post.tags.each do |tag|
|
77
|
+
tag.should be_a(DM::Tag)
|
78
|
+
tag.should_not be_new
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should handle overriding associations" do
|
83
|
+
DM::User.blueprint do
|
84
|
+
username { "user_#{sn}" }
|
85
|
+
end
|
86
|
+
DM::Post.blueprint do
|
87
|
+
author { DM::User.make!(:username => "post_author_#{sn}") }
|
88
|
+
end
|
89
|
+
post = DM::Post.make!
|
90
|
+
post.should be_a(DM::Post)
|
91
|
+
post.should_not be_new
|
92
|
+
post.author.should be_a(DM::User)
|
93
|
+
post.author.should_not be_new
|
94
|
+
post.author.username.should =~ /^post_author_\d+$/
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "error handling" do
|
99
|
+
it "should raise an exception for an attribute with no value" do
|
100
|
+
DM::User.blueprint { username }
|
101
|
+
expect { DM::User.make }.to raise_error(ArgumentError)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'dm-core'
|
2
|
+
require 'dm-validations'
|
3
|
+
require 'dm-migrations'
|
4
|
+
|
5
|
+
DataMapper.setup(:default, 'mysql://localhost/machinist?user=root&password=')
|
6
|
+
|
7
|
+
module DataMapperEnvironment
|
8
|
+
class User
|
9
|
+
include DataMapper::Resource
|
10
|
+
|
11
|
+
property :id, Serial
|
12
|
+
property :username, String, :required => true, :unique => true
|
13
|
+
end
|
14
|
+
|
15
|
+
class Post
|
16
|
+
include DataMapper::Resource
|
17
|
+
|
18
|
+
property :id, Serial
|
19
|
+
property :title, String
|
20
|
+
property :body, Text
|
21
|
+
|
22
|
+
belongs_to :author, 'User', :required => false
|
23
|
+
has n, :comments
|
24
|
+
has n, :tags, :through => Resource
|
25
|
+
end
|
26
|
+
|
27
|
+
class Comment
|
28
|
+
include DataMapper::Resource
|
29
|
+
|
30
|
+
property :id, Serial
|
31
|
+
property :body, Text
|
32
|
+
|
33
|
+
belongs_to :post, :required => false
|
34
|
+
end
|
35
|
+
|
36
|
+
class Tag
|
37
|
+
include DataMapper::Resource
|
38
|
+
|
39
|
+
property :id, Serial
|
40
|
+
property :name, String
|
41
|
+
|
42
|
+
has n, :posts, :through => Resource
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.empty_database!
|
46
|
+
[User, Post, Comment, Tag].each do |model|
|
47
|
+
model.destroy
|
48
|
+
model.clear_blueprints!
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
DataMapper.finalize
|
54
|
+
DataMapper.auto_migrate!
|
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: machinist-dm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chris Corbyn
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-08-26 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &12542620 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *12542620
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: i18n
|
27
|
+
requirement: &12542080 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *12542080
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: machinist
|
38
|
+
requirement: &12541460 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.0.0.beta2
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *12541460
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: dm-core
|
49
|
+
requirement: &12538480 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.1.0
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *12538480
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: dm-migrations
|
60
|
+
requirement: &12538080 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *12538080
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: dm-validations
|
71
|
+
requirement: &12537620 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *12537620
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: dm-mysql-adapter
|
82
|
+
requirement: &12537200 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *12537200
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: rspec
|
93
|
+
requirement: &12536620 !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ~>
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '2.6'
|
99
|
+
type: :development
|
100
|
+
prerelease: false
|
101
|
+
version_requirements: *12536620
|
102
|
+
description: Allows Machinist 2 to operate with DataMapper instead of ActiveRecord
|
103
|
+
email:
|
104
|
+
- chris@w3style.co.uk
|
105
|
+
executables: []
|
106
|
+
extensions: []
|
107
|
+
extra_rdoc_files: []
|
108
|
+
files:
|
109
|
+
- .gitignore
|
110
|
+
- Gemfile
|
111
|
+
- README.md
|
112
|
+
- Rakefile
|
113
|
+
- lib/machinist-dm.rb
|
114
|
+
- lib/machinist/data_mapper.rb
|
115
|
+
- lib/machinist/data_mapper/blueprint.rb
|
116
|
+
- lib/machinist/data_mapper/lathe.rb
|
117
|
+
- lib/machinist/data_mapper/version.rb
|
118
|
+
- machinist-dm.gemspec
|
119
|
+
- spec/data_mapper_spec.rb
|
120
|
+
- spec/spec_helper.rb
|
121
|
+
- spec/support/data_mapper_environment.rb
|
122
|
+
homepage: https://github.com/d11wtq/machinist-dm
|
123
|
+
licenses: []
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
none: false
|
136
|
+
requirements:
|
137
|
+
- - ! '>='
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubyforge_project: machinist-dm
|
142
|
+
rubygems_version: 1.8.6
|
143
|
+
signing_key:
|
144
|
+
specification_version: 3
|
145
|
+
summary: DataMapper support for Machinist 2
|
146
|
+
test_files: []
|