couchbase-model 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,8 @@
1
+ ## 0.4.0 / 2012-09-25
2
+
3
+ * Add validation hooks for Rails application
4
+ * Check meta presence as more robust indicator of key presence
5
+
1
6
  ## 0.3.1 / 2012-09-22
2
7
 
3
8
  * Allow to specify default storage options
@@ -24,5 +24,6 @@ if defined?(Rails)
24
24
  class Couchbase::Model
25
25
  extend ActiveModel::Naming
26
26
  include ActiveModel::Conversion
27
+ include ActiveModel::Validations
27
28
  end
28
29
  end
@@ -37,6 +37,15 @@ module Couchbase
37
37
  # @since 0.0.1
38
38
  class Error::MissingId < Error::Base; end
39
39
 
40
+ # @since 0.4.0
41
+ class Error::RecordInvalid < Error::Base
42
+ attr_reader :record
43
+ def initialize(record)
44
+ @record = record
45
+ super(@record.errors.full_messages.join(", "))
46
+ end
47
+ end
48
+
40
49
  # Declarative layer for Couchbase gem
41
50
  #
42
51
  # @since 0.0.1
@@ -414,6 +423,7 @@ module Couchbase
414
423
  #
415
424
  # @param [Hash] attrs attribute-value pairs
416
425
  def initialize(attrs = {})
426
+ @errors = ::ActiveModel::Errors.new(self) if defined?(::ActiveModel)
417
427
  case attrs
418
428
  when Hash, HashWithIndifferentAccess
419
429
  if attrs.respond_to?(:with_indifferent_access)
@@ -469,7 +479,10 @@ module Couchbase
469
479
  # p.draft = false
470
480
  # p.save
471
481
  def save(cas = nil)
472
- return create if new?
482
+ if respond_to?(:valid?) && !valid?
483
+ raise Couchbase::Error::RecordInvalid.new(self)
484
+ end
485
+ return create unless meta
473
486
  value = @_raw ? @_raw : attributes_with_values
474
487
  model.bucket.replace(@id, value, model.default.merge(:cas => cas))
475
488
  self
@@ -19,7 +19,7 @@ module Couchbase
19
19
 
20
20
  class Model
21
21
 
22
- VERSION = "0.3.1"
22
+ VERSION = "0.4.0"
23
23
 
24
24
  end
25
25
 
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.3.1
4
+ version: 0.4.0
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-23 00:00:00.000000000 Z
12
+ date: 2012-09-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: couchbase