dm-sweatshop 0.9.8 → 0.9.9
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +10 -0
- data/README.textile +2 -2
- data/lib/dm-sweatshop.rb +1 -1
- data/lib/dm-sweatshop/model.rb +16 -0
- data/lib/dm-sweatshop/sweatshop.rb +14 -0
- data/lib/dm-sweatshop/version.rb +1 -1
- data/spec/dm-sweatshop/model_spec.rb +23 -1
- data/spec/spec_helper.rb +13 -6
- metadata +3 -3
data/History.txt
CHANGED
data/README.textile
CHANGED
@@ -114,12 +114,12 @@ Person.gen(:valid)
|
|
114
114
|
</code>
|
115
115
|
</pre>
|
116
116
|
|
117
|
-
@generate@ (or @gen@) method uses @create@ method of DataMapper models.
|
118
|
-
we want just a new record? Use @make@ method instead:
|
117
|
+
@generate@ (or @gen@) method uses @create@ method of DataMapper models. This means that validations are run on the model. There are two other methods you can use to creat data - @make@ to build a model that has not been saved, and @generate!@ to force saving of the model even if it is invalid (it uses @create!@ internally).
|
119
118
|
|
120
119
|
<pre>
|
121
120
|
<code>
|
122
121
|
Person.make(:valid)
|
122
|
+
Person.generate!(:invalid) # You can also use #gen!
|
123
123
|
</code>
|
124
124
|
</pre>
|
125
125
|
|
data/lib/dm-sweatshop.rb
CHANGED
data/lib/dm-sweatshop/model.rb
CHANGED
@@ -40,6 +40,22 @@ module DataMapper
|
|
40
40
|
|
41
41
|
alias_method :gen, :generate
|
42
42
|
|
43
|
+
# Same as generate except that it uses Model#create!. It
|
44
|
+
# forces invalid objects to be saved in the repository.
|
45
|
+
#
|
46
|
+
# @param name [Symbol]
|
47
|
+
# @param attributes [Hash]
|
48
|
+
#
|
49
|
+
# @api public
|
50
|
+
#
|
51
|
+
# @returns [DataMapper::Resource] added instance
|
52
|
+
def generate!(name = default_fauxture_name, attributes = {})
|
53
|
+
name, attributes = default_fauxture_name, name if name.is_a? Hash
|
54
|
+
Sweatshop.create!(self, name, attributes)
|
55
|
+
end
|
56
|
+
|
57
|
+
alias_method :gen!, :generate!
|
58
|
+
|
43
59
|
# Returns a Hash of attributes from the model map.
|
44
60
|
#
|
45
61
|
# @param name [Symbol] name of the fauxture to use
|
@@ -47,6 +47,20 @@ module DataMapper
|
|
47
47
|
instance
|
48
48
|
end
|
49
49
|
|
50
|
+
# Same as create but calls Model#create! and does save
|
51
|
+
# invalid models
|
52
|
+
#
|
53
|
+
# @param klass [Class, DataMapper::Resource]
|
54
|
+
# @param name [Symbol]
|
55
|
+
# @param attributes [Hash]
|
56
|
+
#
|
57
|
+
# @api private
|
58
|
+
#
|
59
|
+
# @returns [DataMapper::Resource] added instance
|
60
|
+
def self.create!(klass, name, attributes = {})
|
61
|
+
record(klass, name, klass.create!(attributes(klass, name).merge(attributes)))
|
62
|
+
end
|
63
|
+
|
50
64
|
# Creates an instance from given hash of attributes, saves it
|
51
65
|
# and adds it to the record map.
|
52
66
|
#
|
data/lib/dm-sweatshop/version.rb
CHANGED
@@ -1,16 +1,17 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
require 'dm-validations'
|
2
3
|
|
3
4
|
describe DataMapper::Model do
|
4
5
|
|
5
6
|
class Widget
|
6
7
|
include DataMapper::Resource
|
7
|
-
|
8
8
|
property :id, Integer, :serial => true
|
9
9
|
property :type, Discriminator
|
10
10
|
property :name, String
|
11
11
|
property :price, Integer
|
12
12
|
|
13
13
|
belongs_to :order
|
14
|
+
validates_present :price
|
14
15
|
end
|
15
16
|
|
16
17
|
class Wonket < Widget
|
@@ -117,6 +118,10 @@ describe DataMapper::Model do
|
|
117
118
|
}}
|
118
119
|
|
119
120
|
@widget = Widget.gen(:red)
|
121
|
+
|
122
|
+
Widget.fix(:blue) {{
|
123
|
+
:name => "blue"
|
124
|
+
}}
|
120
125
|
end
|
121
126
|
|
122
127
|
it "creates an object from named attributes hash" do
|
@@ -127,8 +132,25 @@ describe DataMapper::Model do
|
|
127
132
|
it "returns a saved object" do
|
128
133
|
@widget.should_not be_new_record
|
129
134
|
end
|
135
|
+
|
136
|
+
it "does not save invalid model" do
|
137
|
+
blue_widget = Widget.gen(:blue)
|
138
|
+
blue_widget.should be_new_record
|
139
|
+
end
|
130
140
|
end
|
131
141
|
|
142
|
+
describe ".generate!" do
|
143
|
+
it "saves a model even if it is invalid" do
|
144
|
+
Widget.fix(:blue) {{
|
145
|
+
:name => "blue"
|
146
|
+
}}
|
147
|
+
|
148
|
+
blue_widget = Widget.gen!(:blue)
|
149
|
+
blue_widget.should_not be_new_record
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
|
132
154
|
|
133
155
|
describe ".pick" do
|
134
156
|
before :each do
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,16 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require 'pathname'
|
2
|
+
require 'rubygems'
|
3
3
|
|
4
|
-
|
4
|
+
gem 'dm-core', '~>0.9.8'
|
5
|
+
require 'dm-core'
|
5
6
|
|
6
|
-
|
7
|
+
ROOT = Pathname(__FILE__).dirname.parent.expand_path
|
8
|
+
|
9
|
+
# use local dm-validations if running from dm-more directly
|
10
|
+
lib = ROOT.parent.join('dm-validations', 'lib').expand_path
|
11
|
+
$LOAD_PATH.unshift(lib) if lib.directory?
|
12
|
+
require 'dm-validations'
|
7
13
|
|
8
|
-
|
9
|
-
|
14
|
+
require ROOT + 'lib/dm-sweatshop'
|
15
|
+
|
16
|
+
DataMapper.setup(:default, 'sqlite3::memory:')
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dm-sweatshop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Burkert
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-01-04 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ~>
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 0.9.
|
23
|
+
version: 0.9.9
|
24
24
|
version:
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: randexp
|