couchbase-model 0.4.1 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.markdown CHANGED
@@ -1,3 +1,14 @@
1
+ ## 0.4.3 / 2012-10-17
2
+
3
+ * Make #to_param aware about keys
4
+
5
+ ## 0.4.2 / 2012-10-17
6
+
7
+ * Update CAS value after mutation
8
+ * Added ability to pass options to mutators. Thanks to @kierangraham
9
+ * Always try to include Rails stuff into model
10
+ * Use key if id is nil (makes sense for some view results)
11
+
1
12
  ## 0.4.1 / 2012-09-26
2
13
 
3
14
  * Put support notes in README
@@ -20,10 +20,4 @@ require 'couchbase/model'
20
20
  # If we are using Rails then we will include the Couchbase railtie.
21
21
  if defined?(Rails)
22
22
  require "couchbase/railtie"
23
-
24
- class Couchbase::Model
25
- extend ActiveModel::Naming
26
- include ActiveModel::Conversion
27
- include ActiveModel::Validations
28
- end
29
23
  end
@@ -460,10 +460,16 @@ module Couchbase
460
460
  # @example Create the instance of the Post model
461
461
  # p = Post.new(:title => 'Hello world', :draft => true)
462
462
  # p.create
463
- def create
463
+ def create(options = {})
464
464
  @id ||= Couchbase::Model::UUID.generator.next(1, model.thread_storage[:uuid_algorithm])
465
465
  value = @_raw ? @_raw : attributes_with_values
466
- model.bucket.add(@id, value, model.defaults)
466
+ unless @meta
467
+ @meta = {}
468
+ if @meta.respond_to?(:with_indifferent_access)
469
+ @meta = @meta.with_indifferent_access
470
+ end
471
+ end
472
+ @meta['cas'] = model.bucket.add(@id, value, model.defaults.merge(options))
467
473
  self
468
474
  end
469
475
 
@@ -471,20 +477,32 @@ module Couchbase
471
477
  #
472
478
  # @since 0.0.1
473
479
  #
474
- # @param [Bignum] cas CAS value
480
+ # @param [Hash] options options for operation, see
481
+ # {{Couchbase::Bucket#set}}
475
482
  # @return [Couchbase::Model] The saved object
476
483
  #
477
484
  # @example Update the Post model
478
485
  # p = Post.find('hello-world')
479
486
  # p.draft = false
480
487
  # p.save
481
- def save(cas = nil)
488
+ #
489
+ # @example Use CAS value for optimistic lock
490
+ # p = Post.find('hello-world')
491
+ # p.draft = false
492
+ # p.save('cas' => p.meta['cas'])
493
+ def save(options = {})
482
494
  if respond_to?(:valid?) && !valid?
483
495
  raise Couchbase::Error::RecordInvalid.new(self)
484
496
  end
485
497
  return create unless meta
486
498
  value = @_raw ? @_raw : attributes_with_values
487
- model.bucket.replace(@id, value, model.defaults.merge(:cas => cas))
499
+ unless @meta
500
+ @meta = {}
501
+ if @meta.respond_to?(:with_indifferent_access)
502
+ @meta = @meta.with_indifferent_access
503
+ end
504
+ end
505
+ @meta['cas'] = model.bucket.replace(@id, value, model.defaults.merge(options))
488
506
  self
489
507
  end
490
508
 
@@ -494,11 +512,12 @@ module Couchbase
494
512
  #
495
513
  # @param [Hash] attrs Attribute value pairs to use for the updated
496
514
  # version
497
- # @param [Bignum] cas CAS value
515
+ # @param [Hash] options options for operation, see
516
+ # {{Couchbase::Bucket#set}}
498
517
  # @return [Couchbase::Model] The updated object
499
- def update(attrs, cas = nil)
518
+ def update(attrs, options = {})
500
519
  update_attributes(attrs)
501
- save(cas)
520
+ save(options)
502
521
  end
503
522
 
504
523
  # Delete this object from the bucket
@@ -507,16 +526,18 @@ module Couchbase
507
526
  #
508
527
  # @note This method will reset +id+ attribute
509
528
  #
510
- # @param [Bignum] cas CAS value
529
+ # @param [Hash] options options for operation, see
530
+ # {{Couchbase::Bucket#delete}}
511
531
  # @return [Couchbase::Model] Returns a reference of itself.
512
532
  #
513
533
  # @example Delete the Post model
514
534
  # p = Post.find('hello-world')
515
535
  # p.delete
516
- def delete(cas = nil)
536
+ def delete(options = {})
517
537
  raise Couchbase::Error::MissingId, "missing id attribute" unless @id
518
- model.bucket.delete(@id)
538
+ model.bucket.delete(@id, options)
519
539
  @id = nil
540
+ @meta = nil
520
541
  self
521
542
  end
522
543
 
@@ -713,6 +734,10 @@ module Couchbase
713
734
 
714
735
  if defined?(::Rails)
715
736
  extend ActiveModel::Callbacks
737
+ extend ActiveModel::Naming
738
+ include ActiveModel::Conversion
739
+ include ActiveModel::Validations
740
+
716
741
  define_model_callbacks :create, :update, :delete, :save
717
742
  [:save, :create, :update, :delete].each do |meth|
718
743
  class_eval <<-EOC
@@ -726,6 +751,18 @@ module Couchbase
726
751
  end
727
752
  end
728
753
 
754
+ # Redefine (if exists) #to_key to use #key if #id is missing
755
+ def to_key
756
+ keys = [id || key]
757
+ keys.empty? ? nil : keys
758
+ end
759
+
760
+ def to_param
761
+ keys = to_key
762
+ if keys && !keys.empty?
763
+ keys.join("-")
764
+ end
765
+ end
729
766
  end
730
767
 
731
768
  end
@@ -19,7 +19,7 @@ module Couchbase
19
19
 
20
20
  class Model
21
21
 
22
- VERSION = "0.4.1"
22
+ VERSION = "0.4.3"
23
23
 
24
24
  end
25
25
 
data/test/test_model.rb CHANGED
@@ -171,4 +171,14 @@ class TestModel < MiniTest::Unit::TestCase
171
171
  assert_equal "Anheuser-Busch", assoc.name
172
172
  end
173
173
 
174
+ def test_to_key
175
+ assert_equal ["the-id"], Post.new(:id => "the-id").to_key
176
+ assert_equal ["the-key"], Post.new(:key => "the-key").to_key
177
+ end
178
+
179
+ def test_to_param
180
+ assert_equal "the-id", Post.new(:id => "the-id").to_param
181
+ assert_equal "the-key", Post.new(:key => ["the", "key"]).to_param
182
+ end
183
+
174
184
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couchbase-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-26 00:00:00.000000000 Z
12
+ date: 2012-10-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: couchbase
@@ -167,4 +167,8 @@ rubygems_version: 1.8.23
167
167
  signing_key:
168
168
  specification_version: 3
169
169
  summary: Declarative interface to Couchbase
170
- test_files: []
170
+ test_files:
171
+ - test/setup.rb
172
+ - test/test_model.rb
173
+ - test/test_uuid.rb
174
+ has_rdoc: