couchpillow 0.2.0 → 0.3.0

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: efc31e058fa2047dbc8783576c421912a9d27e2f
4
- data.tar.gz: fd0d87f1485ec3b5419cc1063b09e3da2724a31e
3
+ metadata.gz: 3ee5cf9a4867bd4fa3ab19cb9ad0e1401fa94dce
4
+ data.tar.gz: 56f2d1e20e5a33755946c1e3c7d1147663be2cae
5
5
  SHA512:
6
- metadata.gz: 40bc500b602c184c47ebfc60bed01a45ffd3c3167ef8f0b8419d4af97e6a31fe22ef372705d3bc09019cc7d6f64ba226778a7a6075fc553084be18d66542beea
7
- data.tar.gz: a1c50b1e702d784e695794a770eeacf7daee7151182bd193f9ac58aef01d82e341096c661691a11ad7c0dd1abb8471d76a1a5ae462a2cfa0baa9f717c3ca41d9
6
+ metadata.gz: de31833c33e134ee6dd08dd35416b80b76249bf06d69187be8d8f7e74828e8698945adcd060187f9eefa2b89d6ff0836e992151b732e16fbbbc6b4fc877d4972
7
+ data.tar.gz: 78fb2de9b1356819ce87dbd754be463d79bf056c96175f3f92cc52442fe28d1d26594c51cb71f5f94c99c0269857dd2b457444b35c51b81eb718d0688a6034a4
data/README.markdown CHANGED
@@ -68,8 +68,9 @@ Overriding `CouchPillow::Document`
68
68
  Using validation
69
69
 
70
70
  class User < CouchPillow::Document
71
- type :user
71
+ type :user
72
72
  validate_presence :email
73
+ validate_type :first_name, String
73
74
  end
74
75
 
75
76
  CouchPillow.db = Couchbase.connect( bucket: 'default', host: 'localhost' )
@@ -95,6 +96,17 @@ Using custom validation blocks
95
96
  doc.phone = 123
96
97
  doc.save! # Success!
97
98
 
99
+ Using `rename` to rename keys. Useful to maintain document integrity.
100
+
101
+ class User < CouchPillow::Document
102
+ rename :username, :nickname
103
+ end
104
+ u = User.new( { :username => 'jdoe' } )
105
+ u.nickname # 'jdoe'
106
+
107
+
108
+ ## Design Docs and Views
109
+
98
110
  What about Design Docs and Views? They are outside the scope of CouchPillow.
99
111
  However, given a design doc named `my_design_doc` and a View named `by_email`,
100
112
  that returns documents as values, you can easily use it like this:
@@ -25,6 +25,7 @@ module CouchPillow
25
25
 
26
26
  raise TypeError if @data[:_type] && @data[:_type] != self.class._type
27
27
  @data[:_type] = self.class._type
28
+ rename!
28
29
  end
29
30
 
30
31
 
@@ -113,6 +114,15 @@ module CouchPillow
113
114
  end
114
115
 
115
116
 
117
+ def rename!
118
+ self.class.rename_keys.each do |from, to|
119
+ @data.has_key?(from) and
120
+ @data[to] = @data[from] and
121
+ @data.delete(from)
122
+ end
123
+ end
124
+
125
+
116
126
  def self.get id
117
127
  new(CouchPillow.db.get(id), id)
118
128
  end
@@ -128,8 +138,10 @@ module CouchPillow
128
138
  end
129
139
 
130
140
 
131
- def self.design_doc value
132
- @ddoc = value
141
+ # Rename an existing key to a new key. This is invoked right after initialize.
142
+ #
143
+ def self.rename from, to
144
+ rename_keys << [from.to_s.to_sym, to.to_s.to_sym]
133
145
  end
134
146
 
135
147
 
@@ -141,6 +153,13 @@ module CouchPillow
141
153
  end
142
154
 
143
155
 
156
+ # Validate the type of a particular key.
157
+ #
158
+ def self.validate_type key, type
159
+ validate key, "is not the correct type. Expected a #{type}", lambda { |v| v.is_a? type }
160
+ end
161
+
162
+
144
163
  # Validate the presence of a particular key using a custom validation method.
145
164
  # Implies the Document must contain the key.
146
165
  #
@@ -153,6 +172,11 @@ module CouchPillow
153
172
  private
154
173
 
155
174
 
175
+ def self.rename_keys
176
+ @rename_keys ||= []
177
+ end
178
+
179
+
156
180
  def self.validate_keys
157
181
  @validate_key ||= []
158
182
  end
@@ -1,5 +1,5 @@
1
1
  module CouchPillow
2
2
  GEM_NAME = "couchpillow"
3
3
  NAME = "CouchPillow"
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -5,12 +5,7 @@ require './lib/couchpillow.rb'
5
5
 
6
6
  class TestDocument < Minitest::Test
7
7
 
8
- class TestDoc < CouchPillow::Document
9
- type :test
10
- validate_presence :xyz
11
- validate :xyz, "must be Numeric", lambda { |v| v.is_a? Numeric }
12
- end
13
-
8
+ Document = CouchPillow::Document
14
9
 
15
10
  def mock_time
16
11
  return @time if @time
@@ -31,13 +26,13 @@ class TestDocument < Minitest::Test
31
26
 
32
27
 
33
28
  def test_create
34
- d = CouchPillow::Document.new
29
+ d = Document.new
35
30
  d.save!
36
31
  end
37
32
 
38
33
 
39
34
  def test_timestamp
40
- d = CouchPillow::Document.new({}, "1")
35
+ d = Document.new({}, "1")
41
36
  d.save!
42
37
  assert d.created_at
43
38
  assert d.updated_at
@@ -45,20 +40,24 @@ class TestDocument < Minitest::Test
45
40
 
46
41
 
47
42
  def test_type
48
- d = CouchPillow::Document.new({}, "1")
43
+ d = Document.new({}, "1")
49
44
  assert_equal "default", d._type
50
45
  end
51
46
 
52
47
 
53
48
  def test_type_subclasses
54
- d = TestDoc.new({}, "1")
49
+ d = Class.new(Document) do
50
+ type 'test'
51
+ end.new
55
52
  assert_equal "test", d._type
56
53
  end
57
54
 
58
55
 
59
56
  def test_validate_presence
60
- d = TestDoc.new
61
- assert_raises CouchPillow::Document::ValidationError do
57
+ d = Class.new(Document) do
58
+ validate_presence :xyz
59
+ end.new
60
+ assert_raises Document::ValidationError do
62
61
  d.save!
63
62
  end
64
63
 
@@ -67,15 +66,32 @@ class TestDocument < Minitest::Test
67
66
  end
68
67
 
69
68
 
69
+ def test_validate_type
70
+ d = Class.new(Document) do
71
+ validate_type :abc, Hash
72
+ end.new
73
+
74
+ d.abc = "other type"
75
+ assert_raises Document::ValidationError do
76
+ d.save!
77
+ end
78
+
79
+ d.abc = { :hello => "world" }
80
+ d.save!
81
+ end
82
+
83
+
70
84
  def test_validate_custom
71
- d = TestDoc.new
85
+ d = Class.new(Document) do
86
+ validate :xyz, "must be Numeric", lambda { |v| v.is_a? Numeric }
87
+ end.new
72
88
  d.xyz = "string"
73
- assert_raises CouchPillow::Document::ValidationError do
89
+ assert_raises Document::ValidationError do
74
90
  d.save!
75
91
  end
76
92
 
77
93
  d.xyz = {}
78
- assert_raises CouchPillow::Document::ValidationError do
94
+ assert_raises Document::ValidationError do
79
95
  d.save!
80
96
  end
81
97
 
@@ -86,13 +102,13 @@ class TestDocument < Minitest::Test
86
102
 
87
103
  def test_to_json
88
104
  mock_time
89
- d = CouchPillow::Document.new({}, "1")
105
+ d = Document.new({}, "1")
90
106
  assert_equal "{\"_id\":\"1\",\"created_at\":\"#{mock_time.to_s}\",\"_type\":\"default\"}", d.to_json
91
107
  end
92
108
 
93
109
 
94
110
  def test_brackets
95
- d = CouchPillow::Document.new({})
111
+ d = Document.new({})
96
112
  d[123] = 'test'
97
113
  assert_equal 'test', d[123]
98
114
 
@@ -105,4 +121,16 @@ class TestDocument < Minitest::Test
105
121
  assert_equal nil, d[:something_else]
106
122
  end
107
123
 
124
+
125
+ def test_rename_keys
126
+ d = Class.new(Document) do
127
+ rename :foo, :bar
128
+ end.new( { :foo => 123, :other => 'abc' } )
129
+ assert_equal 123, d[:bar]
130
+ assert_equal 123, d.bar
131
+ assert_equal nil, d[:foo]
132
+ refute d.respond_to?(:foo)
133
+ assert_equal 'abc', d.other
134
+ end
135
+
108
136
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couchpillow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Albert Tedja