dupe 0.4.4 → 0.4.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +21 -1
- data/lib/dupe/dupe.rb +23 -1
- data/lib/dupe/schema.rb +17 -1
- data/spec/lib_specs/dupe_spec.rb +22 -1
- data/spec/lib_specs/schema_spec.rb +20 -1
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -192,7 +192,7 @@ Now, every time we create a book, it will get assigned a random ISBN number:
|
|
192
192
|
irb# b = Dupe.create :book
|
193
193
|
==> <#Duped::Book author=nil title="Untitled" id=2 isbn=606472>
|
194
194
|
|
195
|
-
Another common use of this feature is for associations. Lets suppose we'd like to make sure that a book always has a genre, but a genre should be
|
195
|
+
Another common use of this feature is for associations. Lets suppose we'd like to make sure that a book always has a genre, but a genre should be its own resource. We can accomplish that by taking advantage of Dupe's "find_or_create" method:
|
196
196
|
|
197
197
|
irb# Dupe.define :book do |attrs|
|
198
198
|
--# attrs.title 'Untitled'
|
@@ -260,6 +260,26 @@ Now, let's create a book:
|
|
260
260
|
irb# b.publish_date.class
|
261
261
|
==> Date
|
262
262
|
|
263
|
+
|
264
|
+
=== Uniquify attributes
|
265
|
+
|
266
|
+
If you'd just like to make sure that some attributes get a unique value (probably), then you can use the uniquify
|
267
|
+
method:
|
268
|
+
|
269
|
+
irb# Dupe.define :book do |attrs|
|
270
|
+
--# attrs.uniquify :title, :genre, :author
|
271
|
+
--# end
|
272
|
+
|
273
|
+
Now, Dupe will do its best to assign unique values to the :title, :genre, and :author attributes on
|
274
|
+
any records it creates:
|
275
|
+
|
276
|
+
irb# b = Dupe.create :book
|
277
|
+
==> <#Duped::Book author="book 1 author" title="book 1 title" genre="book 1 genre" id=1>
|
278
|
+
|
279
|
+
irb# b2 = Dupe.create :book, :title => 'Rooby'
|
280
|
+
==> <#Duped::Book author="book 2 author" title="Rooby" genre="book 2 genre" id=2>
|
281
|
+
|
282
|
+
|
263
283
|
=== Callbacks
|
264
284
|
|
265
285
|
Suppose we'd like to make sure that our books get a unique label. We can accomplish that with an after_create callback:
|
data/lib/dupe/dupe.rb
CHANGED
@@ -51,7 +51,7 @@ class Dupe
|
|
51
51
|
# irb# b = Dupe.create :book
|
52
52
|
# ==> <#Duped::Book author=nil title="Untitled" id=2 isbn=606472>
|
53
53
|
#
|
54
|
-
# Another common use of this feature is for associations. Lets suppose we'd like to make sure that a book always has a genre, but a genre should be
|
54
|
+
# Another common use of this feature is for associations. Lets suppose we'd like to make sure that a book always has a genre, but a genre should be its own resource. We can accomplish that by taking advantage of Dupe's "find_or_create" method:
|
55
55
|
#
|
56
56
|
# irb# Dupe.define :book do |attrs|
|
57
57
|
# --# attrs.title 'Untitled'
|
@@ -118,7 +118,29 @@ class Dupe
|
|
118
118
|
#
|
119
119
|
# irb# b.publish_date.class
|
120
120
|
# ==> Date
|
121
|
+
#
|
122
|
+
#
|
123
|
+
#
|
124
|
+
# === Uniquify attributes
|
125
|
+
#
|
126
|
+
# If you'd just like to make sure that some attributes get a unique value, then you can use the uniquify
|
127
|
+
# method:
|
128
|
+
#
|
129
|
+
# irb# Dupe.define :book do |attrs|
|
130
|
+
# --# attrs.uniquify :title, :genre, :author
|
131
|
+
# --# end
|
121
132
|
#
|
133
|
+
# Now, Dupe will do its best to assign unique values to the :title, :genre, and :author attributes on
|
134
|
+
# any records it creates:
|
135
|
+
#
|
136
|
+
# irb# b = Dupe.create :book
|
137
|
+
# ==> <#Duped::Book author="book 1 author" title="book 1 title" genre="book 1 genre" id=1>
|
138
|
+
#
|
139
|
+
# irb# b2 = Dupe.create :book, :title => 'Rooby'
|
140
|
+
# ==> <#Duped::Book author="book 2 author" title="Rooby" genre="book 2 genre" id=2>
|
141
|
+
#
|
142
|
+
#
|
143
|
+
#
|
122
144
|
# === Callbacks
|
123
145
|
#
|
124
146
|
# Suppose we'd like to make sure that our books get a unique label. We can accomplish that with an after_create callback:
|
data/lib/dupe/schema.rb
CHANGED
@@ -31,6 +31,22 @@ class Dupe
|
|
31
31
|
|
32
32
|
@after_create_callbacks << block
|
33
33
|
end
|
34
|
+
|
35
|
+
def uniquify(*args)
|
36
|
+
raise ArgumentError, "You must pass at least one attribute to uniquify." if args.empty?
|
37
|
+
raise ArgumentError, "You may only pass symbols to uniquify." unless all_members_of_class(args, Symbol)
|
38
|
+
|
39
|
+
args.each do |attribute|
|
40
|
+
after_create do |record|
|
41
|
+
record[attribute] = "#{record.__model__.name} #{record.id} #{attribute}" unless record[attribute]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
def all_members_of_class(ary, klass)
|
48
|
+
ary.inject(true) {|bool, v| bool && v.kind_of?(klass)}
|
49
|
+
end
|
34
50
|
end
|
35
51
|
end
|
36
|
-
end
|
52
|
+
end
|
data/spec/lib_specs/dupe_spec.rb
CHANGED
@@ -213,6 +213,27 @@ describe Dupe do
|
|
213
213
|
bs.last['test'].should == nil
|
214
214
|
end
|
215
215
|
end
|
216
|
+
|
217
|
+
describe "create resources with unique attributes" do
|
218
|
+
before do
|
219
|
+
Dupe.define :book do |book|
|
220
|
+
book.uniquify :title, :author, :genre
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
it "should uniquify the appropriate columns if they don't already have values" do
|
225
|
+
b = Dupe.create :book
|
226
|
+
b.title.should == "book 1 title"
|
227
|
+
b.author.should == "book 1 author"
|
228
|
+
b.genre.should == "book 1 genre"
|
229
|
+
|
230
|
+
b = Dupe.create :book, :title => 'Rooby Rocks', :isbn => 1
|
231
|
+
b.title.should == 'Rooby Rocks'
|
232
|
+
b.author.should == "book 2 author"
|
233
|
+
b.genre.should == "book 2 genre"
|
234
|
+
b.isbn.should == 1
|
235
|
+
end
|
236
|
+
end
|
216
237
|
|
217
238
|
describe "find" do
|
218
239
|
before do
|
@@ -340,4 +361,4 @@ describe Dupe do
|
|
340
361
|
end
|
341
362
|
|
342
363
|
end
|
343
|
-
end
|
364
|
+
end
|
@@ -87,4 +87,23 @@ describe Dupe::Model::Schema do
|
|
87
87
|
end
|
88
88
|
|
89
89
|
end
|
90
|
-
|
90
|
+
|
91
|
+
describe "#uniquify" do
|
92
|
+
before do
|
93
|
+
@schema = Dupe::Model::Schema.new
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should only accept a list of symbols" do
|
97
|
+
proc { @schema.uniquify }.should raise_error(ArgumentError, "You must pass at least one attribute to uniquify.")
|
98
|
+
proc { @schema.uniquify :hash => 'value' }.should raise_error(ArgumentError, "You may only pass symbols to uniquify.")
|
99
|
+
proc { @schema.uniquify :one, :two}.should_not raise_error
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should create after_create_callbacks for each symbol passed to it" do
|
103
|
+
@schema.after_create_callbacks.should be_empty
|
104
|
+
@schema.uniquify :title, :label
|
105
|
+
@schema.after_create_callbacks.length.should == 2
|
106
|
+
@schema.after_create_callbacks.first.should be_kind_of(Proc)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dupe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Parker
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-25 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|