couchpillow 0.3.3 → 0.3.4

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: 5528117c461394f549f525f4d587ba6353251934
4
- data.tar.gz: d87079aab16ef5ebfedcf98371d747493d54e6f6
3
+ metadata.gz: d63d2c4df367a2e6437afd6fc0098a19cee672fc
4
+ data.tar.gz: 525b36cfe68b71d73154572cbc172a78d87dd98a
5
5
  SHA512:
6
- metadata.gz: 660b03268c7185f55b5acc33f5acee91422a07e83c8bbd835cd299ee2a9749440a7fc3016171d446dbe96290627b10d951143f504faa09b92f29a8cf8cf6258a
7
- data.tar.gz: 53fd3d0be6683e26edbaf8581adfca520048c1660bcac6c1ec74f692cfed4376c27be6dc3c5f719e4dae6faee7acb833e60f9f0e33d8dee363807d486446c846
6
+ metadata.gz: e4a0d12cb5ea95f34b04d1c007ba2a1a6d8b12ddc487f1d1a3f798adbf9b29fff6614ff67ada756e8dda58b769ca5a1bbe4c7da1bca4a7327465266d4ce0358a
7
+ data.tar.gz: 205392e541c91358338d3b617c21869ea3a41b1407353dcb430389bd1141ac0d6f8851b607426eaf14f0c80001671029e7d87f505c26d2f8a187295f775dbce6
data/README.markdown CHANGED
@@ -1,13 +1,12 @@
1
1
  # CouchPillow
2
2
 
3
- Light and comfortable Document wrapper and integrity tool for Couchbase Server.
3
+ Light and comfortable Document integrity tool for Couchbase Server.
4
4
 
5
5
 
6
6
  ## Why CouchPillow?
7
7
 
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.
8
+ CouchPillow is an integrity tool for Couchbase Documents to make sure that all
9
+ current and existing documents can work nicely with the current code.
11
10
 
12
11
  CouchPillow separates itself from the database drivers, making it light and
13
12
  independent from the implementation. Although it is initially designed to work
@@ -32,6 +32,7 @@ module CouchPillow
32
32
  raise TypeError if @data[:_type] && @data[:_type] != self.class._type
33
33
  @data[:_type] = self.class._type
34
34
  rename!
35
+ ensure_types!
35
36
  end
36
37
 
37
38
 
@@ -67,12 +68,14 @@ module CouchPillow
67
68
  super
68
69
  end
69
70
 
71
+
70
72
  def timestamp!
71
73
  @data[:updated_at] = Time.now.utc
72
74
  end
73
75
 
74
76
 
75
77
  # Save this document to the server
78
+ #
76
79
  def save!
77
80
  validate
78
81
  sort!
@@ -129,10 +132,35 @@ module CouchPillow
129
132
  end
130
133
 
131
134
 
135
+ def ensure_types!
136
+ self.class.type_keys.each do |k, t|
137
+ if value = @data[k] and !value.is_a?(t)
138
+ if t == Integer
139
+ @data[k] = Integer(value)
140
+ elsif t == Float
141
+ @data[k] = Float(value)
142
+ elsif t == String
143
+ @data[k] = String(value)
144
+ elsif t == Array
145
+ @data[k] = Array(value)
146
+ elsif t == Time
147
+ @data[k] = Time.parse(value)
148
+ end
149
+ end
150
+ end
151
+ end
152
+
153
+
154
+ # Get a Document given an id.
155
+ #
156
+ # @returns nil if Document is of a different type.
157
+ #
132
158
  def self.get id
133
159
  result = CouchPillow.db.get(id) and
160
+ type = result['_type'] and
161
+ type == self._type and
134
162
  new(result, id) or
135
- result
163
+ nil
136
164
  end
137
165
 
138
166
 
@@ -168,6 +196,7 @@ module CouchPillow
168
196
  #
169
197
  def self.validate_type key, type
170
198
  validate key, "is not the correct type. Expected a #{type}", lambda { |v| v.is_a? type }
199
+ type_keys << [key, type]
171
200
  end
172
201
 
173
202
 
@@ -188,8 +217,13 @@ module CouchPillow
188
217
  end
189
218
 
190
219
 
220
+ def self.type_keys
221
+ @type_keys ||= []
222
+ end
223
+
224
+
191
225
  def self.validate_keys
192
- @validate_key ||= []
226
+ @validate_keys ||= []
193
227
  end
194
228
 
195
229
 
@@ -1,5 +1,5 @@
1
1
  module CouchPillow
2
2
  GEM_NAME = "couchpillow"
3
3
  NAME = "CouchPillow"
4
- VERSION = "0.3.3"
4
+ VERSION = "0.3.4"
5
5
  end
@@ -63,7 +63,7 @@ class TestDocument < Minitest::Test
63
63
 
64
64
  def test_validate_type
65
65
  d = Class.new(Document) do
66
- validate_type :abc, Hash
66
+ validate_type :abc, Hash
67
67
  end.new
68
68
 
69
69
  d.abc = "other type"
@@ -76,6 +76,94 @@ class TestDocument < Minitest::Test
76
76
  end
77
77
 
78
78
 
79
+ def test_validate_type_also_ensures_types_on_create_integer
80
+ klass = Class.new(Document) do
81
+ type "test"
82
+ validate_type :abc, Integer
83
+ end
84
+
85
+ CouchPillow.db.
86
+ expects(:get).
87
+ with('123').
88
+ returns( { '_type' => 'test', 'abc' => '100' } )
89
+
90
+ d = klass.get('123')
91
+ assert d
92
+ assert_equal 100, d.abc
93
+ end
94
+
95
+
96
+ def test_validate_type_also_ensures_types_on_create_string
97
+ klass = Class.new(Document) do
98
+ type "test"
99
+ validate_type :abc, String
100
+ end
101
+
102
+ CouchPillow.db.
103
+ expects(:get).
104
+ with('123').
105
+ returns( { '_type' => 'test', 'abc' => 100 } )
106
+
107
+ d = klass.get('123')
108
+ assert d
109
+ assert_equal '100', d.abc
110
+ end
111
+
112
+
113
+ def test_validate_type_also_ensures_types_on_create_float
114
+ klass = Class.new(Document) do
115
+ type "test"
116
+ validate_type :abc, Float
117
+ end
118
+
119
+ CouchPillow.db.
120
+ expects(:get).
121
+ with('123').
122
+ returns( { '_type' => 'test', 'abc' => '121.21' } )
123
+
124
+ d = klass.get('123')
125
+ assert d
126
+ assert_equal 121.21, d.abc
127
+ end
128
+
129
+
130
+ def test_validate_type_also_ensures_types_on_create_array
131
+ klass = Class.new(Document) do
132
+ type "test"
133
+ validate_type :abc, Array
134
+ end
135
+
136
+ CouchPillow.db.
137
+ expects(:get).
138
+ with('123').
139
+ returns( { '_type' => 'test', 'abc' => [1, 2, 3] } )
140
+
141
+ d = klass.get('123')
142
+ assert d
143
+ assert_equal [1, 2, 3], d.abc
144
+ end
145
+
146
+
147
+ def test_validate_type_also_ensures_types_on_create_time
148
+ klass = Class.new(Document) do
149
+ type "test"
150
+ validate_type :abc, Time
151
+ end
152
+
153
+ CouchPillow.db.
154
+ expects(:get).
155
+ with('123').
156
+ returns( { '_type' => 'test', 'abc' => "2014/07/04" } )
157
+
158
+ d = klass.get('123')
159
+ assert d
160
+ assert d.abc.is_a?(Time)
161
+ assert_equal 2014, d.abc.year
162
+ assert_equal 7, d.abc.month
163
+ assert_equal 4, d.abc.day
164
+ end
165
+
166
+
79
167
  def test_validate_custom
80
168
  d = Class.new(Document) do
81
169
  validate :xyz, "must be Numeric", lambda { |v| v.is_a? Numeric }
@@ -162,4 +250,19 @@ class TestDocument < Minitest::Test
162
250
  assert_equal nil, d
163
251
  end
164
252
 
253
+
254
+ def test_get_returns_a_document_of_a_different_type
255
+ klass = Class.new(Document) do
256
+ type 'test'
257
+ end
258
+
259
+ CouchPillow.db
260
+ .expects(:get)
261
+ .with('123')
262
+ .returns( { '_type' => 'something else', 'stuff' => 'data' } )
263
+
264
+ d = klass.get('123')
265
+ assert_equal nil, d
266
+ end
267
+
165
268
  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.3
4
+ version: 0.3.4
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-19 00:00:00.000000000 Z
11
+ date: 2014-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  version: '0'
89
89
  requirements: []
90
90
  rubyforge_project:
91
- rubygems_version: 2.3.0
91
+ rubygems_version: 2.4.1
92
92
  signing_key:
93
93
  specification_version: 4
94
94
  summary: Document wrapper for Couchbase