couchbase-orm 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cc28632e30fe3d518b6fb10fe892a9a51fae2229
4
- data.tar.gz: c794c256004e9550f88dd2e760df80d0e35ad6b6
3
+ metadata.gz: f69d88d6820ca854f9650369e01b4ccb7c431a22
4
+ data.tar.gz: c80410e6473d97aee26722c763286bd0a322e9ea
5
5
  SHA512:
6
- metadata.gz: 8cfd1a37baac0d5ff3045165d6a723b1d91814fe20790511e1a242839917de1b04de3806af038f2bcb34ea90445bfd73d0869bea2b0eb77ab12cf326b7f722dc
7
- data.tar.gz: eaa78cf644bb41f50b4e465931308174394589c89ca46b6569e485d7311a17ee27c6a4a42071fa1bd499a4a09ffaec9e7c37913af9fd2e3f8707af441d3f189c
6
+ metadata.gz: 5e157bc072a22300558ef1b6bca1ed2033a4c74e8651b78c09306d43687081b0e3560199c10ab185b8b35ae4fca46056a067ddcbf79af09eacdd2d036f225dce
7
+ data.tar.gz: 3543a615ad4e0f117139ec75658e714f119e15aef615859fa9a5dd161edc90a54d7ba99606cc29b15d39872a79a31a7f3fdcea41527cfc374abb864aaa3f7ab8
@@ -128,9 +128,9 @@ module CouchbaseOrm
128
128
  self.class.attributes.each do |key, options|
129
129
  default = options[:default]
130
130
  if default.respond_to?(:call)
131
- @__attributes__[key] = default.call
131
+ write_attribute key, default.call
132
132
  else
133
- @__attributes__[key] = default
133
+ write_attribute key, default
134
134
  end
135
135
  end
136
136
 
@@ -2,11 +2,11 @@ module CouchbaseOrm
2
2
  module EnsureUnique
3
3
  private
4
4
 
5
- def ensure_unique(attrs, name = nil, &processor)
5
+ def ensure_unique(attrs, name = nil, presence: true, &processor)
6
6
  # index uses a special bucket key to allow record lookups based on
7
7
  # the values of attrs. ensure_unique adds a simple lookup using
8
8
  # one of the added methods to identify duplicate
9
- name = index(attrs, name, &processor)
9
+ name = index(attrs, name, presence: presence, &processor)
10
10
 
11
11
  validate do |record|
12
12
  unless record.send("#{name}_unique?")
@@ -2,7 +2,7 @@ module CouchbaseOrm
2
2
  module Index
3
3
  private
4
4
 
5
- def index(attrs, name = nil, &processor)
5
+ def index(attrs, name = nil, presence: true, &processor)
6
6
  attrs = Array(attrs).flatten
7
7
  name ||= attrs.map(&:to_s).join('_')
8
8
 
@@ -67,9 +67,11 @@ module CouchbaseOrm
67
67
  # validations
68
68
  #----------------
69
69
  # ensure each component of the unique key is present
70
- attrs.each do |attr|
71
- validates attr, presence: true
72
- define_attribute_methods attr
70
+ if presence
71
+ attrs.each do |attr|
72
+ validates attr, presence: true
73
+ define_attribute_methods attr
74
+ end
73
75
  end
74
76
 
75
77
  define_method("#{name}_unique?") do
@@ -97,7 +99,10 @@ module CouchbaseOrm
97
99
  after_save do |record|
98
100
  original_key = instance_variable_get(original_bucket_key_var)
99
101
  record.class.bucket.delete(original_key, quiet: true) if original_key
100
- record.class.bucket.set(record.send(bucket_key_method), record.id, plain: true)
102
+
103
+ unless presence == false && attrs.length == 1 && record[attrs[0]].nil?
104
+ record.class.bucket.set(record.send(bucket_key_method), record.id, plain: true)
105
+ end
101
106
  instance_variable_set(original_bucket_key_var, nil)
102
107
  end
103
108
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true, encoding: ASCII-8BIT
2
2
 
3
3
  module CouchbaseOrm
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.2'
5
5
  end
@@ -32,9 +32,9 @@ module CouchbaseOrm
32
32
  method_opts[:reduce] = reduce if reduce
33
33
 
34
34
  unless method_opts.has_key? :map
35
- emit_key = emit_key || :id
35
+ emit_key = emit_key || :created_at
36
36
 
37
- if emit_key != :id && self.attributes[emit_key][:type].to_s == 'Array'
37
+ if emit_key != :created_at && self.attributes[emit_key][:type].to_s == 'Array'
38
38
  method_opts[:map] = <<-EMAP
39
39
  function(doc) {
40
40
  var i;
data/spec/index_spec.rb CHANGED
@@ -4,8 +4,9 @@ require File.expand_path("../support", __FILE__)
4
4
 
5
5
 
6
6
  class IndexTest < CouchbaseOrm::Base
7
- attribute :email
8
- ensure_unique :email
7
+ attribute :email, type: String
8
+ attribute :name, type: String, default: :joe
9
+ ensure_unique :email, presence: false
9
10
  end
10
11
 
11
12
  class EnumTest < CouchbaseOrm::Base
@@ -14,6 +15,11 @@ end
14
15
 
15
16
 
16
17
  describe CouchbaseOrm::Index do
18
+ after :each do
19
+ IndexTest.bucket.delete('index_testemail-joe@aca.com')
20
+ IndexTest.bucket.delete('index_testemail-')
21
+ end
22
+
17
23
  it "should prevent models being created if they should have unique keys" do
18
24
  joe = IndexTest.create!(email: 'joe@aca.com')
19
25
  expect { IndexTest.create!(email: 'joe@aca.com') }.to raise_error(CouchbaseOrm::Error::RecordInvalid)
@@ -54,6 +60,22 @@ describe CouchbaseOrm::Index do
54
60
  again.destroy
55
61
  end
56
62
 
63
+ it "should work with nil values" do
64
+ joe = IndexTest.create!
65
+ expect(IndexTest.find_by_email(nil)).to eq(nil)
66
+
67
+ joe.email = 'joe@aca.com'
68
+ joe.save!
69
+ expect(IndexTest.find_by_email('joe@aca.com')).to eq(joe)
70
+
71
+ joe.email = nil
72
+ joe.save!
73
+ expect(IndexTest.find_by_email('joe@aca.com')).to eq(nil)
74
+ expect(IndexTest.find_by_email(nil)).to eq(nil)
75
+
76
+ joe.destroy
77
+ end
78
+
57
79
  it "should work with enumerators" do
58
80
  # Test symbol
59
81
  enum = EnumTest.create!(visibility: :public)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couchbase-orm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen von Takach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-08 00:00:00.000000000 Z
11
+ date: 2016-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: libcouchbase