couchpillow 0.4.2 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d84be9337a6dfe5e7c16a73c9b4a7158121270b
4
- data.tar.gz: bb05ae6199ee43110587cef9d50ebf1ddb68eeb2
3
+ metadata.gz: 8766ff55f2a06b3e6a73581eb02a5add86407868
4
+ data.tar.gz: e4b22c670eefa9f68deb5f4bf5de5db8dfa03613
5
5
  SHA512:
6
- metadata.gz: 4beacbd56fbcd07aff7c44bf25edaf557857c85d8ff253194286fd42b2f1b39a1388d0aad69f834b4ff2d45d780fb245ad69787d189fa75bd6619678f3c26a55
7
- data.tar.gz: 329f5fe5aecfb16772275b08533ec80b691868183d65e69df4fa51e228bc0f0dd50213270fbab93363d0442c7a415e2194adb06b205563a156e7c697a195f862
6
+ metadata.gz: d8fd8cec7c43b888807d46a1faac51ed83087b1d7a2af8a85ebff8ec517b8937a8b26bf8cb76bc06213eec4c711d4f51de41190b7c4abde78c7c21acce6cd166
7
+ data.tar.gz: c793792ad45b53674963350e3b740d0b3310e1a10a5b8379a387cee46692c9d1e21d79625e1c44ef13054c9071c9bf485fb218d050a641bc46928d4393bd344d
data/README.markdown CHANGED
@@ -2,11 +2,8 @@
2
2
 
3
3
  Light and comfortable Document integrity tool for Couchbase Server.
4
4
 
5
-
6
- ## Why CouchPillow?
7
-
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.
5
+ CouchPillow is a document integrity tool for Couchbase Documents to make sure
6
+ that all current and existing documents can work nicely with the current code.
10
7
 
11
8
  CouchPillow separates itself from the database drivers, making it light and
12
9
  independent from the implementation. Although it is initially designed to work
@@ -40,7 +37,7 @@ long as they `respond_to?` the `set`, `delete`, `replace`, and `get` methods.
40
37
  doc.save!
41
38
 
42
39
  # {
43
- # '_id': 'my_document_fb579b265cc005c47ff420a5c2a15d2b',
40
+ # '_id': 'my_document::fb579b265cc005c47ff420a5c2a15d2b',
44
41
  # '_type': 'my_document',
45
42
  # 'stuff': 'hello',
46
43
  # '_created_at': '2014-07-04 00:00:00 UTC'
@@ -50,10 +47,10 @@ long as they `respond_to?` the `set`, `delete`, `replace`, and `get` methods.
50
47
 
51
48
  Retrieving Documents:
52
49
 
53
- doc = MyDocument.get('123')
50
+ doc = MyDocument.get('my_document::fb579b265cc005c47ff420a5c2a15d2b')
54
51
  doc.stuff # 'hello'
55
52
 
56
- Specifying custom id:
53
+ Specifying custom id if the auto-generated id is too confusing:
57
54
 
58
55
  class User < CouchPillow::Document
59
56
  type :user
@@ -116,14 +113,14 @@ List of Attribute Directives:
116
113
  * `content(&block)`
117
114
 
118
115
  Custom validation method to check the value of the attribute. This is useful
119
- in cases where you only want certain values to be stored (e.g a number between
120
- 1-10 only)
116
+ in cases where you only want certain values to be stored (e.g a number
117
+ between 1-10 only)
121
118
 
122
119
 
123
120
  ### Migration
124
121
 
125
122
  Using `rename` to rename keys. Useful to maintain document integrity
126
- from a migration.
123
+ after a migration.
127
124
 
128
125
  class User < CouchPillow::Document
129
126
  rename :username, :nickname
@@ -132,6 +129,11 @@ from a migration.
132
129
  u = User.new( { :username => 'jdoe' } )
133
130
  u.nickname # 'jdoe'
134
131
 
132
+ Rename triggers per-document basis. You can use this on a separate script
133
+ that queries each document in the database and updates them, or you can
134
+ simply use this inside your application code, so it only migrates the document
135
+ as it reads them.
136
+
135
137
 
136
138
 
137
139
  ## Design Docs and Views
@@ -108,21 +108,7 @@ module CouchPillow
108
108
  def trigger_type_directive value
109
109
  if @type
110
110
  # Run auto-conversion first.
111
- if @auto_convert
112
- if @type == Integer
113
- value = Integer(value)
114
- elsif @type == Float
115
- value = Float(value)
116
- elsif @type == String
117
- value = String(value)
118
- elsif @type == Array
119
- value = Array(value)
120
- elsif @type == Time && !value.is_a?(Time)
121
- value = Time.parse(value)
122
- elsif @type == CouchPillow::Boolean
123
- value = value == 0 || value.to_s.downcase == "false" || !value ? false : true
124
- end
125
- end
111
+ value = trigger_auto_convert_directive(value)
126
112
 
127
113
  if @type == CouchPillow::Boolean
128
114
  raise ValidationError unless !!value == value
@@ -135,6 +121,28 @@ module CouchPillow
135
121
  end
136
122
 
137
123
 
124
+ # Auto convert the value
125
+ #
126
+ def trigger_auto_convert_directive value
127
+ if @auto_convert
128
+ if @type == Integer
129
+ value = Integer(value)
130
+ elsif @type == Float
131
+ value = Float(value)
132
+ elsif @type == String
133
+ value = String(value)
134
+ elsif @type == Array
135
+ value = Array(value)
136
+ elsif @type == Time && !value.is_a?(Time)
137
+ value = Time.parse(value)
138
+ elsif @type == CouchPillow::Boolean
139
+ value = value == 0 || value.to_s.downcase == "false" || !value ? false : true
140
+ end
141
+ end
142
+ value
143
+ end
144
+
145
+
138
146
  # Run the validation directives, except required directive.
139
147
  # First it executes the {default} directive, then {auto_convert} to type,
140
148
  # then {type} validation, then finally the {content} directive.
@@ -4,7 +4,7 @@ module CouchPillow
4
4
 
5
5
  # Declares a new Attribute
6
6
  #
7
- def attribute attr
7
+ def attribute attr, &block
8
8
  attr = attr.to_s.to_sym
9
9
  new_attr = Attribute.new(attr)
10
10
  attributes[attr] = new_attr
@@ -17,6 +17,8 @@ module CouchPillow
17
17
  @data[attr] = val
18
18
  end
19
19
 
20
+ new_attr.instance_eval &block if block
21
+
20
22
  new_attr
21
23
  end
22
24
 
@@ -32,6 +32,8 @@ module CouchPillow
32
32
 
33
33
  rename!
34
34
  whitelist!
35
+ assign_defaults!
36
+ auto_convert!
35
37
  end
36
38
 
37
39
 
@@ -54,7 +56,7 @@ module CouchPillow
54
56
 
55
57
  # Save this document to the server
56
58
  #
57
- def save!
59
+ def save! opts = {}
58
60
  whitelist!
59
61
  sort!
60
62
  timestamp!
@@ -62,7 +64,7 @@ module CouchPillow
62
64
  to_save = @data.merge({
63
65
  :_type => self.class._type
64
66
  })
65
- CouchPillow.db.set @id, to_save
67
+ CouchPillow.db.set(@id, to_save, opts)
66
68
  end
67
69
 
68
70
 
@@ -136,6 +138,24 @@ module CouchPillow
136
138
  end
137
139
 
138
140
 
141
+ # Assign default value
142
+ #
143
+ def assign_defaults!
144
+ self.class.attributes.each do |k, attr|
145
+ @data[k] = attr.trigger_default_directive if !has?(k) && attr.has_default?
146
+ end
147
+ end
148
+
149
+
150
+ # Auto convert.
151
+ #
152
+ def auto_convert!
153
+ self.class.attributes.each do |k, attr|
154
+ @data[k] = attr.trigger_auto_convert_directive(@data[k]) if has?(k)
155
+ end
156
+ end
157
+
158
+
139
159
  # Go through each attribute, and validate the values.
140
160
  # Validation also perform auto-conversion if auto-conversion is enabled
141
161
  # for that attribute.
@@ -1,5 +1,5 @@
1
1
  module CouchPillow
2
2
  GEM_NAME = "couchpillow"
3
3
  NAME = "CouchPillow"
4
- VERSION = "0.4.2"
4
+ VERSION = "0.4.3"
5
5
  end
data/test/helper.rb CHANGED
@@ -12,7 +12,7 @@ class FakeCouchbaseServer
12
12
  end
13
13
 
14
14
 
15
- def set id, data
15
+ def set id, data, opts = {}
16
16
  @storage[id] = data
17
17
  end
18
18
 
@@ -323,4 +323,74 @@ class TestDocument < Minitest::Test
323
323
  k.save!
324
324
  end
325
325
 
326
+
327
+ def test_auto_convert_triggers_on_initialize
328
+ d = Class.new(Document) do
329
+ type 'test'
330
+ attribute(:foo)
331
+ .type(String).auto_convert
332
+ attribute(:not_auto)
333
+ .type(Integer)
334
+ end.new( foo: 1, not_auto: "100" )
335
+
336
+ assert_equal "1", d.foo
337
+ assert_equal "100", d.not_auto
338
+ end
339
+
340
+
341
+ def test_dsl_style
342
+ d = Class.new(Document) do
343
+ type 'test'
344
+
345
+ attribute :foo do
346
+ type String
347
+ auto_convert
348
+ required
349
+ end
350
+
351
+ attribute :bar do
352
+ type Integer
353
+ auto_convert
354
+ required
355
+ end
356
+ end.new( foo: 1, bar: "100" )
357
+
358
+ assert_equal "1", d.foo
359
+ assert_equal 100, d.bar
360
+ end
361
+
362
+
363
+ def test_assign_default_value_at_creation
364
+ d = Class.new(Document) do
365
+ type 'test'
366
+
367
+ attribute :foo do
368
+ type String
369
+ auto_convert
370
+ required
371
+ default { "tester" }
372
+ end
373
+
374
+ end.new
375
+
376
+ assert_equal "tester", d.foo
377
+ end
378
+
379
+
380
+ def test_ttl_support
381
+ d = Class.new(Document) do
382
+ type 'test'
383
+
384
+ attribute :foo do
385
+ type String
386
+ auto_convert
387
+ required
388
+ default { "tester" }
389
+ end
390
+
391
+ end.new
392
+
393
+ d.save! ttl: 100
394
+ end
395
+
326
396
  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.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Albert Tedja
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-19 00:00:00.000000000 Z
11
+ date: 2015-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  version: '0'
94
94
  requirements: []
95
95
  rubyforge_project:
96
- rubygems_version: 2.4.5
96
+ rubygems_version: 2.4.6
97
97
  signing_key:
98
98
  specification_version: 4
99
99
  summary: Document wrapper for Couchbase