Class: Ashikawa::Core::Collection
- Inherits:
-
Object
- Object
- Ashikawa::Core::Collection
- Extended by:
- Forwardable
- Defined in:
- lib/ashikawa-core/collection.rb
Overview
A certain Collection within the Database
Direct Known Subclasses
Constant Summary collapse
- CONTENT_TYPES =
ArangoDB’s collections contain either only documents or only edges
{ 2 => :document, 3 => :edge }
- CONTENT_CLASS =
Map the content types to the classes from Ashikawa
{ document: Document, edge: Edge }
Instance Attribute Summary collapse
-
#content_type ⇒ :document, :edge
readonly
The kind of content in the collection: Documents or Edges.
-
#database ⇒ Database
readonly
The database the collection belongs to.
-
#id ⇒ Fixnum
readonly
The ID of the collection.
-
#name ⇒ String
The name of the collection, must be unique.
-
#status ⇒ Status
readonly
A wrapper around the status of the collection.
Instance Method Summary collapse
-
#[](document_key) ⇒ Object
Fetch a certain document by its key, return nil if the document does not exist.
-
#add_index(type, options) ⇒ Object
Add an index to the collection.
-
#build_content_class(data, additional_attributes = {}) ⇒ Document
private
Builds an instance for the content class.
-
#create_document(attributes) ⇒ Document
Create a new document with given attributes.
-
#create_edge(from, to, attributes) ⇒ Edge
deprecated
Deprecated.
Since we introduced the dedicated Graph module (‘gharial’) all operations regarding edges and vertices should be done through that module. Due to this please use EdgeCollection#add instead.
-
#delete ⇒ String
Deletes the collection.
-
#fetch(document_key) ⇒ Object
Fetch a certain document by its key.
-
#figure ⇒ Figure
Return a Figure initialized with current data for the collection.
-
#index(id) ⇒ Object
Get an index by ID.
-
#indices ⇒ Array<Index>
Get all indices.
-
#initialize(database, raw_collection) ⇒ Collection
constructor
Create a new Collection object with a name and an optional ID.
-
#key_options ⇒ KeyOptions
Get information about the type of keys of this collection.
-
#length ⇒ Fixnum
Returns the number of documents in the collection.
-
#load ⇒ String
Load the collection into memory.
-
#query ⇒ Query
Return a Query initialized with this collection.
-
#replace(document_key, raw_document) ⇒ Hash
Replace a document by its key.
-
#truncate ⇒ String
Delete all documents from the collection.
-
#unload ⇒ String
Load the collection into memory.
-
#volatile? ⇒ Boolean
Check if the collection is volatile.
-
#wait_for_sync=(new_value) ⇒ String
Change if the document will wait until the data has been synchronised to disk.
-
#wait_for_sync? ⇒ Boolean
Does the document wait until the data has been synchronised to disk?.
Constructor Details
#initialize(database, raw_collection) ⇒ Collection
Create a new Collection object with a name and an optional ID
145 146 147 148 149 |
# File 'lib/ashikawa-core/collection.rb', line 145 def initialize(database, raw_collection) @database = database parse_raw_collection(raw_collection) @content_class = CONTENT_CLASS[@content_type] end |
Instance Attribute Details
#content_type ⇒ :document, :edge (readonly)
The kind of content in the collection: Documents or Edges
124 125 126 |
# File 'lib/ashikawa-core/collection.rb', line 124 def content_type @content_type end |
#database ⇒ Database (readonly)
The database the collection belongs to
106 107 108 |
# File 'lib/ashikawa-core/collection.rb', line 106 def database @database end |
#id ⇒ Fixnum (readonly)
The ID of the collection. Is set by the database and unique
69 70 71 |
# File 'lib/ashikawa-core/collection.rb', line 69 def id @id end |
#name ⇒ String
The name of the collection, must be unique
51 52 53 |
# File 'lib/ashikawa-core/collection.rb', line 51 def name @name end |
#status ⇒ Status (readonly)
A wrapper around the status of the collection
88 89 90 |
# File 'lib/ashikawa-core/collection.rb', line 88 def status @status end |
Instance Method Details
#[](document_key) ⇒ Object
Fetch a certain document by its key, return nil if the document does not exist
367 368 369 370 371 |
# File 'lib/ashikawa-core/collection.rb', line 367 def [](document_key) fetch(document_key) rescue DocumentNotFoundException nil end |
#add_index(type, options) ⇒ Object
Add an index to the collection
430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/ashikawa-core/collection.rb', line 430 def add_index(type, ) [:on] = [[:on]].flatten unique = .fetch(:unique, false) response = send_request("index?collection=#{@id}", post: { 'type' => type.to_s, 'fields' => .fetch(:on).map { |field| field.to_s }, 'unique' => unique }) Index.new(self, response) end |
#build_content_class(data, additional_attributes = {}) ⇒ Document
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds an instance for the content class
498 499 500 |
# File 'lib/ashikawa-core/collection.rb', line 498 def build_content_class(data, additional_attributes = {}) @content_class.new(@database, data, additional_attributes) end |
#create_document(attributes) ⇒ Document
Create a new document with given attributes
392 393 394 395 396 |
# File 'lib/ashikawa-core/collection.rb', line 392 def create_document(attributes) raise "Can't create a document in an edge collection" if @content_type == :edge response = send_request_for_content(post: attributes) build_content_class(response, attributes) end |
#create_edge(from, to, attributes) ⇒ Edge
Since we introduced the dedicated Graph module (‘gharial’) all operations regarding edges and vertices should be done through that module. Due to this please use EdgeCollection#add instead.
Create a new edge between two documents with certain attributes
410 411 412 413 414 415 |
# File 'lib/ashikawa-core/collection.rb', line 410 def create_edge(from, to, attributes) warn '[DEPRECATION] `create_edge` is deprecated. Please use `EdgeCollection#add` instead.' raise "Can't create an edge in a document collection" if @content_type == :document response = send_request("edge?collection=#{@id}&from=#{from.id}&to=#{to.id}", post: attributes) Edge.new(@database, response, attributes) end |
#delete ⇒ String
Deletes the collection
283 284 285 |
# File 'lib/ashikawa-core/collection.rb', line 283 def delete send_request_for_this_collection('', delete: {}) end |
#fetch(document_key) ⇒ Object
Fetch a certain document by its key
355 356 357 358 |
# File 'lib/ashikawa-core/collection.rb', line 355 def fetch(document_key) response = send_request_for_content_key(document_key) build_content_class(response) end |
#figure ⇒ Figure
Return a Figure initialized with current data for the collection
262 263 264 265 |
# File 'lib/ashikawa-core/collection.rb', line 262 def figure raw_figure = get_information_from_server(:figures, :figures) Figure.new(raw_figure) end |
#index(id) ⇒ Object
Get an index by ID
450 451 452 453 |
# File 'lib/ashikawa-core/collection.rb', line 450 def index(id) response = send_request("index/#{@name}/#{id}") Index.new(self, response) end |
#indices ⇒ Array<Index>
Get all indices
462 463 464 465 466 467 468 |
# File 'lib/ashikawa-core/collection.rb', line 462 def indices response = send_request("index?collection=#{@id}") response['indexes'].map do |raw_index| Index.new(self, raw_index) end end |
#key_options ⇒ KeyOptions
Get information about the type of keys of this collection
222 223 224 |
# File 'lib/ashikawa-core/collection.rb', line 222 def KeyOptions.new(get_information_from_server(:properties, :keyOptions)) end |
#length ⇒ Fixnum
Returns the number of documents in the collection
242 243 244 |
# File 'lib/ashikawa-core/collection.rb', line 242 def length get_information_from_server(:count, :count) end |
#load ⇒ String
Load the collection into memory
303 304 305 |
# File 'lib/ashikawa-core/collection.rb', line 303 def load send_command_to_server(:load) end |
#query ⇒ Query
Return a Query initialized with this collection
477 478 479 |
# File 'lib/ashikawa-core/collection.rb', line 477 def query Query.new(self) end |
#replace(document_key, raw_document) ⇒ Hash
Replace a document by its key
381 382 383 |
# File 'lib/ashikawa-core/collection.rb', line 381 def replace(document_key, raw_document) send_request_for_content_key(document_key, put: raw_document) end |
#truncate ⇒ String
Delete all documents from the collection
343 344 345 |
# File 'lib/ashikawa-core/collection.rb', line 343 def truncate send_command_to_server(:truncate) end |
#unload ⇒ String
Load the collection into memory
323 324 325 |
# File 'lib/ashikawa-core/collection.rb', line 323 def unload send_command_to_server(:unload) end |
#volatile? ⇒ Boolean
Check if the collection is volatile
488 489 490 |
# File 'lib/ashikawa-core/collection.rb', line 488 def volatile? get_information_from_server(:properties, :isVolatile) end |
#wait_for_sync=(new_value) ⇒ String
Change if the document will wait until the data has been synchronised to disk
211 212 213 |
# File 'lib/ashikawa-core/collection.rb', line 211 def wait_for_sync=(new_value) send_information_to_server(:properties, :waitForSync, new_value) end |
#wait_for_sync? ⇒ Boolean
Does the document wait until the data has been synchronised to disk?
191 192 193 |
# File 'lib/ashikawa-core/collection.rb', line 191 def wait_for_sync? get_information_from_server(:properties, :waitForSync) end |