arangodb-odm 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +49 -0
- data/VERSION +1 -1
- data/lib/arangodb-odm.rb +74 -14
- data/test/helper.rb +3 -0
- data/test/test_arangodb-odm.rb +54 -5
- metadata +4 -4
data/README.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# arangodb-odm
|
2
2
|
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
gem install arangodb-odm
|
6
|
+
|
7
|
+
## Configuration
|
8
|
+
|
9
|
+
ArangoDb::Transport.base_uri 'http://localhost:8529'
|
10
|
+
|
3
11
|
## Example Code
|
4
12
|
|
5
13
|
### Example with dynamic attributes
|
@@ -31,6 +39,47 @@
|
|
31
39
|
|
32
40
|
all_document_handles = ExampleDocument.keys
|
33
41
|
|
42
|
+
## Callbacks
|
43
|
+
|
44
|
+
class ExampleDocument < ArangoDb::Base
|
45
|
+
collection :examples
|
46
|
+
|
47
|
+
before_create :add_something
|
48
|
+
after_create :do_something_else
|
49
|
+
|
50
|
+
before_destroy :method1
|
51
|
+
after_destroy :method2
|
52
|
+
|
53
|
+
before_save :change_something
|
54
|
+
after_save :change_something_else
|
55
|
+
|
56
|
+
# Called before create/save
|
57
|
+
# must return true if the document is valid
|
58
|
+
def validate
|
59
|
+
not self.foo.nil?
|
60
|
+
end
|
61
|
+
|
62
|
+
def add_something
|
63
|
+
self.something = "other"
|
64
|
+
end
|
65
|
+
|
66
|
+
def do_something_else; end
|
67
|
+
|
68
|
+
def method1
|
69
|
+
puts self._id # not nil
|
70
|
+
end
|
71
|
+
|
72
|
+
def method2
|
73
|
+
puts self._id # is nil
|
74
|
+
end
|
75
|
+
|
76
|
+
def change_something
|
77
|
+
self.foo = 'bar2'
|
78
|
+
end
|
79
|
+
|
80
|
+
def change_something_else; end
|
81
|
+
end
|
82
|
+
|
34
83
|
## Copyright
|
35
84
|
|
36
85
|
Copyright (c) 2012 Oliver Kiessler. See LICENSE.txt for
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/arangodb-odm.rb
CHANGED
@@ -52,6 +52,42 @@ module ArangoDb
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
+
def before_create(value)
|
56
|
+
self.send(:define_method, 'before_create') do
|
57
|
+
value
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def after_create(value)
|
62
|
+
self.send(:define_method, 'after_create') do
|
63
|
+
value
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def before_save(value)
|
68
|
+
self.send(:define_method, 'before_save') do
|
69
|
+
value
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def after_save(value)
|
74
|
+
self.send(:define_method, 'after_save') do
|
75
|
+
value
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def before_destroy(value)
|
80
|
+
self.send(:define_method, 'before_destroy') do
|
81
|
+
value
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def after_destroy(value)
|
86
|
+
self.send(:define_method, 'after_destroy') do
|
87
|
+
value
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
55
91
|
def db_attrs(*attrs)
|
56
92
|
@db_attr_names ||= []
|
57
93
|
@db_attr_names << attrs.collect {|a| a.to_s} if attrs.any?
|
@@ -101,28 +137,46 @@ module ArangoDb
|
|
101
137
|
|
102
138
|
def build(attributes = {})
|
103
139
|
attributes.each {|k, v| self.send("#{k}=".to_sym, v) unless ['_id', '_rev'].include?(k)}
|
104
|
-
self.target._id = attributes['_id']
|
105
|
-
self.target._rev = attributes['_rev']
|
106
|
-
self.target.location = "/_api/document/#{self.target._id}"
|
140
|
+
self.target._id = attributes['_id'] if attributes['_id']
|
141
|
+
self.target._rev = attributes['_rev'] if attributes['_rev']
|
142
|
+
self.target.location = "/_api/document/#{self.target._id}" if attributes['_id']
|
107
143
|
self
|
108
144
|
end
|
109
145
|
|
110
146
|
def save
|
111
|
-
if
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
147
|
+
if validate
|
148
|
+
if @target.is_new?
|
149
|
+
if self.respond_to?(:before_create) and self.before_create
|
150
|
+
self.send(self.before_create.to_sym)
|
151
|
+
end
|
152
|
+
res = @transport.post("/_api/document/?collection=#{@target.collection}&createCollection=true", :body => to_json)
|
153
|
+
if res.code == 201 || res.code == 202
|
154
|
+
@target.location = res.headers["location"]
|
155
|
+
@target._id = res.parsed_response["_id"]
|
156
|
+
@target._rev = res.headers["etag"]
|
157
|
+
if self.respond_to?(:after_create) and self.after_create
|
158
|
+
self.send(self.after_create.to_sym)
|
159
|
+
end
|
160
|
+
return @target._id
|
161
|
+
end
|
162
|
+
else
|
163
|
+
if self.respond_to?(:before_save) and self.before_save
|
164
|
+
self.send(self.before_save.to_sym)
|
165
|
+
end
|
166
|
+
res = @transport.put(@target.location, :body => to_json)
|
167
|
+
@target._rev = res.parsed_response['_rev']
|
168
|
+
if self.respond_to?(:after_save) and self.after_save
|
169
|
+
self.send(self.after_save.to_sym)
|
170
|
+
end
|
171
|
+
return @target._rev
|
118
172
|
end
|
119
|
-
else
|
120
|
-
res = @transport.put(@target.location, :body => to_json)
|
121
|
-
return (@target._rev = res.parsed_response['_rev'])
|
122
173
|
end
|
123
174
|
nil
|
124
175
|
end
|
125
|
-
|
176
|
+
|
177
|
+
# Override to run your own validations.
|
178
|
+
def validate; true; end
|
179
|
+
|
126
180
|
def changed?
|
127
181
|
unless @target.is_new?
|
128
182
|
res = @transport.head(@target.location)
|
@@ -133,9 +187,15 @@ module ArangoDb
|
|
133
187
|
|
134
188
|
def destroy
|
135
189
|
unless is_new?
|
190
|
+
if self.respond_to?(:before_destroy) and self.before_destroy
|
191
|
+
self.send(self.before_destroy.to_sym)
|
192
|
+
end
|
136
193
|
res = @transport.delete(@target.location)
|
137
194
|
if res.code == 200
|
138
195
|
@target.location = nil; @target._id = nil; @target._rev = nil
|
196
|
+
if self.respond_to?(:after_destroy) and self.after_destroy
|
197
|
+
self.send(self.after_destroy.to_sym)
|
198
|
+
end
|
139
199
|
return true
|
140
200
|
end
|
141
201
|
end
|
data/test/helper.rb
CHANGED
@@ -14,5 +14,8 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
14
14
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
15
|
require 'arangodb-odm'
|
16
16
|
|
17
|
+
# Set your ArangoDB host...
|
18
|
+
ArangoDb::Transport.base_uri 'http://localhost:8529'
|
19
|
+
|
17
20
|
class Test::Unit::TestCase
|
18
21
|
end
|
data/test/test_arangodb-odm.rb
CHANGED
@@ -2,12 +2,45 @@ require 'helper'
|
|
2
2
|
|
3
3
|
class ExampleDocument < ArangoDb::Base
|
4
4
|
collection :examples
|
5
|
+
|
6
|
+
before_create :add_something
|
7
|
+
after_create :do_something_else
|
8
|
+
|
9
|
+
before_destroy :method1
|
10
|
+
after_destroy :method2
|
11
|
+
|
12
|
+
def validate
|
13
|
+
not self.foo.nil?
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_something
|
17
|
+
self.something = "other"
|
18
|
+
end
|
19
|
+
|
20
|
+
def do_something_else; end
|
21
|
+
|
22
|
+
def method1
|
23
|
+
# puts self._id
|
24
|
+
end
|
25
|
+
|
26
|
+
def method2
|
27
|
+
# puts self._id
|
28
|
+
end
|
5
29
|
end
|
6
30
|
|
7
31
|
# Example with predefined attributes
|
8
32
|
class AnotherExampleDocument < ArangoDb::Base
|
9
33
|
collection :more_examples
|
10
34
|
db_attrs :foo, :bar
|
35
|
+
|
36
|
+
before_save :change_something
|
37
|
+
after_save :change_something_else
|
38
|
+
|
39
|
+
def change_something
|
40
|
+
self.foo = 'bar2'
|
41
|
+
end
|
42
|
+
|
43
|
+
def change_something_else; end
|
11
44
|
end
|
12
45
|
|
13
46
|
class TestArangoDbRb < Test::Unit::TestCase
|
@@ -21,23 +54,23 @@ class TestArangoDbRb < Test::Unit::TestCase
|
|
21
54
|
doc.foo = "bar"
|
22
55
|
doc.test = 1
|
23
56
|
doc.list = [1, 2, 3]
|
24
|
-
|
57
|
+
|
25
58
|
assert_nil doc._id
|
26
59
|
assert_nil doc._rev
|
27
60
|
assert_nil doc.location
|
28
|
-
|
61
|
+
|
29
62
|
_id = doc.save
|
30
63
|
assert_not_nil _id
|
31
64
|
assert_not_nil doc._id
|
32
65
|
assert_not_nil doc._rev
|
33
66
|
assert_not_nil doc.location
|
34
|
-
|
67
|
+
|
35
68
|
doc = ExampleDocument.find(_id)
|
36
69
|
assert_equal doc._id, _id
|
37
70
|
assert_equal doc.foo, "bar"
|
38
71
|
assert_equal doc.test, 1
|
39
72
|
assert_equal doc.list, [1, 2, 3]
|
40
|
-
|
73
|
+
|
41
74
|
doc2 = AnotherExampleDocument.new
|
42
75
|
doc2.foo = 'bar'
|
43
76
|
doc2.bar = 'foo'
|
@@ -47,10 +80,12 @@ class TestArangoDbRb < Test::Unit::TestCase
|
|
47
80
|
assert_not_nil doc2._id
|
48
81
|
assert_not_nil doc2._rev
|
49
82
|
assert_not_nil doc2.location
|
83
|
+
assert_equal doc2.foo, 'bar'
|
84
|
+
_rev = doc2.save
|
50
85
|
|
51
86
|
doc3 = AnotherExampleDocument.find(_id)
|
52
87
|
assert_nil doc3.foo2
|
53
|
-
assert_equal doc3.foo, '
|
88
|
+
assert_equal doc3.foo, 'bar2'
|
54
89
|
assert_equal doc3.bar, 'foo'
|
55
90
|
end
|
56
91
|
|
@@ -64,6 +99,20 @@ class TestArangoDbRb < Test::Unit::TestCase
|
|
64
99
|
assert_equal doc.list, [1, 2, 3]
|
65
100
|
end
|
66
101
|
|
102
|
+
should "create a document and use callbacks" do
|
103
|
+
doc = ExampleDocument.new
|
104
|
+
doc.a = "b"
|
105
|
+
doc.foo = "bar"
|
106
|
+
assert_nil doc.something
|
107
|
+
_id = doc.save
|
108
|
+
assert_not_nil doc.something
|
109
|
+
|
110
|
+
doc = ExampleDocument.find(_id)
|
111
|
+
assert_equal doc._id, _id
|
112
|
+
assert_equal doc.a, "b"
|
113
|
+
assert_equal doc.something, "other"
|
114
|
+
end
|
115
|
+
|
67
116
|
should "find a document by id" do
|
68
117
|
doc = ExampleDocument.create(:foo => "bar", :test => 1, :list => [1, 2, 3])
|
69
118
|
assert_not_nil doc._id
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arangodb-odm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Oliver Kiessler
|