sequel 0.1.9.10 → 0.1.9.11

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,14 +1,18 @@
1
+ === 0.19.11 (2007-08-24)
2
+
3
+ * Changed Dataset#set_model to allow supplying additional arguments to the model's initialize method (#35). Thanks Sunny Hirai.
4
+
1
5
  === 0.19.10 (2007-08-22)
2
6
 
3
7
  * Changed schema generation code to generate separate statements for CREATE TABLE and each CREATE INDEX (#34).
4
8
 
5
- * Refactored Dataset::SQL#field_name for better support of different quoting schemes by specific adapters.
9
+ * Refactored Dataset::SQL#field_name for better support of different field quoting standards by specific adapters.
6
10
 
7
11
  * Added #current_page_record_count for paginated datasets.
8
12
 
9
13
  * Removed Database#literal and included Dataset::SQL instead.
10
14
 
11
- * Sequel::Dataset:SQL#field_name can now take a hash (as well as #select and any method that uses #field_name) for aliasing column names. E.g. Dataset.new(nil).from(:test).select(:_qqa => 'Date').sql #=> 'SELECT _qqa AS 'Date' FROM test'.
15
+ * Sequel::Dataset:SQL#field_name can now take a hash (as well as #select and any method that uses #field_name) for aliasing column names. E.g. DB[:test].select(:_qqa => 'Date').sql #=> 'SELECT _qqa AS Date FROM test'.
12
16
 
13
17
  * Moved SingleThreadedPool to lib/sequel/connection_pool.rb.
14
18
 
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'fileutils'
6
6
  include FileUtils
7
7
 
8
8
  NAME = "sequel"
9
- VERS = "0.1.9.10"
9
+ VERS = "0.1.9.11"
10
10
  CLEAN.include ['**/.*.sw?', 'pkg/*', '.config', 'doc/*', 'coverage/*']
11
11
  RDOC_OPTS = ['--quiet', '--title', "Sequel: Concise ORM for Ruby",
12
12
  "--opname", "index.html",
@@ -179,33 +179,56 @@ module Sequel
179
179
  # class MyModel
180
180
  # def initialize(values)
181
181
  # @values = values
182
+ # ...
182
183
  # end
183
184
  # end
184
185
  #
185
186
  # dataset.set_model(MyModel)
186
187
  #
188
+ # You can also provide additional arguments to be passed to the model's
189
+ # initialize method:
190
+ #
191
+ # class MyModel
192
+ # def initialize(values, options)
193
+ # @values = values
194
+ # ...
195
+ # end
196
+ # end
197
+ #
198
+ # dataset.set_model(MyModel, :allow_delete => false)
199
+ #
187
200
  # The dataset can be made polymorphic by specifying a column name as the
188
201
  # polymorphic key and a hash mapping column values to model classes.
189
202
  #
190
203
  # dataset.set_model(:kind, {1 => Person, 2 => Business})
191
204
  #
192
- def set_model(*args)
193
- if args.empty? || (args.first == nil)
194
- # If no argument or nil is provided, the dataset is denuded
205
+ # You can also set a default model class to fall back on by specifying a
206
+ # class corresponding to nil:
207
+ #
208
+ # dataset.set_model(:kind, {nil => DefaultClass, 1 => Person, 2 => Business})
209
+ #
210
+ # To disassociate a model from the dataset, you can call the #set_model
211
+ # and specify nil as the class:
212
+ #
213
+ # dataset.set_model(nil)
214
+ #
215
+ def set_model(key, *args)
216
+ # pattern matching
217
+ case key
218
+ when nil: # set_model(nil) => no
219
+ # no argument provided, so the dataset is denuded
195
220
  @opts.merge!(:naked => true, :models => nil, :polymorphic_key => nil)
196
221
  extend_with_stock_each
197
- elsif args.size == 1
198
- # If a single argument is provided, it is regarded the model class
199
- c = args.first
200
- @opts.merge!(:naked => nil, :models => {nil => c}, :polymorphic_key => nil)
201
- extend_with_model(c)
222
+ when Class:
223
+ # isomorphic model
224
+ @opts.merge!(:naked => nil, :models => {nil => key}, :polymorphic_key => nil)
225
+ extend_with_model(key, *args)
202
226
  extend_with_destroy
203
- elsif args.size == 2
204
- # If two arguments are provided, the first is considered the
205
- # polymorphic key, and the second a hash of classes.
206
- key, hash = args
227
+ when Symbol:
228
+ # polymorphic model
229
+ hash = args.shift || raise(SequelError, "No class hash supplied for polymorphic model")
207
230
  @opts.merge!(:naked => true, :models => hash, :polymorphic_key => key)
208
- extend_with_polymorphic_model(key, hash)
231
+ extend_with_polymorphic_model(key, hash, *args)
209
232
  extend_with_destroy
210
233
  else
211
234
  raise SequelError, "Invalid parameters specified"
@@ -215,15 +238,14 @@ module Sequel
215
238
 
216
239
  private
217
240
  # Overrides the each method to convert records to model instances.
218
- def extend_with_model(c)
219
- meta_def(:model_class) {c}
241
+ def extend_with_model(c, *args)
242
+ meta_def(:make_model_instance) {|r| c.new(r, *args)}
220
243
  m = Module.new do
221
244
  def each(opts = nil, &block)
222
- c = model_class
223
245
  if opts && opts[:naked]
224
246
  fetch_rows(select_sql(opts), &block)
225
247
  else
226
- fetch_rows(select_sql(opts)) {|r| block.call(c.new(r))}
248
+ fetch_rows(select_sql(opts)) {|r| block.call(make_model_instance(r))}
227
249
  end
228
250
  end
229
251
  end
@@ -233,21 +255,18 @@ module Sequel
233
255
  # Overrides the each method to convert records to polymorphic model
234
256
  # instances. The model class is determined according to the value in the
235
257
  # key column.
236
- def extend_with_polymorphic_model(key, hash)
237
- meta_def(:model_class) {|r| hash[r[key]] || hash[nil]}
258
+ def extend_with_polymorphic_model(key, hash, *args)
259
+ meta_def(:make_model_instance) do |r|
260
+ c = hash[r[key]] || hash[nil] || \
261
+ raise(SequelError, "No matching model class for record (#{polymorphic_key} => #{r[polymorphic_key].inspect})")
262
+ c.new(r, *args)
263
+ end
238
264
  m = Module.new do
239
265
  def each(opts = nil, &block)
240
266
  if opts && opts[:naked]
241
267
  fetch_rows(select_sql(opts), &block)
242
268
  else
243
- fetch_rows(select_sql(opts)) do |r|
244
- c = model_class(r)
245
- if c
246
- block.call(c.new(r))
247
- else
248
- raise SequelError, "No matching model class for record (#{polymorphic_key} = #{r[polymorphic_key].inspect})"
249
- end
250
- end
269
+ fetch_rows(select_sql(opts)) {|r| block.call(make_model_instance(r))}
251
270
  end
252
271
  end
253
272
  end
@@ -1150,35 +1150,36 @@ context "Dataset#set_model" do
1150
1150
  setup do
1151
1151
  @c = Class.new(Sequel::Dataset) do
1152
1152
  def fetch_rows(sql, &block)
1153
- (1..10).each(&block)
1153
+ # yield a hash with kind as the 1 bit of a number
1154
+ (1..10).each {|i| block.call({:kind => i[0]})}
1154
1155
  end
1155
1156
  end
1156
1157
  @dataset = @c.new(nil).from(:items)
1157
1158
  @m = Class.new do
1158
- attr_accessor :c
1159
- def initialize(c); @c = c; end
1160
- def ==(o); @c == o.c; end
1159
+ attr_accessor :c, :args
1160
+ def initialize(c, *args); @c = c; @args = args; end
1161
+ def ==(o); (@c == o.c) && (@args = o.args); end
1161
1162
  end
1162
1163
  end
1163
1164
 
1164
1165
  specify "should clear the models hash and restore the stock #each if nil is specified" do
1165
1166
  @dataset.set_model(@m)
1166
1167
  @dataset.set_model(nil)
1167
- @dataset.first.should == 1
1168
+ @dataset.first.should == {:kind => 1}
1168
1169
  @dataset.model_classes.should be_nil
1169
1170
  end
1170
1171
 
1171
1172
  specify "should clear the models hash and restore the stock #each if nothing is specified" do
1172
1173
  @dataset.set_model(@m)
1173
- @dataset.set_model
1174
- @dataset.first.should == 1
1174
+ @dataset.set_model(nil)
1175
+ @dataset.first.should == {:kind => 1}
1175
1176
  @dataset.model_classes.should be_nil
1176
1177
  end
1177
1178
 
1178
1179
  specify "should alter #each to provide model instances" do
1179
- @dataset.first.should == 1
1180
+ @dataset.first.should == {:kind => 1}
1180
1181
  @dataset.set_model(@m)
1181
- @dataset.first.should == @m.new(1)
1182
+ @dataset.first.should == @m.new({:kind => 1})
1182
1183
  end
1183
1184
 
1184
1185
  specify "should extend the dataset with a #destroy method" do
@@ -1193,20 +1194,43 @@ context "Dataset#set_model" do
1193
1194
  @dataset.opts[:naked].should be_nil
1194
1195
  end
1195
1196
 
1197
+ specify "should send additional arguments to the models' initialize method" do
1198
+ @dataset.set_model(@m, 7, 6, 5)
1199
+ @dataset.first.should == @m.new({:kind => 1}, 7, 6, 5)
1200
+ end
1201
+
1196
1202
  specify "should provide support for polymorphic model instantiation" do
1197
1203
  @m1 = Class.new(@m)
1198
1204
  @m2 = Class.new(@m)
1199
- @dataset.set_model(0, 0 => @m1, 1 => @m2)
1205
+ @dataset.set_model(:kind, 0 => @m1, 1 => @m2)
1206
+ @dataset.opts[:polymorphic_key].should == :kind
1200
1207
  all = @dataset.all
1201
1208
  all[0].class.should == @m2
1202
1209
  all[1].class.should == @m1
1203
1210
  all[2].class.should == @m2
1204
1211
  all[3].class.should == @m1
1205
1212
  #...
1213
+
1214
+ # denude model
1215
+ @dataset.set_model(nil)
1216
+ @dataset.first.should == {:kind => 1}
1217
+ end
1218
+
1219
+ specify "should send additional arguments for polymorphic models as well" do
1220
+ @m1 = Class.new(@m)
1221
+ @m2 = Class.new(@m)
1222
+ @dataset.set_model(:kind, {0 => @m1, 1 => @m2}, :hey => :wow)
1223
+ all = @dataset.all
1224
+ all[0].class.should == @m2; all[0].args.should == [{:hey => :wow}]
1225
+ all[1].class.should == @m1; all[1].args.should == [{:hey => :wow}]
1226
+ all[2].class.should == @m2; all[2].args.should == [{:hey => :wow}]
1227
+ all[3].class.should == @m1; all[3].args.should == [{:hey => :wow}]
1206
1228
  end
1207
1229
 
1208
- specify "should raise an error if more than two arguments are supplied" do
1209
- proc {@dataset.set_model(1, 2, 3)}.should raise_error(SequelError)
1230
+ specify "should raise for invalid parameters" do
1231
+ proc {@dataset.set_model('kind')}.should raise_error(SequelError)
1232
+ proc {@dataset.set_model(0)}.should raise_error(SequelError)
1233
+ proc {@dataset.set_model(:kind)}.should raise_error(SequelError) # no hash given
1210
1234
  end
1211
1235
  end
1212
1236
 
@@ -1238,7 +1262,7 @@ context "Dataset#model_classes" do
1238
1262
  specify "should return the polymorphic hash for a polymorphic model dataset" do
1239
1263
  @m1 = Class.new(@m)
1240
1264
  @m2 = Class.new(@m)
1241
- @dataset.set_model(0, 0 => @m1, 1 => @m2)
1265
+ @dataset.set_model(:key, 0 => @m1, 1 => @m2)
1242
1266
  @dataset.model_classes.should == {0 => @m1, 1 => @m2}
1243
1267
  end
1244
1268
  end
@@ -1295,7 +1319,7 @@ context "A polymorphic model dataset" do
1295
1319
  setup do
1296
1320
  @c = Class.new(Sequel::Dataset) do
1297
1321
  def fetch_rows(sql, &block)
1298
- (1..10).each(&block)
1322
+ (1..10).each {|i| block.call(:bit => i[0])}
1299
1323
  end
1300
1324
  end
1301
1325
  @dataset = @c.new(nil).from(:items)
@@ -1308,7 +1332,7 @@ context "A polymorphic model dataset" do
1308
1332
 
1309
1333
  specify "should use a nil key in the polymorphic hash to specify the default model class" do
1310
1334
  @m2 = Class.new(@m)
1311
- @dataset.set_model(0, nil => @m, 1 => @m2)
1335
+ @dataset.set_model(:bit, nil => @m, 1 => @m2)
1312
1336
  all = @dataset.all
1313
1337
  all[0].class.should == @m2
1314
1338
  all[1].class.should == @m
@@ -1319,13 +1343,13 @@ context "A polymorphic model dataset" do
1319
1343
 
1320
1344
  specify "should raise SequelError if no suitable class is found in the polymorphic hash" do
1321
1345
  @m2 = Class.new(@m)
1322
- @dataset.set_model(0, 1 => @m2)
1346
+ @dataset.set_model(:bit, 1 => @m2)
1323
1347
  proc {@dataset.all}.should raise_error(SequelError)
1324
1348
  end
1325
1349
 
1326
1350
  specify "should supply naked records if the naked option is specified" do
1327
- @dataset.set_model(0, nil => @m)
1328
- @dataset.each(:naked => true) {|r| r.class.should == Fixnum}
1351
+ @dataset.set_model(:bit, nil => @m)
1352
+ @dataset.each(:naked => true) {|r| r.class.should == Hash}
1329
1353
  end
1330
1354
  end
1331
1355
 
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: sequel
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.9.10
7
- date: 2007-08-23 00:00:00 +03:00
6
+ version: 0.1.9.11
7
+ date: 2007-08-24 00:00:00 +03:00
8
8
  summary: Lightweight ORM library for Ruby
9
9
  require_paths:
10
10
  - lib