couchbase-model 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ ## 0.3.1 / 2012-09-22
2
+
3
+ * Allow to specify default storage options
4
+
1
5
  ## 0.3.0 / 2012-09-22
2
6
 
3
7
  * Implement belongs_to asscociation
@@ -163,6 +163,14 @@ module Couchbase
163
163
  end
164
164
  end
165
165
 
166
+ def self.defaults(options = nil)
167
+ if options
168
+ @_defaults = options
169
+ else
170
+ @_defaults || {}
171
+ end
172
+ end
173
+
166
174
  # Ensure that design document is up to date.
167
175
  #
168
176
  # @since 0.1.0
@@ -224,7 +232,7 @@ module Couchbase
224
232
  end
225
233
 
226
234
  doc['views'].delete_if {|_, v| v.empty? }
227
- doc.delete('spatial') if doc['spatial'].empty?
235
+ doc.delete('spatial') if doc['spatial'] && doc['spatial'].empty?
228
236
  doc['signature'] = digest.to_s
229
237
  doc['timestamp'] = mtime
230
238
  if doc['signature'] != thread_storage[:signature] && doc['timestamp'] > thread_storage[:timestamp].to_i
@@ -406,23 +414,28 @@ module Couchbase
406
414
  #
407
415
  # @param [Hash] attrs attribute-value pairs
408
416
  def initialize(attrs = {})
409
- if attrs.respond_to?(:with_indifferent_access)
410
- attrs = attrs.with_indifferent_access
411
- end
412
- @id = attrs.delete(:id)
413
- @key = attrs.delete(:key)
414
- @value = attrs.delete(:value)
415
- @doc = attrs.delete(:doc)
416
- @meta = attrs.delete(:meta)
417
- @_attributes = ::Hash.new do |h, k|
418
- default = self.class.attributes[k]
419
- h[k] = if default.respond_to?(:call)
420
- default.call
421
- else
422
- default
423
- end
417
+ case attrs
418
+ when Hash, HashWithIndifferentAccess
419
+ if attrs.respond_to?(:with_indifferent_access)
420
+ attrs = attrs.with_indifferent_access
421
+ end
422
+ @id = attrs.delete(:id)
423
+ @key = attrs.delete(:key)
424
+ @value = attrs.delete(:value)
425
+ @doc = attrs.delete(:doc)
426
+ @meta = attrs.delete(:meta)
427
+ @_attributes = ::Hash.new do |h, k|
428
+ default = self.class.attributes[k]
429
+ h[k] = if default.respond_to?(:call)
430
+ default.call
431
+ else
432
+ default
433
+ end
434
+ end
435
+ update_attributes(@doc || attrs)
436
+ else
437
+ @_raw = attrs
424
438
  end
425
- update_attributes(@doc || attrs)
426
439
  end
427
440
 
428
441
  # Create this model and assign new id if necessary
@@ -439,7 +452,8 @@ module Couchbase
439
452
  # p.create
440
453
  def create
441
454
  @id ||= Couchbase::Model::UUID.generator.next(1, model.thread_storage[:uuid_algorithm])
442
- model.bucket.add(@id, attributes_with_values)
455
+ value = @_raw ? @_raw : attributes_with_values
456
+ model.bucket.add(@id, value, model.defaults)
443
457
  self
444
458
  end
445
459
 
@@ -456,7 +470,8 @@ module Couchbase
456
470
  # p.save
457
471
  def save(cas = nil)
458
472
  return create if new?
459
- model.bucket.replace(@id, attributes_with_values, :cas => cas)
473
+ value = @_raw ? @_raw : attributes_with_values
474
+ model.bucket.replace(@id, value, model.default.merge(:cas => cas))
460
475
  self
461
476
  end
462
477
 
@@ -19,7 +19,7 @@ module Couchbase
19
19
 
20
20
  class Model
21
21
 
22
- VERSION = "0.3.0"
22
+ VERSION = "0.3.1"
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.0
4
+ version: 0.3.1
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-22 00:00:00.000000000 Z
12
+ date: 2012-09-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: couchbase