couchbase-model 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ ## 0.5.1 / 2012-11-29
2
+
3
+ * Introduce save! and create! methods and raise RecordInvalid only from them
4
+
1
5
  ## 0.5.0 / 2012-11-21
2
6
 
3
7
  * Update template for map function
@@ -430,11 +430,20 @@ module Couchbase
430
430
  # @since 0.0.1
431
431
  #
432
432
  # @param [Hash] args attribute-value pairs for the object
433
- # @return [Couchbase::Model] an instance of the model
433
+ # @return [Couchbase::Model, false] an instance of the model
434
434
  def self.create(*args)
435
435
  new(*args).create
436
436
  end
437
437
 
438
+ # Creates an object just like {{Model.create} but raises an exception if
439
+ # the record is invalid.
440
+ #
441
+ # @since 0.5.1
442
+ # @raise [Couchbase::Error::RecordInvalid] if the instance is invalid
443
+ def self.create!(*args)
444
+ new(*args).create!
445
+ end
446
+
438
447
  # Constructor for all subclasses of Couchbase::Model
439
448
  #
440
449
  # @since 0.0.1
@@ -473,7 +482,7 @@ module Couchbase
473
482
  #
474
483
  # @since 0.0.1
475
484
  #
476
- # @return [Couchbase::Model] newly created object
485
+ # @return [Couchbase::Model, false] newly created object
477
486
  #
478
487
  # @raise [Couchbase::Error::KeyExists] if model with the same +id+
479
488
  # exists in the bucket
@@ -484,7 +493,7 @@ module Couchbase
484
493
  def create(options = {})
485
494
  @id ||= Couchbase::Model::UUID.generator.next(1, model.thread_storage[:uuid_algorithm])
486
495
  if respond_to?(:valid?) && !valid?
487
- raise Couchbase::Error::RecordInvalid.new(self)
496
+ return false
488
497
  end
489
498
  options = model.defaults.merge(options)
490
499
  value = (options[:format] == :plain) ? @raw : attributes_with_values
@@ -498,13 +507,25 @@ module Couchbase
498
507
  self
499
508
  end
500
509
 
510
+ # Creates an object just like {{Model#create} but raises an exception if
511
+ # the record is invalid.
512
+ #
513
+ # @since 0.5.1
514
+ #
515
+ # @raise [Couchbase::Error::RecordInvalid] if the instance is invalid
516
+ def create!(options = {})
517
+ create(options) || raise(Couchbase::Error::RecordInvalid.new(self))
518
+ end
519
+
501
520
  # Create or update this object based on the state of #new?.
502
521
  #
503
522
  # @since 0.0.1
504
523
  #
505
524
  # @param [Hash] options options for operation, see
506
525
  # {{Couchbase::Bucket#set}}
507
- # @return [Couchbase::Model] The saved object
526
+ #
527
+ # @return [Couchbase::Model, false] saved object or false if there
528
+ # are validation errors
508
529
  #
509
530
  # @example Update the Post model
510
531
  # p = Post.find('hello-world')
@@ -515,10 +536,11 @@ module Couchbase
515
536
  # p = Post.find('hello-world')
516
537
  # p.draft = false
517
538
  # p.save('cas' => p.meta['cas'])
539
+ #
518
540
  def save(options = {})
519
541
  return create(options) unless @meta
520
542
  if respond_to?(:valid?) && !valid?
521
- raise Couchbase::Error::RecordInvalid.new(self)
543
+ return false
522
544
  end
523
545
  options = model.defaults.merge(options)
524
546
  value = (options[:format] == :plain) ? @raw : attributes_with_values
@@ -526,6 +548,16 @@ module Couchbase
526
548
  self
527
549
  end
528
550
 
551
+ # Creates an object just like {{Model#save} but raises an exception if
552
+ # the record is invalid.
553
+ #
554
+ # @since 0.5.1
555
+ #
556
+ # @raise [Couchbase::Error::RecordInvalid] if the instance is invalid
557
+ def save!(options = {})
558
+ save(options) || raise(Couchbase::Error::RecordInvalid.new(self))
559
+ end
560
+
529
561
  # Update this object, optionally accepting new attributes.
530
562
  #
531
563
  # @since 0.0.1
@@ -19,7 +19,7 @@ module Couchbase
19
19
 
20
20
  class Model
21
21
 
22
- VERSION = "0.5.0"
22
+ VERSION = "0.5.1"
23
23
 
24
24
  end
25
25
 
@@ -50,7 +50,8 @@ module Rails #:nodoc:
50
50
  def self.rescue_responses
51
51
  {
52
52
  "Couchbase::Error::NotFound" => :not_found,
53
- "Couchbase::Error::NotStored" => :unprocessable_entity
53
+ "Couchbase::Error::NotStored" => :unprocessable_entity,
54
+ "Couchbase::Error::RecordInvalid" => :unprocessable_entity
54
55
  }
55
56
  end
56
57
 
@@ -225,11 +225,13 @@ class TestModel < MiniTest::Unit::TestCase
225
225
  post = ValidPost.create(:title => 'Hello, World!')
226
226
  assert post.valid?, "post with title should be valid"
227
227
  post.title = nil
228
+ refute post.save
228
229
  assert_raises(Couchbase::Error::RecordInvalid) do
229
- post.save
230
+ post.save!
230
231
  end
232
+ refute ValidPost.create(:title => nil)
231
233
  assert_raises(Couchbase::Error::RecordInvalid) do
232
- ValidPost.create(:title => nil)
234
+ ValidPost.create!(:title => nil)
233
235
  end
234
236
  end
235
237
 
metadata CHANGED
@@ -1,121 +1,118 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: couchbase-model
3
- version: !ruby/object:Gem::Version
4
- hash: 11
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.1
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 5
9
- - 0
10
- version: 0.5.0
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Couchbase
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-11-21 00:00:00 +03:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- type: :runtime
23
- requirement: &id001 !ruby/object:Gem::Requirement
12
+ date: 2012-11-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: couchbase
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
18
+ requirements:
26
19
  - - ~>
27
- - !ruby/object:Gem::Version
28
- hash: -168919795
29
- segments:
30
- - 1
31
- - 2
32
- - 0
33
- - dp
20
+ - !ruby/object:Gem::Version
34
21
  version: 1.2.0.dp
35
- name: couchbase
36
- version_requirements: *id001
22
+ type: :runtime
37
23
  prerelease: false
38
- - !ruby/object:Gem::Dependency
39
- type: :development
40
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
41
25
  none: false
42
- requirements:
26
+ requirements:
43
27
  - - ~>
44
- - !ruby/object:Gem::Version
45
- hash: 49
46
- segments:
47
- - 0
48
- - 8
49
- - 7
50
- version: 0.8.7
28
+ - !ruby/object:Gem::Version
29
+ version: 1.2.0.dp
30
+ - !ruby/object:Gem::Dependency
51
31
  name: rake
52
- version_requirements: *id002
53
- prerelease: false
54
- - !ruby/object:Gem::Dependency
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.8.7
55
38
  type: :development
56
- requirement: &id003 !ruby/object:Gem::Requirement
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
57
41
  none: false
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- hash: 3
62
- segments:
63
- - 0
64
- version: "0"
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.8.7
46
+ - !ruby/object:Gem::Dependency
65
47
  name: minitest
66
- version_requirements: *id003
67
- prerelease: false
68
- - !ruby/object:Gem::Dependency
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
69
54
  type: :development
70
- requirement: &id004 !ruby/object:Gem::Requirement
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
71
57
  none: false
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- hash: 3
76
- segments:
77
- - 0
78
- version: "0"
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
79
63
  name: rdiscount
80
- version_requirements: *id004
81
- prerelease: false
82
- - !ruby/object:Gem::Dependency
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
83
70
  type: :development
84
- requirement: &id005 !ruby/object:Gem::Requirement
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
85
73
  none: false
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- hash: 3
90
- segments:
91
- - 0
92
- version: "0"
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
93
79
  name: yard
94
- version_requirements: *id005
95
- prerelease: false
96
- - !ruby/object:Gem::Dependency
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
97
86
  type: :development
98
- requirement: &id006 !ruby/object:Gem::Requirement
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
99
89
  none: false
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- hash: 3
104
- segments:
105
- - 0
106
- version: "0"
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
107
95
  name: debugger
108
- version_requirements: *id006
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
109
103
  prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
110
  description: ORM-like interface allows you to persist your models to Couchbase
111
111
  email: support@couchbase.com
112
112
  executables: []
113
-
114
113
  extensions: []
115
-
116
114
  extra_rdoc_files: []
117
-
118
- files:
115
+ files:
119
116
  - .gitignore
120
117
  - .travis.yml
121
118
  - .yardopts
@@ -146,41 +143,28 @@ files:
146
143
  - test/setup.rb
147
144
  - test/test_model.rb
148
145
  - test/test_uuid.rb
149
- has_rdoc: true
150
146
  homepage: https://github.com/couchbase/couchbase-ruby-model
151
147
  licenses: []
152
-
153
148
  post_install_message:
154
149
  rdoc_options: []
155
-
156
- require_paths:
150
+ require_paths:
157
151
  - lib
158
- required_ruby_version: !ruby/object:Gem::Requirement
152
+ required_ruby_version: !ruby/object:Gem::Requirement
159
153
  none: false
160
- requirements:
161
- - - ">="
162
- - !ruby/object:Gem::Version
163
- hash: 3
164
- segments:
165
- - 0
166
- version: "0"
167
- required_rubygems_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
168
159
  none: false
169
- requirements:
170
- - - ">="
171
- - !ruby/object:Gem::Version
172
- hash: 3
173
- segments:
174
- - 0
175
- version: "0"
160
+ requirements:
161
+ - - ! '>='
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
176
164
  requirements: []
177
-
178
165
  rubyforge_project:
179
- rubygems_version: 1.6.2
166
+ rubygems_version: 1.8.23
180
167
  signing_key:
181
168
  specification_version: 3
182
169
  summary: Declarative interface to Couchbase
183
- test_files:
184
- - test/setup.rb
185
- - test/test_model.rb
186
- - test/test_uuid.rb
170
+ test_files: []