Created
September 8, 2009 23:06
-
-
Save ryanking/183311 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/cassandra_object/identity/natural_key_factory.rb b/lib/cassandra_object/identity/natural_key_factory.rb | |
index 69dec5b..91859ee 100644 | |
--- a/lib/cassandra_object/identity/natural_key_factory.rb | |
+++ b/lib/cassandra_object/identity/natural_key_factory.rb | |
@@ -35,7 +35,7 @@ module CassandraObject | |
end | |
def next_key(object) | |
- NaturalKey.new(attributes.map { |a| object[a.to_s] }.join(separator)) | |
+ NaturalKey.new(attributes.map { |a| object.attributes[a.to_s] }.join(separator)) | |
end | |
def parse(paramized_key) | |
diff --git a/lib/cassandra_object/persistence.rb b/lib/cassandra_object/persistence.rb | |
index aec6e7e..6bd2006 100644 | |
--- a/lib/cassandra_object/persistence.rb | |
+++ b/lib/cassandra_object/persistence.rb | |
@@ -52,7 +52,7 @@ module CassandraObject | |
end | |
def write(key, attributes, schema_version) | |
- returning(key || next_key(attributes)) do |key| | |
+ returning(key) do |key| | |
connection.insert(column_family, key.to_s, encode_columns_hash(attributes, schema_version)) | |
end | |
end | |
@@ -88,8 +88,8 @@ module CassandraObject | |
run_callbacks :before_save | |
changed_attributes = changed.inject({}) { |h, n| h[n] = read_attribute(n); h } | |
- returned_key = self.class.write(key, changed_attributes, schema_version) | |
- @key ||= returned_key | |
+ @key ||= self.class.next_key(self) | |
+ self.class.write(key, changed_attributes, schema_version) | |
run_callbacks :after_save | |
run_callbacks :after_create if was_new_record | |
@new_record = false | |
diff --git a/test/fixture_models.rb b/test/fixture_models.rb | |
index fb63127..f01fe2e 100644 | |
--- a/test/fixture_models.rb | |
+++ b/test/fixture_models.rb | |
@@ -52,3 +52,8 @@ class Payment < CassandraObject::Base | |
end | |
MockRecord = Struct.new(:key) | |
+ | |
+class Person < CassandraObject::Base | |
+ attribute :name, :type => String | |
+ attribute :age, :type => Integer | |
+end | |
diff --git a/test/identity/natural_key_factory_test.rb b/test/identity/natural_key_factory_test.rb | |
index 0ef115d..9275ffa 100644 | |
--- a/test/identity/natural_key_factory_test.rb | |
+++ b/test/identity/natural_key_factory_test.rb | |
@@ -5,10 +5,11 @@ module Identity | |
context "With one attribute" do | |
setup do | |
@key_factory = CassandraObject::Identity::NaturalKeyFactory.new :attributes => :name | |
+ @james = Person.new("name" => "james") | |
end | |
should "have a key whose string and param value is the value of that attribute" do | |
- @key = @key_factory.next_key({"name" => "james"}) | |
+ @key = @key_factory.next_key(@james) | |
assert_equal "james", @key.to_s | |
assert_equal "james", @key.to_param | |
@@ -32,10 +33,11 @@ module Identity | |
context "With multiple attributes" do | |
setup do | |
@key_factory = CassandraObject::Identity::NaturalKeyFactory.new :attributes => [:name, :age] | |
+ @james = Person.new("name" => "james", "age" => 23) | |
end | |
should "create a key whose string value is the two values, joined with a separator" do | |
- key = @key_factory.next_key({"name" => "james", "age" => 23}) | |
+ key = @key_factory.next_key(@james) | |
assert_equal "james-23", key.to_s | |
assert_equal "james-23", key.to_param | |
@@ -60,10 +62,11 @@ module Identity | |
setup do | |
@key_factory = CassandraObject::Identity::NaturalKeyFactory.new :attributes => [:name, :age], | |
:separator => "#" | |
+ @james = Person.new("name" => "james", "age" => 23) | |
end | |
should "join the attributes with the custom separator" do | |
- key = @key_factory.next_key({"name" => "james", "age" => 23}) | |
+ key = @key_factory.next_key(@james) | |
assert_equal "james#23", key.to_s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment