arangodb-odm 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -4,7 +4,8 @@ gem 'json'
4
4
 
5
5
  group :development do
6
6
  gem "shoulda", ">= 0"
7
- gem "bundler", "~> 1.0.0"
7
+ gem "bundler", "~> 1.1.0"
8
8
  gem "jeweler", "~> 1.6.4"
9
9
  gem "rcov", ">= 0"
10
+ gem "rdoc", "~> 2.4.2"
10
11
  end
@@ -1,28 +1,50 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ activesupport (3.2.9)
5
+ i18n (~> 0.6)
6
+ multi_json (~> 1.0)
7
+ bourne (1.1.2)
8
+ mocha (= 0.10.5)
4
9
  git (1.2.5)
5
- httparty (0.8.1)
6
- multi_json
10
+ hoe (3.3.1)
11
+ rake (>= 0.8, < 11.0)
12
+ httparty (0.9.0)
13
+ multi_json (~> 1.0)
7
14
  multi_xml
15
+ i18n (0.6.1)
8
16
  jeweler (1.6.4)
9
17
  bundler (~> 1.0)
10
18
  git (>= 1.2.5)
11
19
  rake
12
- json (1.6.5)
13
- multi_json (1.0.4)
14
- multi_xml (0.4.1)
15
- rake (0.9.2.2)
20
+ json (1.7.5)
21
+ metaclass (0.0.1)
22
+ minitest (1.7.2)
23
+ mocha (0.10.5)
24
+ metaclass (~> 0.0.1)
25
+ multi_json (1.4.0)
26
+ multi_xml (0.5.1)
27
+ rake (10.0.2)
16
28
  rcov (1.0.0)
17
- shoulda (2.11.3)
29
+ rdoc (2.4.3)
30
+ hoe (>= 1.12.1)
31
+ minitest (~> 1.3)
32
+ shoulda (3.3.2)
33
+ shoulda-context (~> 1.0.1)
34
+ shoulda-matchers (~> 1.4.1)
35
+ shoulda-context (1.0.1)
36
+ shoulda-matchers (1.4.2)
37
+ activesupport (>= 3.0.0)
38
+ bourne (~> 1.1.2)
18
39
 
19
40
  PLATFORMS
20
41
  ruby
21
42
 
22
43
  DEPENDENCIES
23
- bundler (~> 1.0.0)
44
+ bundler (~> 1.1.0)
24
45
  httparty
25
46
  jeweler (~> 1.6.4)
26
47
  json
27
48
  rcov
49
+ rdoc (~> 2.4.2)
28
50
  shoulda
data/README.md CHANGED
@@ -13,7 +13,7 @@
13
13
  ### Example with dynamic attributes
14
14
 
15
15
  class ExampleDocument < ArangoDb::Base
16
- collection :examples
16
+ collection :examples
17
17
  end
18
18
 
19
19
  ### Example with predefined attributes
@@ -25,6 +25,8 @@
25
25
 
26
26
  ## Usage
27
27
 
28
+ ### CRUD
29
+
28
30
  doc = ExampleDocument.new
29
31
  doc.foo = "bar"
30
32
  _id = doc.save
@@ -38,6 +40,23 @@
38
40
  doc2 = ExampleDocument.create("foo" => "bar")
39
41
 
40
42
  all_document_handles = ExampleDocument.keys
43
+
44
+ ### Simple Queries
45
+
46
+ #### All
47
+
48
+ ExampleDocument.all
49
+ ExampleDocument.skip(1).limit(2).all
50
+
51
+ #### All by example
52
+
53
+ ExampleDocument.where('foo' => 'bar').all
54
+ ExampleDocument.where('foo' => 'bar').skip(10).limit(10).all
55
+ ExampleDocument.where('foo' => 'bar').where('a' => 'b').skip(10).limit(10).all
56
+
57
+ #### First by example
58
+
59
+ ExampleDocument.where('foo' => 'bar', 'a' => 'b').first
41
60
 
42
61
  ## Callbacks
43
62
 
data/Rakefile CHANGED
@@ -43,7 +43,7 @@ end
43
43
 
44
44
  task :default => :test
45
45
 
46
- require 'rake/rdoctask'
46
+ require 'rdoc/task'
47
47
  Rake::RDocTask.new do |rdoc|
48
48
  version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
49
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -1,6 +1,7 @@
1
1
  require "rubygems"
2
2
  require "httparty"
3
3
  require "json"
4
+ require "queries"
4
5
 
5
6
  module ArangoDb
6
7
  class Transport
@@ -108,6 +109,7 @@ module ArangoDb
108
109
  # end
109
110
  class Base
110
111
  include ArangoDb::Properties
112
+ extend ArangoDb::Queries::ClassMethods
111
113
  transport ArangoDb::Transport
112
114
  target ArangoDb::Document
113
115
  db_attrs []
@@ -0,0 +1,96 @@
1
+ module ArangoDb
2
+ module Queries
3
+ class QueryResult < Array
4
+ end
5
+
6
+ class Query
7
+ def initialize(model)
8
+ @model = model
9
+ end
10
+
11
+ def where(hash)
12
+ @where ||= {}
13
+ @where.merge!(hash)
14
+ self
15
+ end
16
+
17
+ def limit(number)
18
+ @limit = number
19
+ self
20
+ end
21
+
22
+ def skip(number)
23
+ @skip = number
24
+ self
25
+ end
26
+
27
+ def first
28
+ @model.first(query_parameters)
29
+ end
30
+
31
+ def all
32
+ @model.all(query_parameters)
33
+ end
34
+
35
+ private
36
+ def query_parameters
37
+ options = {}
38
+ options['limit'] = @limit if @limit
39
+ options['skip'] = @skip if @skip
40
+ options['example'] = @where if @where
41
+ options
42
+ end
43
+ end
44
+
45
+ module ClassMethods
46
+ # PUT /_api/simple/all
47
+ # Returns all documents of a collections. The call expects an JSON object as body with the following attributes:
48
+ # collection: The identifier or name of the collection to query.
49
+ # skip: The documents to skip in the query. (optional)
50
+ # limit: The maximal amount of documents to return. The skip is applied before the limit restriction. (optional)
51
+ #
52
+ # PUT /_api/simple/by-example
53
+ # This will find all documents matching a given example.
54
+ # The call expects a JSON hash array as body with the following attributes:
55
+ # collection: The identifier or name of the collection to query.
56
+ # example: The example.
57
+ # skip: The documents to skip in the query. (optional)
58
+ # limit: The maximal amount of documents to return. (optional)
59
+ def all(options = {})
60
+ query_parameters = {'collection' => collection}; endpoint = '/_api/simple/all'
61
+ endpoint = '/_api/simple/by-example' if options and options['example'] and options['example'].any?
62
+ res = transport.put(endpoint, :body => query_parameters.merge(options).to_json)
63
+ if res.code == 201 and res.parsed_response and res.parsed_response["result"]
64
+ query_result = QueryResult.new
65
+ res["result"].each {|json_doc| query_result << self.new.build(json_doc)}
66
+ query_result
67
+ end
68
+ end
69
+
70
+ # PUT /_api/simple/first-example
71
+ # This will return the first document matching a given example.
72
+ # The call expects a JSON hash array as body with the following attributes:
73
+ # collection: The identifier or name of the collection to query.
74
+ # example: The example.
75
+ def first(options = {})
76
+ query_parameters = {'collection' => collection}; endpoint = '/_api/simple/first-example'
77
+ res = transport.put(endpoint, :body => query_parameters.merge(options).to_json)
78
+ if res.code == 200 and res.parsed_response and (json_doc = res.parsed_response["document"])
79
+ self.new.build(json_doc)
80
+ end
81
+ end
82
+
83
+ def where(hash)
84
+ Query.new(self).where(hash)
85
+ end
86
+
87
+ def limit(number)
88
+ Query.new(self).limit(number)
89
+ end
90
+
91
+ def skip(number)
92
+ Query.new(self).skip(number)
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,14 @@
1
+ # Example with predefined attributes
2
+ class AnotherExampleDocument < ArangoDb::Base
3
+ collection :more_examples
4
+ db_attrs :foo, :bar
5
+
6
+ before_save :change_something
7
+ after_save :change_something_else
8
+
9
+ def change_something
10
+ self.foo = 'bar2'
11
+ end
12
+
13
+ def change_something_else; end
14
+ end
@@ -0,0 +1,27 @@
1
+ class ExampleDocument < ArangoDb::Base
2
+ collection :examples
3
+
4
+ before_create :add_something
5
+ after_create :do_something_else
6
+
7
+ before_destroy :method1
8
+ after_destroy :method2
9
+
10
+ def validate
11
+ not self.foo.nil?
12
+ end
13
+
14
+ def add_something
15
+ self.something = "other"
16
+ end
17
+
18
+ def do_something_else; end
19
+
20
+ def method1
21
+ # puts self._id
22
+ end
23
+
24
+ def method2
25
+ # puts self._id
26
+ end
27
+ end
@@ -13,6 +13,8 @@ require 'shoulda'
13
13
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
14
  $LOAD_PATH.unshift(File.dirname(__FILE__))
15
15
  require 'arangodb-odm'
16
+ require 'example_document.rb'
17
+ require 'another_example_document.rb'
16
18
 
17
19
  # Set your ArangoDB host...
18
20
  ArangoDb::Transport.base_uri 'http://localhost:8529'
@@ -1,49 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ExampleDocument < ArangoDb::Base
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
29
- end
30
-
31
- # Example with predefined attributes
32
- class AnotherExampleDocument < ArangoDb::Base
33
- collection :more_examples
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
44
- end
45
-
46
- class TestArangoDbRb < Test::Unit::TestCase
3
+ class TestArangoDb < Test::Unit::TestCase
47
4
  should "class should have a collection" do
48
5
  assert_equal ExampleDocument.collection, 'examples'
49
6
  assert_equal AnotherExampleDocument.collection, 'more_examples'
@@ -0,0 +1,32 @@
1
+ require 'helper'
2
+
3
+ class TestArangoDbQueries < Test::Unit::TestCase
4
+ should "get all documents" do
5
+ example_documents = ExampleDocument.skip(1).limit(2).all
6
+ assert_not_nil example_documents
7
+ assert example_documents.is_a?(ArangoDb::Queries::QueryResult)
8
+ assert example_documents.size > 0
9
+ assert_equal example_documents.size, 2
10
+ end
11
+
12
+ should "get all documents by example" do
13
+ example_documents = ExampleDocument.where(:foo => 'bar').where("test" => 1).where("list" => [1, 2, 3]).limit(10).all
14
+ assert_not_nil example_documents
15
+ assert example_documents.size > 0
16
+
17
+ example_documents = ExampleDocument.where(:something => 'not existant').all
18
+ assert_not_nil example_documents
19
+ assert_equal example_documents.size, 0
20
+ end
21
+
22
+ should "get first document by example" do
23
+ example_document = ExampleDocument.where('foo' => 'bar').first
24
+ assert_not_nil example_document
25
+ assert_not_nil example_document._id
26
+ assert_not_nil example_document._rev
27
+ assert_equal example_document.foo, 'bar'
28
+
29
+ example_document = ExampleDocument.where(:something => 'not existant').first
30
+ assert_nil example_document
31
+ end
32
+ end
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: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Oliver Kiessler
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-10-03 00:00:00 Z
18
+ date: 2012-12-03 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  requirement: &id001 !ruby/object:Gem::Requirement
@@ -65,12 +65,12 @@ dependencies:
65
65
  requirements:
66
66
  - - ~>
67
67
  - !ruby/object:Gem::Version
68
- hash: 23
68
+ hash: 19
69
69
  segments:
70
70
  - 1
71
+ - 1
71
72
  - 0
72
- - 0
73
- version: 1.0.0
73
+ version: 1.1.0
74
74
  version_requirements: *id004
75
75
  name: bundler
76
76
  prerelease: false
@@ -107,6 +107,22 @@ dependencies:
107
107
  type: :development
108
108
  - !ruby/object:Gem::Dependency
109
109
  requirement: &id007 !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ~>
113
+ - !ruby/object:Gem::Version
114
+ hash: 27
115
+ segments:
116
+ - 2
117
+ - 4
118
+ - 2
119
+ version: 2.4.2
120
+ version_requirements: *id007
121
+ name: rdoc
122
+ prerelease: false
123
+ type: :development
124
+ - !ruby/object:Gem::Dependency
125
+ requirement: &id008 !ruby/object:Gem::Requirement
110
126
  none: false
111
127
  requirements:
112
128
  - - ">="
@@ -115,12 +131,12 @@ dependencies:
115
131
  segments:
116
132
  - 0
117
133
  version: "0"
118
- version_requirements: *id007
134
+ version_requirements: *id008
119
135
  name: httparty
120
136
  prerelease: false
121
137
  type: :runtime
122
138
  - !ruby/object:Gem::Dependency
123
- requirement: &id008 !ruby/object:Gem::Requirement
139
+ requirement: &id009 !ruby/object:Gem::Requirement
124
140
  none: false
125
141
  requirements:
126
142
  - - ">="
@@ -129,7 +145,7 @@ dependencies:
129
145
  segments:
130
146
  - 0
131
147
  version: "0"
132
- version_requirements: *id008
148
+ version_requirements: *id009
133
149
  name: json
134
150
  prerelease: false
135
151
  type: :runtime
@@ -151,8 +167,12 @@ files:
151
167
  - Rakefile
152
168
  - VERSION
153
169
  - lib/arangodb-odm.rb
170
+ - lib/queries.rb
171
+ - test/another_example_document.rb
172
+ - test/example_document.rb
154
173
  - test/helper.rb
155
174
  - test/test_arangodb-odm.rb
175
+ - test/test_queries.rb
156
176
  homepage: http://github.com/okiess/arangodb-odm
157
177
  licenses:
158
178
  - MIT