couchpillow 0.3.0 → 0.3.1

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: 3ee5cf9a4867bd4fa3ab19cb9ad0e1401fa94dce
4
- data.tar.gz: 56f2d1e20e5a33755946c1e3c7d1147663be2cae
3
+ metadata.gz: dd27aa70e0b7f3922ff6580ead5c85c0ad3cbf48
4
+ data.tar.gz: 7b15227e763211f3638452b5604949b77c5038aa
5
5
  SHA512:
6
- metadata.gz: de31833c33e134ee6dd08dd35416b80b76249bf06d69187be8d8f7e74828e8698945adcd060187f9eefa2b89d6ff0836e992151b732e16fbbbc6b4fc877d4972
7
- data.tar.gz: 78fb2de9b1356819ce87dbd754be463d79bf056c96175f3f92cc52442fe28d1d26594c51cb71f5f94c99c0269857dd2b457444b35c51b81eb718d0688a6034a4
6
+ metadata.gz: b21d6f6094133fd122e9f3f473c3affcb0e5e268a393138321d0ca7451c57c5d3268fff7afb3abe848abaa563c1f7901d62e19e6e382c47af1572e429ff02a41
7
+ data.tar.gz: 018d26070dc1111eae361f086fac5317e86fbb01b439a8b8233c16ef68fd8be80fa12cdf14fe316c15d8f790d5788ca2e7d1656a83bb940c7f49571ed31f3d42
data/README.markdown CHANGED
@@ -1,22 +1,25 @@
1
1
  # CouchPillow
2
2
 
3
- Light and comfortable Document wrapper for Couchbase Server.
3
+ Light and comfortable Document wrapper and integrity tool for Couchbase Server.
4
4
 
5
5
 
6
6
  ## Why CouchPillow?
7
7
 
8
- Yet another ORM? Not quite. CouchPillow separates itself from the database
9
- drivers, making it light and independent from the implementation.
8
+ Yet another ORM? Not quite. CouchPillow is a wrapper to Couchbase Documents
9
+ and also an integrity tool to make sure that all current and existing documents
10
+ can work nicely with the current code.
10
11
 
11
- Although it is initially designed to work with Couchbase Server, one can
12
- easily extend this to other NoSQL drivers as long as they `respond_to?`
13
- the `set`, `delete`, `replace`, and `get` methods.
12
+ CouchPillow separates itself from the database drivers, making it light and
13
+ independent from the implementation. Although it is initially designed to work
14
+ with Couchbase Server, one can easily extend this to other NoSQL drivers as
15
+ long as they `respond_to?` the `set`, `delete`, `replace`, and `get` methods.
14
16
 
15
17
 
16
18
  ## Features
17
19
 
18
- - Automatic timestamp
19
- - Validation
20
+ - Automatic id generation.
21
+ - Automatic timestamp.
22
+ - Built-in and custom data validations.
20
23
 
21
24
 
22
25
  ## Installation
@@ -40,13 +43,13 @@ the `set`, `delete`, `replace`, and `get` methods.
40
43
  # }
41
44
 
42
45
 
43
- Retrieving Documents
46
+ Retrieving Documents:
44
47
 
45
48
  doc = CouchPillow::Document.get('123')
46
49
  doc.stuff # '123'
47
50
 
48
51
 
49
- Overriding `CouchPillow::Document`
52
+ Overriding `CouchPillow::Document`:
50
53
 
51
54
  class User < CouchPillow::Document
52
55
  type :user
@@ -65,7 +68,7 @@ Overriding `CouchPillow::Document`
65
68
  # 'updated_at': '2014-07-04 00:00:00 UTC'
66
69
  # }
67
70
 
68
- Using validation
71
+ Using built-in validation methods:
69
72
 
70
73
  class User < CouchPillow::Document
71
74
  type :user
@@ -79,18 +82,15 @@ Using validation
79
82
  doc.email = 'john@email.com'
80
83
  doc.save! # Success!
81
84
 
82
- Using custom validation blocks
85
+ Using custom validation blocks:
83
86
 
84
87
  class User < CouchPillow::Document
85
88
  type :user
86
- validate_presence :email
87
89
  validate :phone, 'is not a number', lambda { |v| v.is_a? Numeric }
88
90
  end
89
91
 
90
92
  CouchPillow.db = Couchbase.connect( bucket: 'default', host: 'localhost' )
91
93
  doc = User.new
92
- doc.email = 'john@email.com'
93
- doc.first_name = 'john'
94
94
  doc.phone = '123'
95
95
  doc.save! # raises ValidationError('phone is not a number')
96
96
  doc.phone = 123
@@ -13,15 +13,21 @@ module CouchPillow
13
13
 
14
14
  PRESENCE_LAMBDA = lambda do |value| !value.nil? end
15
15
 
16
+ RESERVED_KEYS = %i[_id _type created_at updated_at]
17
+
16
18
 
17
19
  def initialize hash = {}, id = SecureRandom.hex
18
20
  @data = self.class.symbolize(hash)
19
21
  @id = id
20
22
 
21
23
  @data[:created_at] and
24
+ @data[:created_at].is_a? String and
22
25
  @data[:created_at] = Time.parse(@data[:created_at]) or
23
26
  @data[:created_at] = Time.now.utc
24
- @data[:updated_at] = Time.parse(@data[:updated_at]) if @data[:updated_at]
27
+
28
+ @data[:updated_at] and
29
+ @data[:updated_at].is_a? String and
30
+ @data[:updated_at] = Time.parse(@data[:updated_at])
25
31
 
26
32
  raise TypeError if @data[:_type] && @data[:_type] != self.class._type
27
33
  @data[:_type] = self.class._type
@@ -141,6 +147,9 @@ module CouchPillow
141
147
  # Rename an existing key to a new key. This is invoked right after initialize.
142
148
  #
143
149
  def self.rename from, to
150
+ raise ArgumentError, "Cannot rename reserved keys" if
151
+ RESERVED_KEYS.include?(from) || RESERVED_KEYS.include?(to)
152
+
144
153
  rename_keys << [from.to_s.to_sym, to.to_s.to_sym]
145
154
  end
146
155
 
@@ -1,5 +1,5 @@
1
1
  module CouchPillow
2
2
  GEM_NAME = "couchpillow"
3
3
  NAME = "CouchPillow"
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
data/lib/couchpillow.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'securerandom'
2
2
  require 'json'
3
+ require 'time'
3
4
 
4
5
  module CouchPillow
5
6
 
data/test/helper.rb ADDED
@@ -0,0 +1,32 @@
1
+ require './lib/couchpillow.rb'
2
+
3
+
4
+ class FakeCouchbaseServer
5
+
6
+ def initialize
7
+ @storage = {}
8
+ end
9
+
10
+
11
+ def set id, data
12
+ @storage[id] = data
13
+ end
14
+
15
+
16
+ def delete id
17
+ @storage.delete(id)
18
+ end
19
+
20
+
21
+ def replace id, data
22
+ @storage.has_key?(id) or raise "Document does not exist"
23
+ @storage[id] = data
24
+ end
25
+
26
+
27
+ def get id
28
+ @storage[id]
29
+ end
30
+ end
31
+
32
+
@@ -1,7 +1,7 @@
1
1
  require 'minitest/autorun'
2
2
  require 'minitest/unit'
3
3
  require 'mocha/mini_test'
4
- require './lib/couchpillow.rb'
4
+ require './test/helper.rb'
5
5
 
6
6
  class TestDocument < Minitest::Test
7
7
 
@@ -16,12 +16,7 @@ class TestDocument < Minitest::Test
16
16
 
17
17
 
18
18
  def setup
19
- @db = mock('couchbaseserver')
20
- @db.stubs(:set)
21
- @db.stubs(:delete)
22
- @db.stubs(:replace)
23
- @db.stubs(:get)
24
- CouchPillow.db = @db
19
+ CouchPillow.db = FakeCouchbaseServer.new
25
20
  end
26
21
 
27
22
 
@@ -83,7 +78,7 @@ class TestDocument < Minitest::Test
83
78
 
84
79
  def test_validate_custom
85
80
  d = Class.new(Document) do
86
- validate :xyz, "must be Numeric", lambda { |v| v.is_a? Numeric }
81
+ validate :xyz, "must be Numeric", lambda { |v| v.is_a? Numeric }
87
82
  end.new
88
83
  d.xyz = "string"
89
84
  assert_raises Document::ValidationError do
@@ -133,4 +128,31 @@ class TestDocument < Minitest::Test
133
128
  assert_equal 'abc', d.other
134
129
  end
135
130
 
131
+
132
+ def test_rename_keys_reserved_keys
133
+ assert_raises ArgumentError do
134
+ d = Class.new(Document) do
135
+ rename :_id, :bad
136
+ end.new
137
+ end
138
+
139
+ assert_raises ArgumentError do
140
+ d = Class.new(Document) do
141
+ rename :_type, :err
142
+ end.new
143
+ end
144
+
145
+ assert_raises ArgumentError do
146
+ d = Class.new(Document) do
147
+ rename :created_at, :err
148
+ end.new
149
+ end
150
+
151
+ assert_raises ArgumentError do
152
+ d = Class.new(Document) do
153
+ rename :bla, :updated_at
154
+ end.new
155
+ end
156
+ end
157
+
136
158
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couchpillow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Albert Tedja
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-06 00:00:00.000000000 Z
11
+ date: 2014-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -66,6 +66,7 @@ files:
66
66
  - lib/couchpillow.rb
67
67
  - lib/couchpillow/document.rb
68
68
  - lib/couchpillow/version.rb
69
+ - test/helper.rb
69
70
  - test/test_document.rb
70
71
  homepage: https://github.com/atedja/pillow
71
72
  licenses: