Permalink
Browse files

Added ability to pass options to mutators. Thanks to @kierangraham

Model#save, Model#create, Model#update and Model#delete now pass options to
the underlying calls. It is useful for setting TTL, CAS, format etc.

Change-Id: I542361e644eb020c9319e866aac819c8db3391a0
Reviewed-on: http://review.couchbase.org/21342
Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Reviewed-by: Marty Schoch <marty.schoch@gmail.com>
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
  • Loading branch information...
1 parent 3ca8e4a commit 745b530658a820f2ca9ddcbb3e07fd4ab0d805a6 @avsej avsej committed Oct 4, 2012
Showing with 19 additions and 11 deletions.
  1. +19 −11 lib/couchbase/model.rb
@@ -460,7 +460,7 @@ def initialize(attrs = {})
# @example Create the instance of the Post model
# p = Post.new(:title => 'Hello world', :draft => true)
# p.create
- def create
+ def create(options = {})
@id ||= Couchbase::Model::UUID.generator.next(1, model.thread_storage[:uuid_algorithm])
value = @_raw ? @_raw : attributes_with_values
unless @meta
@@ -469,22 +469,28 @@ def create
@meta = @meta.with_indifferent_access
end
end
- @meta['cas'] = model.bucket.add(@id, value, model.defaults)
+ @meta['cas'] = model.bucket.add(@id, value, model.defaults.merge(options))
self
end
# Create or update this object based on the state of #new?.
#
# @since 0.0.1
#
- # @param [Bignum] cas CAS value
+ # @param [Hash] options options for operation, see
+ # {{Couchbase::Bucket#set}}
# @return [Couchbase::Model] The saved object
#
# @example Update the Post model
# p = Post.find('hello-world')
# p.draft = false
# p.save
- def save(cas = nil)
+ #
+ # @example Use CAS value for optimistic lock
+ # p = Post.find('hello-world')
+ # p.draft = false
+ # p.save('cas' => p.meta['cas'])
+ def save(options = {})
if respond_to?(:valid?) && !valid?
raise Couchbase::Error::RecordInvalid.new(self)
end
@@ -496,7 +502,7 @@ def save(cas = nil)
@meta = @meta.with_indifferent_access
end
end
- @meta['cas'] = model.bucket.replace(@id, value, model.defaults.merge(:cas => cas))
+ @meta['cas'] = model.bucket.replace(@id, value, model.defaults.merge(options))
self
end
@@ -506,11 +512,12 @@ def save(cas = nil)
#
# @param [Hash] attrs Attribute value pairs to use for the updated
# version
- # @param [Bignum] cas CAS value
+ # @param [Hash] options options for operation, see
+ # {{Couchbase::Bucket#set}}
# @return [Couchbase::Model] The updated object
- def update(attrs, cas = nil)
+ def update(attrs, options = {})
update_attributes(attrs)
- save(cas)
+ save(options)
end
# Delete this object from the bucket
@@ -519,15 +526,16 @@ def update(attrs, cas = nil)
#
# @note This method will reset +id+ attribute
#
- # @param [Bignum] cas CAS value
+ # @param [Hash] options options for operation, see
+ # {{Couchbase::Bucket#delete}}
# @return [Couchbase::Model] Returns a reference of itself.
#
# @example Delete the Post model
# p = Post.find('hello-world')
# p.delete
- def delete(cas = nil)
+ def delete(options = {})
raise Couchbase::Error::MissingId, "missing id attribute" unless @id
- model.bucket.delete(@id)
+ model.bucket.delete(@id, options)
@id = nil
@meta = nil
self

0 comments on commit 745b530

Please sign in to comment.