Browse files

Allow Cequel::Model classes to implement #generate_key

Sets the ID early in initialization, before setting attributes from
hash/block
  • Loading branch information...
1 parent 88aab1a commit 4466c3024272dacee40b4408c760666b1a4799f5 @outoftime outoftime committed Aug 10, 2012
Showing with 21 additions and 0 deletions.
  1. +5 −0 lib/cequel/model/properties.rb
  2. +4 −0 spec/examples/model/properties_spec.rb
  3. +12 −0 spec/models/comment.rb
View
5 lib/cequel/model/properties.rb
@@ -78,6 +78,7 @@ def type_column
def initialize(attributes = {})
super()
+ @_cequel.key = generate_key
self.class.columns.each do |column|
default = column.default
@_cequel.attributes[column.name] = default unless default.nil?
@@ -122,6 +123,10 @@ def read_attribute(column_name)
@_cequel.attributes[column_name.to_sym]
end
+ def generate_key
+ # Noop -- model classes can override if desired
+ end
+
end
end
View
4 spec/examples/model/properties_spec.rb
@@ -74,4 +74,8 @@
Post.new(:id => 1).should_not == Blog.new(:id => 1)
end
+ it 'should use #generate_key method when implemented' do
+ Comment.new.id.should be_a(SimpleUUID::UUID)
+ end
+
end
View
12 spec/models/comment.rb
@@ -0,0 +1,12 @@
+class Comment
+ include Cequel::Model
+
+ key :id, :uuid
+ column :body, :text
+
+ private
+
+ def generate_key
+ SimpleUUID::UUID.new
+ end
+end

0 comments on commit 4466c30

Please sign in to comment.