ashikawa-core 0.5.1 → 0.6.0
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/.gitignore +11 -9
- data/.rspec +4 -0
- data/.travis.yml +8 -3
- data/CONTRIBUTING.md +5 -5
- data/Gemfile +5 -1
- data/Gemfile.devtools +65 -0
- data/Guardfile +1 -11
- data/README.md +14 -8
- data/Rakefile +5 -103
- data/ashikawa-core.gemspec +3 -29
- data/config/flay.yml +3 -0
- data/config/flog.yml +2 -0
- data/config/mutant.yml +3 -0
- data/config/roodi.yml +18 -0
- data/config/site.reek +95 -0
- data/config/yardstick.yml +2 -0
- data/lib/ashikawa-core/collection.rb +138 -178
- data/lib/ashikawa-core/connection.rb +74 -26
- data/lib/ashikawa-core/cursor.rb +30 -9
- data/lib/ashikawa-core/database.rb +23 -19
- data/lib/ashikawa-core/document.rb +33 -8
- data/lib/ashikawa-core/exceptions/collection_not_found.rb +15 -0
- data/lib/ashikawa-core/exceptions/document_not_found.rb +4 -0
- data/lib/ashikawa-core/exceptions/index_not_found.rb +15 -0
- data/lib/ashikawa-core/exceptions/no_collection_provided.rb +4 -0
- data/lib/ashikawa-core/exceptions/unknown_path.rb +15 -0
- data/lib/ashikawa-core/figure.rb +73 -0
- data/lib/ashikawa-core/index.rb +25 -7
- data/lib/ashikawa-core/query.rb +68 -55
- data/lib/ashikawa-core/status.rb +77 -0
- data/lib/ashikawa-core/version.rb +1 -1
- data/spec/acceptance/basic_spec.rb +14 -18
- data/spec/acceptance/index_spec.rb +4 -2
- data/spec/acceptance/query_spec.rb +18 -19
- data/spec/acceptance_auth/auth_spec.rb +2 -2
- data/spec/setup/arangodb.sh +34 -39
- data/spec/spec_helper.rb +27 -0
- data/spec/unit/collection_spec.rb +25 -73
- data/spec/unit/connection_spec.rb +46 -15
- data/spec/unit/cursor_spec.rb +3 -3
- data/spec/unit/database_spec.rb +8 -7
- data/spec/unit/document_spec.rb +2 -2
- data/spec/unit/exception_spec.rb +21 -0
- data/spec/unit/figure_spec.rb +28 -0
- data/spec/unit/index_spec.rb +1 -1
- data/spec/unit/query_spec.rb +25 -25
- data/spec/unit/spec_helper.rb +6 -4
- data/spec/unit/status_spec.rb +51 -0
- data/tasks/adjustments.rake +46 -0
- metadata +31 -203
@@ -2,12 +2,13 @@ require "ashikawa-core/document"
|
|
2
2
|
require "ashikawa-core/index"
|
3
3
|
require "ashikawa-core/cursor"
|
4
4
|
require "ashikawa-core/query"
|
5
|
-
require "
|
5
|
+
require "ashikawa-core/status"
|
6
|
+
require "ashikawa-core/figure"
|
6
7
|
require "forwardable"
|
7
8
|
|
8
9
|
module Ashikawa
|
9
10
|
module Core
|
10
|
-
#
|
11
|
+
# A certain Collection within the Database
|
11
12
|
class Collection
|
12
13
|
extend Forwardable
|
13
14
|
|
@@ -16,7 +17,7 @@ module Ashikawa
|
|
16
17
|
# @return [String]
|
17
18
|
# @api public
|
18
19
|
# @example Change the name of a collection
|
19
|
-
# database = Ashikawa::Core::Database.new
|
20
|
+
# database = Ashikawa::Core::Database.new("http://localhost:8529")
|
20
21
|
# raw_collection = {
|
21
22
|
# "name" => "example_1",
|
22
23
|
# "waitForSync" => true,
|
@@ -25,7 +26,7 @@ module Ashikawa
|
|
25
26
|
# "error" => false,
|
26
27
|
# "code" => 200
|
27
28
|
# }
|
28
|
-
# collection = Ashikawa::Core::Collection.new
|
29
|
+
# collection = Ashikawa::Core::Collection.new(database, raw_collection)
|
29
30
|
# collection.name # => "example_1"
|
30
31
|
# collection.name = "example_2"
|
31
32
|
# collection.name # => "example_2"
|
@@ -36,7 +37,7 @@ module Ashikawa
|
|
36
37
|
# @return [Fixnum]
|
37
38
|
# @api public
|
38
39
|
# @example Get the id of the collection
|
39
|
-
# database = Ashikawa::Core::Database.new
|
40
|
+
# database = Ashikawa::Core::Database.new("http://localhost:8529")
|
40
41
|
# raw_collection = {
|
41
42
|
# "name" => "example_1",
|
42
43
|
# "waitForSync" => true,
|
@@ -45,72 +46,16 @@ module Ashikawa
|
|
45
46
|
# "error" => false,
|
46
47
|
# "code" => 200
|
47
48
|
# }
|
48
|
-
# collection = Ashikawa::Core::Collection.new
|
49
|
+
# collection = Ashikawa::Core::Collection.new(database, raw_collection)
|
49
50
|
# collection.id #=> 4588
|
50
51
|
attr_reader :id
|
51
52
|
|
52
|
-
#
|
53
|
-
#
|
54
|
-
# @return [Database]
|
55
|
-
# @api public
|
56
|
-
attr_reader :database
|
57
|
-
|
58
|
-
# Sending requests is delegated to the database
|
59
|
-
delegate send_request: :@database
|
60
|
-
|
61
|
-
# Create a new Collection object with a name and an optional ID
|
62
|
-
#
|
63
|
-
# @param [Database] database The database the connection belongs to
|
64
|
-
# @param [Hash] raw_collection The raw collection returned from the server
|
65
|
-
# @api public
|
66
|
-
# @example Create a Collection object from scratch
|
67
|
-
# database = Ashikawa::Core::Database.new "http://localhost:8529"
|
68
|
-
# raw_collection = {
|
69
|
-
# "name" => "example_1",
|
70
|
-
# "waitForSync" => true,
|
71
|
-
# "id" => 4588,
|
72
|
-
# "status" => 3,
|
73
|
-
# "error" => false,
|
74
|
-
# "code" => 200
|
75
|
-
# }
|
76
|
-
# collection = Ashikawa::Core::Collection.new database, raw_collection
|
77
|
-
def initialize(database, raw_collection)
|
78
|
-
@database = database
|
79
|
-
@name = raw_collection['name'] if raw_collection.has_key? 'name'
|
80
|
-
@id = raw_collection['id'].to_i if raw_collection.has_key? 'id'
|
81
|
-
@status = raw_collection['status'].to_i if raw_collection.has_key? 'status'
|
82
|
-
end
|
83
|
-
|
84
|
-
# Change the name of the collection
|
85
|
-
#
|
86
|
-
# @param [String] new_name New Name
|
87
|
-
# @return [String] New Name
|
88
|
-
# @api public
|
89
|
-
# @example Change the name of a collection
|
90
|
-
# database = Ashikawa::Core::Database.new "http://localhost:8529"
|
91
|
-
# raw_collection = {
|
92
|
-
# "name" => "example_1",
|
93
|
-
# "waitForSync" => true,
|
94
|
-
# "id" => 4588,
|
95
|
-
# "status" => 3,
|
96
|
-
# "error" => false,
|
97
|
-
# "code" => 200
|
98
|
-
# }
|
99
|
-
# collection = Ashikawa::Core::Collection.new database, raw_collection
|
100
|
-
# collection.name # => "example_1"
|
101
|
-
# collection.name = "example_2"
|
102
|
-
# collection.name # => "example_2"
|
103
|
-
def name=(new_name)
|
104
|
-
send_request_for_this_collection "/rename", put: { "name" => new_name }
|
105
|
-
@name = new_name
|
106
|
-
end
|
107
|
-
|
108
|
-
# Checks if the collection is new born
|
53
|
+
# A wrapper around the status of the collection
|
109
54
|
#
|
110
|
-
# @return [
|
55
|
+
# @return [Status]
|
111
56
|
# @api public
|
112
|
-
# @example
|
113
|
-
# database = Ashikawa::Core::Database.new
|
57
|
+
# @example
|
58
|
+
# database = Ashikawa::Core::Database.new("http://localhost:8529")
|
114
59
|
# raw_collection = {
|
115
60
|
# "name" => "example_1",
|
116
61
|
# "waitForSync" => true,
|
@@ -119,18 +64,17 @@ module Ashikawa
|
|
119
64
|
# "error" => false,
|
120
65
|
# "code" => 200
|
121
66
|
# }
|
122
|
-
# collection = Ashikawa::Core::Collection.new
|
123
|
-
# collection.
|
124
|
-
|
125
|
-
|
126
|
-
end
|
67
|
+
# collection = Ashikawa::Core::Collection.new(database, raw_collection)
|
68
|
+
# collection.status.loaded? #=> true
|
69
|
+
# collection.status.new_born? #=> false
|
70
|
+
attr_reader :status
|
127
71
|
|
128
|
-
#
|
72
|
+
# The database the collection belongs to
|
129
73
|
#
|
130
|
-
# @return [
|
74
|
+
# @return [Database]
|
131
75
|
# @api public
|
132
|
-
# @example
|
133
|
-
# database = Ashikawa::Core::Database.new
|
76
|
+
# @example
|
77
|
+
# database = Ashikawa::Core::Database.new("http://localhost:8529")
|
134
78
|
# raw_collection = {
|
135
79
|
# "name" => "example_1",
|
136
80
|
# "waitForSync" => true,
|
@@ -139,38 +83,20 @@ module Ashikawa
|
|
139
83
|
# "error" => false,
|
140
84
|
# "code" => 200
|
141
85
|
# }
|
142
|
-
# collection = Ashikawa::Core::Collection.new
|
143
|
-
# collection.
|
144
|
-
|
145
|
-
@status == 2
|
146
|
-
end
|
86
|
+
# collection = Ashikawa::Core::Collection.new(database, raw_collection)
|
87
|
+
# collection.database #=> #<Database: ...>
|
88
|
+
attr_reader :database
|
147
89
|
|
148
|
-
#
|
149
|
-
|
150
|
-
# @return [Boolean]
|
151
|
-
# @api public
|
152
|
-
# @example Is the collection loaded?
|
153
|
-
# database = Ashikawa::Core::Database.new "http://localhost:8529"
|
154
|
-
# raw_collection = {
|
155
|
-
# "name" => "example_1",
|
156
|
-
# "waitForSync" => true,
|
157
|
-
# "id" => 4588,
|
158
|
-
# "status" => 3,
|
159
|
-
# "error" => false,
|
160
|
-
# "code" => 200
|
161
|
-
# }
|
162
|
-
# collection = Ashikawa::Core::Collection.new database, raw_collection
|
163
|
-
# collection.loaded? #=> true
|
164
|
-
def loaded?
|
165
|
-
@status == 3
|
166
|
-
end
|
90
|
+
# Sending requests is delegated to the database
|
91
|
+
def_delegator :@database, :send_request
|
167
92
|
|
168
|
-
#
|
93
|
+
# Create a new Collection object with a name and an optional ID
|
169
94
|
#
|
170
|
-
# @
|
95
|
+
# @param [Database] database The database the connection belongs to
|
96
|
+
# @param [Hash] raw_collection The raw collection returned from the server
|
171
97
|
# @api public
|
172
|
-
# @example
|
173
|
-
# database = Ashikawa::Core::Database.new
|
98
|
+
# @example Create a Collection object from scratch
|
99
|
+
# database = Ashikawa::Core::Database.new("http://localhost:8529")
|
174
100
|
# raw_collection = {
|
175
101
|
# "name" => "example_1",
|
176
102
|
# "waitForSync" => true,
|
@@ -179,18 +105,21 @@ module Ashikawa
|
|
179
105
|
# "error" => false,
|
180
106
|
# "code" => 200
|
181
107
|
# }
|
182
|
-
# collection = Ashikawa::Core::Collection.new
|
183
|
-
|
184
|
-
|
185
|
-
@
|
108
|
+
# collection = Ashikawa::Core::Collection.new(database, raw_collection)
|
109
|
+
def initialize(database, raw_collection)
|
110
|
+
@database = database
|
111
|
+
@name = raw_collection['name'] if raw_collection.has_key?('name')
|
112
|
+
@id = raw_collection['id'].to_i if raw_collection.has_key?('id')
|
113
|
+
@status = Status.new raw_collection['status'].to_i if raw_collection.has_key?('status')
|
186
114
|
end
|
187
115
|
|
188
|
-
#
|
116
|
+
# Change the name of the collection
|
189
117
|
#
|
190
|
-
# @
|
118
|
+
# @param [String] new_name New Name
|
119
|
+
# @return [String] New Name
|
191
120
|
# @api public
|
192
|
-
# @example
|
193
|
-
# database = Ashikawa::Core::Database.new
|
121
|
+
# @example Change the name of a collection
|
122
|
+
# database = Ashikawa::Core::Database.new("http://localhost:8529")
|
194
123
|
# raw_collection = {
|
195
124
|
# "name" => "example_1",
|
196
125
|
# "waitForSync" => true,
|
@@ -199,10 +128,13 @@ module Ashikawa
|
|
199
128
|
# "error" => false,
|
200
129
|
# "code" => 200
|
201
130
|
# }
|
202
|
-
# collection = Ashikawa::Core::Collection.new
|
203
|
-
# collection.
|
204
|
-
|
205
|
-
|
131
|
+
# collection = Ashikawa::Core::Collection.new(database, raw_collection)
|
132
|
+
# collection.name # => "example_1"
|
133
|
+
# collection.name = "example_2"
|
134
|
+
# collection.name # => "example_2"
|
135
|
+
def name=(new_name)
|
136
|
+
send_information_to_server(:rename, :name, new_name)
|
137
|
+
@name = new_name
|
206
138
|
end
|
207
139
|
|
208
140
|
# Does the document wait until the data has been synchronised to disk?
|
@@ -210,7 +142,7 @@ module Ashikawa
|
|
210
142
|
# @return [Boolean]
|
211
143
|
# @api public
|
212
144
|
# @example Does the collection wait for file synchronization?
|
213
|
-
# database = Ashikawa::Core::Database.new
|
145
|
+
# database = Ashikawa::Core::Database.new("http://localhost:8529")
|
214
146
|
# raw_collection = {
|
215
147
|
# "name" => "example_1",
|
216
148
|
# "waitForSync" => true,
|
@@ -219,11 +151,10 @@ module Ashikawa
|
|
219
151
|
# "error" => false,
|
220
152
|
# "code" => 200
|
221
153
|
# }
|
222
|
-
# collection = Ashikawa::Core::Collection.new
|
154
|
+
# collection = Ashikawa::Core::Collection.new(database, raw_collection)
|
223
155
|
# collection.wait_for_sync? #=> false
|
224
156
|
def wait_for_sync?
|
225
|
-
|
226
|
-
server_response["waitForSync"]
|
157
|
+
get_information_from_server(:properties, :waitForSync)
|
227
158
|
end
|
228
159
|
|
229
160
|
# Change if the document will wait until the data has been synchronised to disk
|
@@ -231,7 +162,7 @@ module Ashikawa
|
|
231
162
|
# @return [String] Response from the server
|
232
163
|
# @api public
|
233
164
|
# @example Tell the collection to wait for file synchronization
|
234
|
-
# database = Ashikawa::Core::Database.new
|
165
|
+
# database = Ashikawa::Core::Database.new("http://localhost:8529")
|
235
166
|
# raw_collection = {
|
236
167
|
# "name" => "example_1",
|
237
168
|
# "waitForSync" => true,
|
@@ -240,10 +171,10 @@ module Ashikawa
|
|
240
171
|
# "error" => false,
|
241
172
|
# "code" => 200
|
242
173
|
# }
|
243
|
-
# collection = Ashikawa::Core::Collection.new
|
174
|
+
# collection = Ashikawa::Core::Collection.new(database, raw_collection)
|
244
175
|
# collection.wait_for_sync = true
|
245
176
|
def wait_for_sync=(new_value)
|
246
|
-
|
177
|
+
send_information_to_server(:properties, :waitForSync, new_value)
|
247
178
|
end
|
248
179
|
|
249
180
|
# Returns the number of documents in the collection
|
@@ -251,7 +182,7 @@ module Ashikawa
|
|
251
182
|
# @return [Fixnum] Number of documents
|
252
183
|
# @api public
|
253
184
|
# @example How many documents are in the collection?
|
254
|
-
# database = Ashikawa::Core::Database.new
|
185
|
+
# database = Ashikawa::Core::Database.new("http://localhost:8529")
|
255
186
|
# raw_collection = {
|
256
187
|
# "name" => "example_1",
|
257
188
|
# "waitForSync" => true,
|
@@ -260,25 +191,18 @@ module Ashikawa
|
|
260
191
|
# "error" => false,
|
261
192
|
# "code" => 200
|
262
193
|
# }
|
263
|
-
# collection = Ashikawa::Core::Collection.new
|
194
|
+
# collection = Ashikawa::Core::Collection.new(database, raw_collection)
|
264
195
|
# collection.length # => 0
|
265
196
|
def length
|
266
|
-
|
267
|
-
server_response["count"]
|
197
|
+
get_information_from_server(:count, :count)
|
268
198
|
end
|
269
199
|
|
270
|
-
# Return a
|
200
|
+
# Return a Figure initialized with current data for the collection
|
271
201
|
#
|
272
|
-
# @
|
273
|
-
# * :datafiles_count - the number of active datafiles
|
274
|
-
# * :alive_size - the total size in bytes used by all living documents
|
275
|
-
# * :alive_count - the number of living documents
|
276
|
-
# * :dead_size - the total size in bytes used by all dead documents
|
277
|
-
# * :dead_count - the number of dead documents
|
278
|
-
# @return [Fixnum] The figure you requested
|
202
|
+
# @return [Figure]
|
279
203
|
# @api public
|
280
204
|
# @example Get the datafile count for a collection
|
281
|
-
# database = Ashikawa::Core::Database.new
|
205
|
+
# database = Ashikawa::Core::Database.new("http://localhost:8529")
|
282
206
|
# raw_collection = {
|
283
207
|
# "name" => "example_1",
|
284
208
|
# "waitForSync" => true,
|
@@ -287,12 +211,11 @@ module Ashikawa
|
|
287
211
|
# "error" => false,
|
288
212
|
# "code" => 200
|
289
213
|
# }
|
290
|
-
# collection = Ashikawa::Core::Collection.new
|
291
|
-
# collection.figure
|
292
|
-
def figure
|
293
|
-
|
294
|
-
|
295
|
-
server_response["figures"][figure_area][figure_name]
|
214
|
+
# collection = Ashikawa::Core::Collection.new(database, raw_collection)
|
215
|
+
# collection.figure.datafiles_count #=> 0
|
216
|
+
def figure
|
217
|
+
raw_figure = get_information_from_server(:figures, :figures)
|
218
|
+
Figure.new(raw_figure)
|
296
219
|
end
|
297
220
|
|
298
221
|
# Deletes the collection
|
@@ -300,7 +223,7 @@ module Ashikawa
|
|
300
223
|
# @return [String] Response from the server
|
301
224
|
# @api public
|
302
225
|
# @example Delete a collection
|
303
|
-
# database = Ashikawa::Core::Database.new
|
226
|
+
# database = Ashikawa::Core::Database.new("http://localhost:8529")
|
304
227
|
# raw_collection = {
|
305
228
|
# "name" => "example_1",
|
306
229
|
# "waitForSync" => true,
|
@@ -309,10 +232,10 @@ module Ashikawa
|
|
309
232
|
# "error" => false,
|
310
233
|
# "code" => 200
|
311
234
|
# }
|
312
|
-
# collection = Ashikawa::Core::Collection.new
|
235
|
+
# collection = Ashikawa::Core::Collection.new(database, raw_collection)
|
313
236
|
# collection.delete
|
314
237
|
def delete
|
315
|
-
send_request_for_this_collection
|
238
|
+
send_request_for_this_collection("", :delete => {})
|
316
239
|
end
|
317
240
|
|
318
241
|
# Load the collection into memory
|
@@ -320,7 +243,7 @@ module Ashikawa
|
|
320
243
|
# @return [String] Response from the server
|
321
244
|
# @api public
|
322
245
|
# @example Load a collection into memory
|
323
|
-
# database = Ashikawa::Core::Database.new
|
246
|
+
# database = Ashikawa::Core::Database.new("http://localhost:8529")
|
324
247
|
# raw_collection = {
|
325
248
|
# "name" => "example_1",
|
326
249
|
# "waitForSync" => true,
|
@@ -329,10 +252,10 @@ module Ashikawa
|
|
329
252
|
# "error" => false,
|
330
253
|
# "code" => 200
|
331
254
|
# }
|
332
|
-
# collection = Ashikawa::Core::Collection.new
|
255
|
+
# collection = Ashikawa::Core::Collection.new(database, raw_collection)
|
333
256
|
# collection.load
|
334
257
|
def load
|
335
|
-
|
258
|
+
send_command_to_server(:load)
|
336
259
|
end
|
337
260
|
|
338
261
|
# Load the collection into memory
|
@@ -340,7 +263,7 @@ module Ashikawa
|
|
340
263
|
# @return [String] Response from the server
|
341
264
|
# @api public
|
342
265
|
# @example Unload a collection into memory
|
343
|
-
# database = Ashikawa::Core::Database.new
|
266
|
+
# database = Ashikawa::Core::Database.new("http://localhost:8529")
|
344
267
|
# raw_collection = {
|
345
268
|
# "name" => "example_1",
|
346
269
|
# "waitForSync" => true,
|
@@ -349,10 +272,10 @@ module Ashikawa
|
|
349
272
|
# "error" => false,
|
350
273
|
# "code" => 200
|
351
274
|
# }
|
352
|
-
# collection = Ashikawa::Core::Collection.new
|
275
|
+
# collection = Ashikawa::Core::Collection.new(database, raw_collection)
|
353
276
|
# collection.unload
|
354
277
|
def unload
|
355
|
-
|
278
|
+
send_command_to_server(:unload)
|
356
279
|
end
|
357
280
|
|
358
281
|
# Delete all documents from the collection
|
@@ -360,7 +283,7 @@ module Ashikawa
|
|
360
283
|
# @return [String] Response from the server
|
361
284
|
# @api public
|
362
285
|
# @example Remove all documents from a collection
|
363
|
-
# database = Ashikawa::Core::Database.new
|
286
|
+
# database = Ashikawa::Core::Database.new("http://localhost:8529")
|
364
287
|
# raw_collection = {
|
365
288
|
# "name" => "example_1",
|
366
289
|
# "waitForSync" => true,
|
@@ -369,10 +292,10 @@ module Ashikawa
|
|
369
292
|
# "error" => false,
|
370
293
|
# "code" => 200
|
371
294
|
# }
|
372
|
-
# collection = Ashikawa::Core::Collection.new
|
295
|
+
# collection = Ashikawa::Core::Collection.new(database, raw_collection)
|
373
296
|
# collection.truncate!
|
374
297
|
def truncate!
|
375
|
-
|
298
|
+
send_command_to_server(:truncate)
|
376
299
|
end
|
377
300
|
|
378
301
|
# Fetch a certain document by its ID
|
@@ -381,37 +304,35 @@ module Ashikawa
|
|
381
304
|
# @raise [DocumentNotFoundException] If the requested document was not found
|
382
305
|
# @return Document
|
383
306
|
# @api public
|
384
|
-
# @example Fetch
|
307
|
+
# @example Fetch the document with the ID 12345
|
385
308
|
# document = collection[12345]
|
386
309
|
def [](document_id)
|
387
|
-
|
388
|
-
|
389
|
-
rescue RestClient::ResourceNotFound
|
390
|
-
raise DocumentNotFoundException
|
391
|
-
end
|
392
|
-
|
393
|
-
Document.new @database, server_response
|
310
|
+
server_response = send_request("/document/#{@id}/#{document_id}")
|
311
|
+
Document.new(@database, server_response)
|
394
312
|
end
|
395
313
|
|
396
314
|
# Replace a document by its ID
|
397
315
|
#
|
398
316
|
# @param [Integer] document_id the id of the document
|
399
317
|
# @param [Hash] raw_document the data you want to replace it with
|
318
|
+
# @return [Hash] parsed JSON response from the server
|
400
319
|
# @api public
|
320
|
+
# @example Replace the document with the ID 12345
|
321
|
+
# collection[12345] = document
|
401
322
|
def []=(document_id, raw_document)
|
402
|
-
send_request
|
323
|
+
send_request("/document/#{@id}/#{document_id}", :put => raw_document)
|
403
324
|
end
|
404
325
|
|
405
326
|
# Create a new document from raw data
|
406
327
|
#
|
407
328
|
# @param [Hash] raw_document
|
408
|
-
# @return
|
329
|
+
# @return [Document] The created document
|
409
330
|
# @api public
|
331
|
+
# @example Create a new document from raw data
|
332
|
+
# collection.create(raw_document)
|
410
333
|
def create(raw_document)
|
411
|
-
server_response = send_request
|
412
|
-
|
413
|
-
|
414
|
-
Document.new @database, server_response
|
334
|
+
server_response = send_request("/document?collection=#{@id}", :post => raw_document)
|
335
|
+
Document.new(@database, server_response)
|
415
336
|
end
|
416
337
|
|
417
338
|
alias :<< :create
|
@@ -424,14 +345,14 @@ module Ashikawa
|
|
424
345
|
# @api public
|
425
346
|
# @example Add a hash-index to the fields :name and :profession of a collection
|
426
347
|
# people = database['people']
|
427
|
-
# people.add_index
|
348
|
+
# people.add_index(:hash, :on => [:name, :profession])
|
428
349
|
def add_index(type, opts)
|
429
|
-
|
350
|
+
response = send_request("/index?collection=#{@id}", :post => {
|
430
351
|
"type" => type.to_s,
|
431
352
|
"fields" => opts[:on].map { |field| field.to_s }
|
432
|
-
}
|
353
|
+
})
|
433
354
|
|
434
|
-
Index.new
|
355
|
+
Index.new(self, response)
|
435
356
|
end
|
436
357
|
|
437
358
|
# Get an index by ID
|
@@ -439,21 +360,26 @@ module Ashikawa
|
|
439
360
|
# @param [Integer] id
|
440
361
|
# @return Index
|
441
362
|
# @api public
|
363
|
+
# @example Get an Index by its ID
|
364
|
+
# people = database['people']
|
365
|
+
# people.index(1244) #=> #<Index: id=1244...>
|
442
366
|
def index(id)
|
443
|
-
server_response = send_request
|
444
|
-
|
445
|
-
Index.new self, server_response
|
367
|
+
server_response = send_request("/index/#{@id}/#{id}")
|
368
|
+
Index.new(self, server_response)
|
446
369
|
end
|
447
370
|
|
448
371
|
# Get all indices
|
449
372
|
#
|
450
373
|
# @return [Array<Index>]
|
451
374
|
# @api public
|
375
|
+
# @example Get all indices
|
376
|
+
# people = database['people']
|
377
|
+
# people.indices #=> [#<Index: id=1244...>, ...]
|
452
378
|
def indices
|
453
|
-
server_response = send_request
|
379
|
+
server_response = send_request("/index?collection=#{@id}")
|
454
380
|
|
455
381
|
server_response["indexes"].map do |raw_index|
|
456
|
-
Index.new
|
382
|
+
Index.new(self, raw_index)
|
457
383
|
end
|
458
384
|
end
|
459
385
|
|
@@ -461,18 +387,52 @@ module Ashikawa
|
|
461
387
|
#
|
462
388
|
# @return [Query]
|
463
389
|
# @api public
|
390
|
+
# @example Get all documents in this collection
|
391
|
+
# people = database['people']
|
392
|
+
# people.query.all #=> #<Cursor: id=1244...>
|
464
393
|
def query
|
465
|
-
Query.new
|
394
|
+
Query.new(self)
|
466
395
|
end
|
467
396
|
|
468
397
|
private
|
469
398
|
|
399
|
+
# Send a put request with a given key and value to the server
|
400
|
+
#
|
401
|
+
# @param [Symbol] path
|
402
|
+
# @param [Symbol] key
|
403
|
+
# @param [Symbol] value
|
404
|
+
# @return [Object] The result
|
405
|
+
# @api private
|
406
|
+
def send_information_to_server(path, key, value)
|
407
|
+
send_request_for_this_collection("/#{path}", :put => { key.to_s => value })
|
408
|
+
end
|
409
|
+
|
410
|
+
# Send a put request with the given command
|
411
|
+
#
|
412
|
+
# @param [Symbol] command The command you want to execute
|
413
|
+
# @return [Object] The result
|
414
|
+
# @api private
|
415
|
+
def send_command_to_server(command)
|
416
|
+
send_request_for_this_collection("/#{command}", :put => {})
|
417
|
+
end
|
418
|
+
|
419
|
+
# Send a get request to the server and return a certain attribute
|
420
|
+
#
|
421
|
+
# @param [Symbol] path The path without trailing slash
|
422
|
+
# @param [Symbol] attribute The attribute of the answer that should be returned
|
423
|
+
# @return [Object] The result
|
424
|
+
# @api private
|
425
|
+
def get_information_from_server(path, attribute)
|
426
|
+
server_response = send_request_for_this_collection("/#{path}")
|
427
|
+
server_response[attribute.to_s]
|
428
|
+
end
|
429
|
+
|
470
430
|
# Send a request to the server with the name of the collection prepended
|
471
431
|
#
|
472
432
|
# @return [String] Response from the server
|
473
433
|
# @api private
|
474
434
|
def send_request_for_this_collection(path, method={})
|
475
|
-
send_request
|
435
|
+
send_request("/collection/#{id}#{path}", method)
|
476
436
|
end
|
477
437
|
end
|
478
438
|
end
|