class Ashikawa::Core::Cursor
Attributes
The ID of the cursor @return [String] @api public @example Get the id of the cursor
cursor = Ashikawa::Core::Cursor.new(database, raw_cursor) cursor.id #=> '1337'
The number of documents @return [Int] @api public @example Get the number of documents
cursor = Ashikawa::Core::Cursor.new(database, raw_cursor) cursor.length #=> 23
Public Class Methods
Initialize a Cursor
with the database and raw data
@param [Database] database @param [Hash] raw_cursor @api public @example Create a new Cursor
from the raw representation
cursor = Ashikawa::Core::Cursor.new(database, raw_cursor)
# File lib/ashikawa-core/cursor.rb, line 37 def initialize(database, raw_cursor) @database = database parse_raw_cursor(raw_cursor) end
Public Instance Methods
Delete the cursor @return [Hash] parsed JSON response from the server @api public @example Delete the cursor
cursor = Ashikawa::Core::Cursor.new(database, raw_cursor) cursor.delete
# File lib/ashikawa-core/cursor.rb, line 73 def delete @database.send_request("cursor/#{@id}", delete: {}) end
Iterate over the result
@yield [Object] A Document
, An Edge
or a Raw Object @return [nil, Enumerator] If no block is given, an Enumerator is returned @api public @example Print all documents
cursor = Ashikawa::Core::Cursor.new(database, raw_cursor) cursor.each do |document| p document end
@example Get an enumerator to iterate over all documents
cursor = Ashikawa::Core::Cursor.new(database, raw_cursor) enumerator = cursor.each enumerator.next #=> #<Document ...>
# File lib/ashikawa-core/cursor.rb, line 56 def each return to_enum(__callee__) unless block_given? begin @current.each do |raw_document| yield parse_raw_document(raw_document) end end while next_batch nil end
Private Instance Methods
Detect if a raw document is a document or edge and return the class
@param [Hash] raw_document @return class @api private
# File lib/ashikawa-core/cursor.rb, line 97 def detect_document_class_for(raw_document) if raw_document.key?('_from') && raw_document.key?('_to') Edge else Document end end
Get a new batch from the server
@return [Boolean] Is there a next batch? @api private
# File lib/ashikawa-core/cursor.rb, line 131 def next_batch return false unless @has_more raw_cursor = @database.send_request("cursor/#{@id}", put: {}) parse_raw_cursor(raw_cursor) end
Parse the cursor for multiple documents
@param [Hash] raw_cursor @return self @api private
# File lib/ashikawa-core/cursor.rb, line 122 def parse_documents_cursor(raw_cursor) @current = raw_cursor['result'] @length = raw_cursor['count'].to_i if raw_cursor.key?('count') end
Pull the raw data from the cursor into this object
@param [Hash] raw_cursor @return self @api private
# File lib/ashikawa-core/cursor.rb, line 110 def parse_raw_cursor(raw_cursor) @id = raw_cursor['id'] @has_more = raw_cursor['hasMore'] parse_documents_cursor(raw_cursor) self end
Parse a raw document and return a Document
, an Edge
or a raw object
@param [Hash] raw_document @return Document
| Edge
| Object @api private
# File lib/ashikawa-core/cursor.rb, line 84 def parse_raw_document(raw_document) if raw_document.class == Hash detect_document_class_for(raw_document).new(@database, raw_document) else raw_document end end