Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix for initialization process.

  • Loading branch information...
commit d528fddf417fe52d3d9985f5105acc74df042f0d 1 parent 1a6623e
@paulcarey authored
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
Please sign in to comment.
Something went wrong with that request. Please try again.