Permalink
Browse files

Fix for initialization process.

  • Loading branch information...
1 parent 1a6623e commit d528fddf417fe52d3d9985f5105acc74df042f0d @paulcarey committed Apr 27, 2010
Showing with 35 additions and 2 deletions.
  1. +6 −2 lib/relaxdb/document.rb
  2. +11 −0 spec/document_spec.rb
  3. +18 −0 spec/spec_models.rb
View
8 lib/relaxdb/document.rb
@@ -139,6 +139,11 @@ def initialize(hash={})
@save_list = []
@validation_skip_list = []
+ # hash.dup because assigning references properties and defaults both
+ # modify the internal representation - @data. This messes with the
+ # iterator below that assigns vals to @data.
+ params = hash.dup
+
# If there's no rev, it's a new document
if hash["_rev"].nil?
# Clients may use symbols as keys so convert all to strings first.
@@ -148,7 +153,6 @@ def initialize(hash={})
end
unless @data["_id"]
- # TODO - use uuids from CouchDB (sequential) and convert to base 36
@data["_id"] = UuidGenerator.uuid
end
@@ -163,7 +167,7 @@ def initialize(hash={})
end
end
- @data.each do |key, val|
+ params.each do |key, val|
send("#{key}=".to_sym, val)
end
View
11 spec/document_spec.rb
@@ -540,4 +540,15 @@ def required; raise; end;
end
+ describe "initialization process" do
+
+ it "should not modify internal state unexpectedly" do
+ a = Atom.new
+ c = Contrived.new :context => a
+ c.context_count.should == 1
+ c.foo.should == 10
+ end
+
+ end
+
end
View
18 spec/spec_models.rb
@@ -158,6 +158,24 @@ class RichDescendant < Descendant
:derived => [:ukulele, lambda { |p, o| o.ukulele.name } ]
end
+class Contrived < RelaxDB::Document
+
+ attr_accessor :context_count
+ def initialize params = {}
+ @context_count = 0
+ super
+ end
+
+ property :foo,
+ :default => 5,
+ :derived => [
+ :context,
+ lambda { |f, c| c.context_count += 1; 10 }
+ ]
+
+ references :context
+end
+
module Inh
class X < RelaxDB::Document; end

0 comments on commit d528fdd

Please sign in to comment.