Class: Ashikawa::Core::Cursor

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ashikawa-core/cursor.rb

Overview

A Cursor on a certain Database. It is an enumerable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, raw_cursor) ⇒ Cursor

Initialize a Cursor with the database and raw data

Examples:

Create a new Cursor from the raw representation

cursor = Ashikawa::Core::Cursor.new(database, raw_cursor)

Parameters:


38
39
40
41
# File 'lib/ashikawa-core/cursor.rb', line 38

def initialize(database, raw_cursor)
  @database = database
  parse_raw_cursor(raw_cursor)
end

Instance Attribute Details

#idString (readonly)

The ID of the cursor

Examples:

Get the id of the cursor

cursor = Ashikawa::Core::Cursor.new(database, raw_cursor)
cursor.id #=> '1337'

Returns:

  • (String)

21
22
23
# File 'lib/ashikawa-core/cursor.rb', line 21

def id
  @id
end

#lengthInt (readonly)

The number of documents

Examples:

Get the number of documents

cursor = Ashikawa::Core::Cursor.new(database, raw_cursor)
cursor.length #=> 23

Returns:

  • (Int)

29
30
31
# File 'lib/ashikawa-core/cursor.rb', line 29

def length
  @length
end

Instance Method Details

#deleteHash

Delete the cursor

Examples:

Delete the cursor

cursor = Ashikawa::Core::Cursor.new(database, raw_cursor)
cursor.delete

Returns:

  • (Hash)

    parsed JSON response from the server


74
75
76
# File 'lib/ashikawa-core/cursor.rb', line 74

def delete
  @database.send_request("cursor/#{@id}", delete: {})
end

#each {|Object| ... } ⇒ nil, Enumerator

Iterate over the result

Examples:

Print all documents

cursor = Ashikawa::Core::Cursor.new(database, raw_cursor)
cursor.each do |document|
  p document
end

Get an enumerator to iterate over all documents

cursor = Ashikawa::Core::Cursor.new(database, raw_cursor)
enumerator = cursor.each
enumerator.next #=> #<Document ...>

Yields:

  • (Object)

    A Document, An Edge or a Raw Object

Returns:

  • (nil, Enumerator)

    If no block is given, an Enumerator is returned


57
58
59
60
61
62
63
64
65
66
# File 'lib/ashikawa-core/cursor.rb', line 57

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