arangodb-odm 0.1.1

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+ gem 'httparty'
3
+ gem 'json'
4
+
5
+ group :development do
6
+ gem "shoulda", ">= 0"
7
+ gem "bundler", "~> 1.0.0"
8
+ gem "jeweler", "~> 1.6.4"
9
+ gem "rcov", ">= 0"
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ httparty (0.8.1)
6
+ multi_json
7
+ multi_xml
8
+ jeweler (1.6.4)
9
+ bundler (~> 1.0)
10
+ git (>= 1.2.5)
11
+ rake
12
+ json (1.6.5)
13
+ multi_json (1.0.4)
14
+ multi_xml (0.4.1)
15
+ rake (0.9.2.2)
16
+ rcov (1.0.0)
17
+ shoulda (2.11.3)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ bundler (~> 1.0.0)
24
+ httparty
25
+ jeweler (~> 1.6.4)
26
+ json
27
+ rcov
28
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Oliver Kiessler
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # arangodb-odm
2
+
3
+ ## Example Code
4
+
5
+ ### Example with dynamic attributes
6
+
7
+ class ExampleDocument < ArangoDb::Base
8
+ collection :examples
9
+ end
10
+
11
+ ### Example with predefined attributes
12
+
13
+ class AnotherExampleDocument < ArangoDb::Base
14
+ collection :more_examples
15
+ db_attrs :foo, :bar # only these attributes will be saved
16
+ end
17
+
18
+ ## Usage
19
+
20
+ doc = ExampleDocument.new
21
+ doc.foo = "bar"
22
+ _id = doc.save
23
+
24
+ doc.foo = "bar2"
25
+ _rev = doc.save
26
+
27
+ success = doc.destroy
28
+
29
+ doc = ExampleDocument.find(_id)
30
+ doc2 = ExampleDocument.create("foo" => "bar")
31
+
32
+ all_document_handles = ExampleDocument.keys
33
+
34
+ ## Copyright
35
+
36
+ Copyright (c) 2012 Oliver Kiessler. See LICENSE.txt for
37
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "arangodb-odm"
18
+ gem.homepage = "http://github.com/okiess/arangodb-odm"
19
+ gem.license = "MIT"
20
+ gem.summary = "Ruby client for ArangoDB"
21
+ gem.description = "Ruby client for ArangoDB"
22
+ gem.email = "kiessler@inceedo.com"
23
+ gem.authors = ["Oliver Kiessler"]
24
+ gem.add_runtime_dependency 'httparty'
25
+ gem.add_runtime_dependency 'json'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ test.rcov_opts << '--exclude "gems/*"'
42
+ end
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "arangodb-odm #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,162 @@
1
+ require "rubygems"
2
+ require "httparty"
3
+ require "json"
4
+
5
+ module ArangoDb
6
+ class Transport
7
+ include HTTParty
8
+ base_uri 'http://localhost:8529'
9
+ end
10
+
11
+ class Document
12
+ attr_accessor :collection, :db_attrs, :location, :_id, :_rev
13
+ attr_reader :attributes
14
+
15
+ def initialize(collection, db_attrs = [])
16
+ @attributes = {}; @collection = collection; @db_attr_names = db_attrs
17
+ end
18
+
19
+ def is_new?; self._id.nil?; end
20
+ def to_json
21
+ if @db_attr_names and @db_attr_names.any?
22
+ values = {}
23
+ @db_attr_names.each {|a| values[a] = self.attributes[a.to_s]}
24
+ values.to_json
25
+ else
26
+ self.attributes.to_json
27
+ end
28
+ end
29
+ end
30
+
31
+ module Properties
32
+ def self.included(klass)
33
+ klass.extend ClassMethods
34
+ end
35
+
36
+ module ClassMethods
37
+ def collection(value)
38
+ (class << self; self; end).send(:define_method, 'collection') do
39
+ value.to_s
40
+ end
41
+ end
42
+
43
+ def transport(value)
44
+ (class << self; self; end).send(:define_method, 'transport') do
45
+ value
46
+ end
47
+ end
48
+
49
+ def target(value)
50
+ (class << self; self; end).send(:define_method, 'target') do
51
+ value
52
+ end
53
+ end
54
+
55
+ def db_attrs(*attrs)
56
+ @db_attr_names ||= []
57
+ @db_attr_names << attrs.collect {|a| a.to_s} if attrs.any?
58
+ (class << self; self; end).send(:define_method, 'db_attributes') do
59
+ @db_attr_names ? @db_attr_names.first : []
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ # Base class for ArangoDB documents. Subclass it to create your own collection
66
+ # specific document representations.
67
+ #
68
+ # Example:
69
+ #
70
+ # class ExampleDocument < ArangoDb::Base
71
+ # collection :examples
72
+ # end
73
+ class Base
74
+ include ArangoDb::Properties
75
+ transport ArangoDb::Transport
76
+ target ArangoDb::Document
77
+ db_attrs []
78
+ attr_reader :target
79
+
80
+ def initialize
81
+ @transport = self.class.transport
82
+ @target = self.class.target.new(self.class.collection, self.class.db_attributes)
83
+ end
84
+
85
+ def self.find(document_handle)
86
+ raise "missing document handle" if document_handle.nil?
87
+ res = transport.get("/_api/document/#{document_handle}")
88
+ res.code == 200 ? self.new.build(res.parsed_response) : nil
89
+ end
90
+
91
+ def self.keys
92
+ res = transport.get("/_api/document?collection=#{collection}")
93
+ res.code == 200 ? res.parsed_response['documents'] : []
94
+ end
95
+
96
+ def self.create(attributes = {})
97
+ document = self.new.build(attributes)
98
+ document.save
99
+ document
100
+ end
101
+
102
+ def build(attributes = {})
103
+ 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}"
107
+ self
108
+ end
109
+
110
+ def save
111
+ if @target.is_new?
112
+ res = @transport.post("/_api/document/?collection=#{@target.collection}&createCollection=true", :body => to_json)
113
+ if res.code == 201 || res.code == 202
114
+ @target.location = res.headers["location"]
115
+ @target._id = res.parsed_response["_id"]
116
+ @target._rev = res.headers["etag"]
117
+ return @target._id
118
+ end
119
+ else
120
+ res = @transport.put(@target.location, :body => to_json)
121
+ return (@target._rev = res.parsed_response['_rev'])
122
+ end
123
+ nil
124
+ end
125
+
126
+ def changed?
127
+ unless @target.is_new?
128
+ res = @transport.head(@target.location)
129
+ return (self._rev.to_s.gsub("\"", '') != res.headers['etag'].to_s.gsub("\"", ''))
130
+ end
131
+ false
132
+ end
133
+
134
+ def destroy
135
+ unless is_new?
136
+ res = @transport.delete(@target.location)
137
+ if res.code == 200
138
+ @target.location = nil; @target._id = nil; @target._rev = nil
139
+ return true
140
+ end
141
+ end
142
+ false
143
+ end
144
+
145
+ def to_json
146
+ @target.to_json
147
+ end
148
+
149
+ protected
150
+ # Delegates all unknown method invocations to the target.
151
+ def method_missing(method, *args, &block)
152
+ method = method.to_s
153
+ if method[-1, 1] == '='
154
+ @target.attributes[method.gsub('=', '')] = args.first unless ['_id=', '_rev='].include?(method)
155
+ else
156
+ val = @target.attributes[method]
157
+ val = @target.send(method.to_sym) rescue nil unless val
158
+ val
159
+ end
160
+ end
161
+ end
162
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'arangodb-odm'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,105 @@
1
+ require 'helper'
2
+
3
+ class ExampleDocument < ArangoDb::Base
4
+ collection :examples
5
+ end
6
+
7
+ # Example with predefined attributes
8
+ class AnotherExampleDocument < ArangoDb::Base
9
+ collection :more_examples
10
+ db_attrs :foo, :bar
11
+ end
12
+
13
+ class TestArangoDbRb < Test::Unit::TestCase
14
+ should "class should have a collection" do
15
+ assert_equal ExampleDocument.collection, 'examples'
16
+ assert_equal AnotherExampleDocument.collection, 'more_examples'
17
+ end
18
+
19
+ should "create a new document" do
20
+ doc = ExampleDocument.new
21
+ doc.foo = "bar"
22
+ doc.test = 1
23
+ doc.list = [1, 2, 3]
24
+
25
+ assert_nil doc._id
26
+ assert_nil doc._rev
27
+ assert_nil doc.location
28
+
29
+ _id = doc.save
30
+ assert_not_nil _id
31
+ assert_not_nil doc._id
32
+ assert_not_nil doc._rev
33
+ assert_not_nil doc.location
34
+
35
+ doc = ExampleDocument.find(_id)
36
+ assert_equal doc._id, _id
37
+ assert_equal doc.foo, "bar"
38
+ assert_equal doc.test, 1
39
+ assert_equal doc.list, [1, 2, 3]
40
+
41
+ doc2 = AnotherExampleDocument.new
42
+ doc2.foo = 'bar'
43
+ doc2.bar = 'foo'
44
+ doc2.foo2 = 'bar2' # won't be saved => not in predefined db_attrs
45
+ _id = doc2.save
46
+ assert_not_nil _id
47
+ assert_not_nil doc2._id
48
+ assert_not_nil doc2._rev
49
+ assert_not_nil doc2.location
50
+
51
+ doc3 = AnotherExampleDocument.find(_id)
52
+ assert_nil doc3.foo2
53
+ assert_equal doc3.foo, 'bar'
54
+ assert_equal doc3.bar, 'foo'
55
+ end
56
+
57
+ should "create a document on the create class method" do
58
+ doc = ExampleDocument.create(:foo => "bar", :test => 1, :list => [1, 2, 3])
59
+ assert_not_nil doc._id
60
+ assert_not_nil doc._rev
61
+ assert_not_nil doc.location
62
+ assert_equal doc.foo, "bar"
63
+ assert_equal doc.test, 1
64
+ assert_equal doc.list, [1, 2, 3]
65
+ end
66
+
67
+ should "find a document by id" do
68
+ doc = ExampleDocument.create(:foo => "bar", :test => 1, :list => [1, 2, 3])
69
+ assert_not_nil doc._id
70
+ doc2 = ExampleDocument.find(doc._id)
71
+ assert_equal doc._id, doc2._id
72
+ end
73
+
74
+ should "update a document" do
75
+ doc = ExampleDocument.create(:foo => "bar", :test => 1, :list => [1, 2, 3])
76
+ doc2 = ExampleDocument.find(doc._id)
77
+
78
+ assert_not_nil doc._id
79
+ doc.foo = "bar2"
80
+ _rev_before = doc._rev
81
+ _rev = doc.save
82
+ assert _rev != _rev_before
83
+ assert_equal doc.foo, 'bar2'
84
+
85
+ assert_not_nil doc2
86
+ assert doc2.changed?
87
+ end
88
+
89
+ should "destroy a document" do
90
+ doc = ExampleDocument.create(:foo => "bar", :test => 1, :list => [1, 2, 3])
91
+ assert_not_nil doc._id
92
+ _id = doc._id
93
+ assert doc.destroy
94
+ doc2 = ExampleDocument.find(_id)
95
+ assert_nil doc2
96
+ end
97
+
98
+ should "get all document handles" do
99
+ doc = ExampleDocument.create(:foo => "bar", :test => 1, :list => [1, 2, 3])
100
+ all_handles = ExampleDocument.keys
101
+ assert_not_nil all_handles
102
+ assert all_handles.include?(doc.location)
103
+ assert doc.destroy
104
+ end
105
+ end
metadata ADDED
@@ -0,0 +1,190 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arangodb-odm
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Oliver Kiessler
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-10-03 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ requirement: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ hash: 3
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ version_requirements: *id001
31
+ name: httparty
32
+ prerelease: false
33
+ type: :runtime
34
+ - !ruby/object:Gem::Dependency
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ hash: 3
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ version_requirements: *id002
45
+ name: json
46
+ prerelease: false
47
+ type: :runtime
48
+ - !ruby/object:Gem::Dependency
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ version_requirements: *id003
59
+ name: shoulda
60
+ prerelease: false
61
+ type: :development
62
+ - !ruby/object:Gem::Dependency
63
+ requirement: &id004 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ hash: 23
69
+ segments:
70
+ - 1
71
+ - 0
72
+ - 0
73
+ version: 1.0.0
74
+ version_requirements: *id004
75
+ name: bundler
76
+ prerelease: false
77
+ type: :development
78
+ - !ruby/object:Gem::Dependency
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ~>
83
+ - !ruby/object:Gem::Version
84
+ hash: 7
85
+ segments:
86
+ - 1
87
+ - 6
88
+ - 4
89
+ version: 1.6.4
90
+ version_requirements: *id005
91
+ name: jeweler
92
+ prerelease: false
93
+ type: :development
94
+ - !ruby/object:Gem::Dependency
95
+ requirement: &id006 !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ version_requirements: *id006
105
+ name: rcov
106
+ prerelease: false
107
+ type: :development
108
+ - !ruby/object:Gem::Dependency
109
+ requirement: &id007 !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ version_requirements: *id007
119
+ name: httparty
120
+ prerelease: false
121
+ type: :runtime
122
+ - !ruby/object:Gem::Dependency
123
+ requirement: &id008 !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ hash: 3
129
+ segments:
130
+ - 0
131
+ version: "0"
132
+ version_requirements: *id008
133
+ name: json
134
+ prerelease: false
135
+ type: :runtime
136
+ description: Ruby client for ArangoDB
137
+ email: kiessler@inceedo.com
138
+ executables: []
139
+
140
+ extensions: []
141
+
142
+ extra_rdoc_files:
143
+ - LICENSE.txt
144
+ - README.md
145
+ files:
146
+ - .document
147
+ - Gemfile
148
+ - Gemfile.lock
149
+ - LICENSE.txt
150
+ - README.md
151
+ - Rakefile
152
+ - VERSION
153
+ - lib/arangodb-odm.rb
154
+ - test/helper.rb
155
+ - test/test_arangodb-odm.rb
156
+ homepage: http://github.com/okiess/arangodb-odm
157
+ licenses:
158
+ - MIT
159
+ post_install_message:
160
+ rdoc_options: []
161
+
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ none: false
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ hash: 3
170
+ segments:
171
+ - 0
172
+ version: "0"
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ none: false
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ hash: 3
179
+ segments:
180
+ - 0
181
+ version: "0"
182
+ requirements: []
183
+
184
+ rubyforge_project:
185
+ rubygems_version: 1.8.24
186
+ signing_key:
187
+ specification_version: 3
188
+ summary: Ruby client for ArangoDB
189
+ test_files: []
190
+