sequel_core 1.0.4.1 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ === 1.0.5 (2008-01-25)
2
+
3
+ * Added support for instantiating models by using the load constructor method.
4
+
1
5
  === 1.0.4.1 (2008-01-24)
2
6
 
3
7
  * Fixed bin/sequel to require sequel_model if available.
data/Rakefile CHANGED
@@ -9,7 +9,7 @@ include FileUtils
9
9
  # Configuration
10
10
  ##############################################################################
11
11
  NAME = "sequel_core"
12
- VERS = "1.0.4.1"
12
+ VERS = "1.0.5"
13
13
  CLEAN.include ["**/.*.sw?", "pkg/*", ".config", "doc/*", "coverage/*"]
14
14
  RDOC_OPTS = [
15
15
  "--quiet",
@@ -235,16 +235,32 @@ module Sequel
235
235
  when Class
236
236
  # isomorphic model
237
237
  @opts.merge!(:naked => nil, :models => {nil => key}, :polymorphic_key => nil)
238
- set_row_proc {|h| key.new(h, *args)}
238
+ if key.respond_to?(:load)
239
+ # the class has a values setter method, so we use it
240
+ set_row_proc {|h| key.load(h, *args)}
241
+ else
242
+ # otherwise we just pass the hash to the constructor
243
+ set_row_proc {|h| key.new(h, *args)}
244
+ end
239
245
  extend_with_destroy
240
246
  when Symbol
241
247
  # polymorphic model
242
248
  hash = args.shift || raise(ArgumentError, "No class hash supplied for polymorphic model")
243
249
  @opts.merge!(:naked => true, :models => hash, :polymorphic_key => key)
244
- set_row_proc do |h|
245
- c = hash[h[key]] || hash[nil] || \
246
- raise(Error, "No matching model class for record (#{polymorphic_key} => #{h[polymorphic_key].inspect})")
247
- c.new(h, *args)
250
+ if hash.values.first.respond_to?(:load)
251
+ # the class has a values setter method, so we use it
252
+ set_row_proc do |h|
253
+ c = hash[h[key]] || hash[nil] || \
254
+ raise(Error, "No matching model class for record (#{polymorphic_key} => #{h[polymorphic_key].inspect})")
255
+ c.load(h, *args)
256
+ end
257
+ else
258
+ # otherwise we just pass the hash to the constructor
259
+ set_row_proc do |h|
260
+ c = hash[h[key]] || hash[nil] || \
261
+ raise(Error, "No matching model class for record (#{polymorphic_key} => #{h[polymorphic_key].inspect})")
262
+ c.new(h, *args)
263
+ end
248
264
  end
249
265
  extend_with_destroy
250
266
  else
@@ -305,7 +305,7 @@ context "String#to_date" do
305
305
  end
306
306
 
307
307
  specify "should raise Error::InvalidValue for an invalid date" do
308
- proc {'0000-00-00'.to_time}.should raise_error(Sequel::Error::InvalidValue)
308
+ proc {'0000-00-00'.to_date}.should raise_error(Sequel::Error::InvalidValue)
309
309
  end
310
310
  end
311
311
 
data/spec/dataset_spec.rb CHANGED
@@ -1736,6 +1736,50 @@ context "A polymorphic model dataset" do
1736
1736
  end
1737
1737
  end
1738
1738
 
1739
+ context "A dataset with associated model class(es)" do
1740
+ setup do
1741
+ @c = Class.new(Sequel::Dataset) do
1742
+ def fetch_rows(sql, &block)
1743
+ block.call({:x => 1, :y => 2})
1744
+ end
1745
+ end
1746
+ @dataset = @c.new(nil).from(:items)
1747
+ @m1 = Class.new do
1748
+ attr_accessor :v
1749
+ def initialize(v); @v = v; end
1750
+ end
1751
+ @m2 = Class.new do
1752
+ attr_accessor :v, :vv
1753
+ def initialize(v = nil); @v = v; end
1754
+ def self.load(v); o = new(nil); o.vv = v; o; end
1755
+ end
1756
+ @m3 = Class.new(@m2)
1757
+ end
1758
+
1759
+ specify "should instantiate an instance by passing the record hash as argument" do
1760
+ @dataset.set_model(@m1)
1761
+ o = @dataset.first
1762
+ o.class.should == @m1
1763
+ o.v.should == {:x => 1, :y => 2}
1764
+ end
1765
+
1766
+ specify "should use the .load constructor if available" do
1767
+ @dataset.set_model(@m2)
1768
+ o = @dataset.first
1769
+ o.class.should == @m2
1770
+ o.v.should == nil
1771
+ o.vv.should == {:x => 1, :y => 2}
1772
+ end
1773
+
1774
+ specify "should use the .load constructor also for polymorphic datasets" do
1775
+ @dataset.set_model(:y, 1 => @m2, 2 => @m3)
1776
+ o = @dataset.first
1777
+ o.class.should == @m3
1778
+ o.v.should == nil
1779
+ o.vv.should == {:x => 1, :y => 2}
1780
+ end
1781
+ end
1782
+
1739
1783
  context "Dataset#destroy" do
1740
1784
  setup do
1741
1785
  db = Object.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4.1
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-01-24 00:00:00 +02:00
12
+ date: 2008-01-25 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency