couchpillow 0.3.9 → 0.3.10

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: b500f6013d468a7d8dda1e7eee6298520a76244d
4
- data.tar.gz: a5b75ca81bfaaffaa5a8e7264fa2caa2f0f6b64f
3
+ metadata.gz: 3185ef13356f6eadae67b5b1e57d3af472a3e545
4
+ data.tar.gz: 9954f18c4e2469738fabab9ff8e3c9cd0409e681
5
5
  SHA512:
6
- metadata.gz: 36bc6d363f9b5f44ce2105bb9e1178b648fe6932eaa90937c40dbce10f33ddf04d38da4a4ea3ca430e9fe245077dd412e41efd9a360daf57ba7e6550629c9d9f
7
- data.tar.gz: 8405f987a7711209aea4c01d8d6911eec27d553c1671e595653b09157d9b6e6ea292176662ee3b51fd5d2c24021c4d2167d0571426fb05db060c727345811d82
6
+ metadata.gz: ffd1543a372cd2fa2431d70b9210532ad04ed94398d8b4a54b1ed233070eaeb6dafcc8437e687790a954d3b9a5636e644e50b045ad4e70ab72c3803a47423013
7
+ data.tar.gz: cedf8d10d9b414c100797b505667da6010d8e6b0088bc73d2646e33456ea859d29b86a3dac0f4958da4fc656631be1de9fb940ffcb7568c3e09d077b14845611
data/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
1
  *.gem
2
2
  *.swp
3
3
  tags
4
+ .yardoc
5
+ doc
@@ -2,9 +2,16 @@ module CouchPillow
2
2
 
3
3
  class Document
4
4
 
5
+ # Validation Error.
6
+ #
5
7
  class ValidationError < StandardError; end
6
8
 
7
9
 
10
+ # Faux Boolean class used for type checking and conversion.
11
+ #
12
+ class Boolean; end
13
+
14
+
8
15
  attr_reader :id
9
16
 
10
17
 
@@ -47,6 +54,7 @@ module CouchPillow
47
54
  end
48
55
 
49
56
 
57
+ # @private
50
58
  # Map hash keys to methods
51
59
  #
52
60
  def method_missing m, *args, &block
@@ -64,6 +72,8 @@ module CouchPillow
64
72
  end
65
73
 
66
74
 
75
+ # @private
76
+ #
67
77
  def respond_to? m
68
78
  ms = m.to_s
69
79
  return true if ms.end_with?("=")
@@ -72,6 +82,8 @@ module CouchPillow
72
82
  end
73
83
 
74
84
 
85
+ # @private
86
+ #
75
87
  def timestamp!
76
88
  @data[:updated_at] = Time.now.utc
77
89
  end
@@ -143,12 +155,14 @@ module CouchPillow
143
155
 
144
156
  def validate
145
157
  self.class.validate_keys.each do |k, msg, method|
146
- raise ValidationError, "#{k} #{msg}" unless
158
+ raise ValidationError, "[#{k}, #{@data[k]}] #{msg}" unless
147
159
  @data.has_key?(k) && method.call(@data[k])
148
160
  end
149
161
  end
150
162
 
151
163
 
164
+ # Rename the keys in this Document as specified by the {rename} directive.
165
+ #
152
166
  def rename!
153
167
  self.class.rename_keys.each do |from, to|
154
168
  @data.has_key?(from) and
@@ -158,6 +172,9 @@ module CouchPillow
158
172
  end
159
173
 
160
174
 
175
+ # Ensure types of the values in the Document are converted to the type
176
+ # specified in the {validate_type} method.
177
+ #
161
178
  def ensure_types!
162
179
  self.class.type_keys.each do |k, t|
163
180
  if value = @data[k] and !value.is_a?(t)
@@ -171,12 +188,17 @@ module CouchPillow
171
188
  @data[k] = Array(value)
172
189
  elsif t == Time
173
190
  @data[k] = Time.parse(value)
191
+ elsif t == Boolean
192
+ @data[k] = value == 0 || !value ? false : true
174
193
  end
175
194
  end
176
195
  end
177
196
  end
178
197
 
179
198
 
199
+ # Filter this Document through the whitelist as specified by the
200
+ # {whitelist} directive.
201
+ #
180
202
  def whitelist!
181
203
  unless self.class.whitelist_keys.empty?
182
204
  @data.select! do |k, v|
@@ -189,7 +211,7 @@ module CouchPillow
189
211
 
190
212
  # Get a Document given an id.
191
213
  #
192
- # @returns nil if Document is of a different type.
214
+ # @return nil if Document is of a different type.
193
215
  #
194
216
  def self.get id
195
217
  result = CouchPillow.db.get(id) and
@@ -200,16 +222,13 @@ module CouchPillow
200
222
  end
201
223
 
202
224
 
225
+ # Sets the type of this Document.
226
+ #
203
227
  def self.type value
204
228
  @type = value.to_s
205
229
  end
206
230
 
207
231
 
208
- def self._type
209
- @type
210
- end
211
-
212
-
213
232
  # Rename an existing key to a new key. This is invoked right after
214
233
  # initialize.
215
234
  #
@@ -231,23 +250,35 @@ module CouchPillow
231
250
 
232
251
  # Validate the type of a particular key.
233
252
  #
253
+ # @example
254
+ # validate_type :name, String
255
+ #
234
256
  def self.validate_type key, type
235
- validate key, "is not the correct type. Expected a #{type}", lambda { |v| v.is_a? type }
257
+ validate key, "#{key} is not the correct type. Expected a #{type}", lambda { |v| v.is_a? type }
236
258
  type_keys << [key, type]
237
259
  end
238
260
 
239
261
 
262
+ # Validate the type is a Boolean, since Ruby lacks a Boolean class.
263
+ #
264
+ # @example
265
+ # validate_type_boolean :available, Boolean
266
+ #
267
+ def self.validate_type_boolean key
268
+ validate key, "#{key} is not the correct type. Expected a Boolean", lambda { |v| !!v == v }
269
+ type_keys << [key, Boolean]
270
+ end
271
+
272
+
240
273
  # Validate the presence of a particular key using a custom validation method.
241
- # If block is omitted, it only validates the existence of the key.
242
274
  #
243
275
  # @param key Key to be validated.
244
276
  # @param message Message that will be displayed when validation fails.
245
- # Will be appended after the key.
246
277
  # @yield [v] Value of the key.
247
278
  # The block must return truthy for it to pass the validation.
248
279
  #
249
280
  # @example
250
- # validate :first_name, 'does not exist', lambda { |v| !v.nil? }
281
+ # validate :first_name, 'first name is not Joe', lambda { |v| v != "Joe" }
251
282
  #
252
283
  def self.validate key, message, block
253
284
  raise ValidationError, "Provide validation method for key #{key}" unless block
@@ -257,13 +288,15 @@ module CouchPillow
257
288
 
258
289
  # This Document should only accept keys that are specified here.
259
290
  # The existence of these keys are optional, and won't trigger any validation
260
- # unless specified by the validate methods. Hashes passed to update and
261
- # initialize will be filtered through this list.
291
+ # unless specified by the validate methods. Hashes passed to {#update} and
292
+ # {#initialize} will be filtered through this list.
262
293
  #
263
294
  # If you don't specify a whitelist, Document will accept any keys, but
264
295
  # once you specify it, only those keys will be accepted and the rest will
265
296
  # be dropped.
266
297
  #
298
+ # @param list Whitelist of keys.
299
+ #
267
300
  # @example
268
301
  # whitelist :first_name, :last_name, :address
269
302
  #
@@ -277,6 +310,13 @@ module CouchPillow
277
310
  private
278
311
 
279
312
 
313
+ # Read the type of this Document. Internal use only.
314
+ #
315
+ def self._type
316
+ @type
317
+ end
318
+
319
+
280
320
  def self.rename_keys
281
321
  @rename_keys ||= []
282
322
  end
@@ -1,5 +1,5 @@
1
1
  module CouchPillow
2
2
  GEM_NAME = "couchpillow"
3
3
  NAME = "CouchPillow"
4
- VERSION = "0.3.9"
4
+ VERSION = "0.3.10"
5
5
  end
@@ -108,6 +108,21 @@ class TestDocument < Minitest::Test
108
108
  end
109
109
 
110
110
 
111
+ def test_validate_type_boolean
112
+ d = Class.new(Document) do
113
+ validate_type_boolean :abc
114
+ end.new
115
+
116
+ d.abc = "other type"
117
+ assert_raises Document::ValidationError do
118
+ d.save!
119
+ end
120
+
121
+ d.abc = true
122
+ d.save!
123
+ end
124
+
125
+
111
126
  def test_validate_type_also_ensures_types_on_create_integer
112
127
  klass = Class.new(Document) do
113
128
  type "test"
@@ -196,6 +211,34 @@ class TestDocument < Minitest::Test
196
211
  end
197
212
 
198
213
 
214
+ def test_validate_type_also_ensures_types_on_create_boolean
215
+ klass = Class.new(Document) do
216
+ type "test"
217
+ validate_type_boolean :abc
218
+ end
219
+
220
+ # true
221
+ CouchPillow.db.
222
+ expects(:get).
223
+ with('123').
224
+ returns( { '_type' => 'test', 'abc' => 1 } )
225
+
226
+ d = klass.get('123')
227
+ assert d
228
+ assert_equal true, d.abc
229
+
230
+ # false
231
+ CouchPillow.db.
232
+ expects(:get).
233
+ with('123').
234
+ returns( { '_type' => 'test', 'abc' => 0 } )
235
+
236
+ d = klass.get('123')
237
+ assert d
238
+ assert_equal false, d.abc
239
+ end
240
+
241
+
199
242
  def test_validate_custom
200
243
  d = Class.new(Document) do
201
244
  validate :xyz, "must be Numeric", lambda { |v| v.is_a? Numeric }
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.9
4
+ version: 0.3.10
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-11-05 00:00:00.000000000 Z
11
+ date: 2014-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest