class Guacamole::Query

Build a query for ArangoDB

Attributes

connection[R]

Connection to the database

@return [Ashikawa::Core::Collection]

example[RW]

The example to search for

@return [Hash] the example

mapper[R]

The mapper class

@return [Class]

options[RW]

Currently set options

@return [Hash] @api private

Public Class Methods

new(connection, mapper) click to toggle source

Create a new Query

@param [Ashikawa::Core::Collection] connection The collection to use to talk to the database @param [Class] mapper the class of the mapper to use

# File lib/guacamole/query.rb, line 31
def initialize(connection, mapper)
  @connection = connection
  @mapper = mapper
  @options = {}
end

Public Instance Methods

==(other) click to toggle source

Is this {Query} equal to another {Query}

Two {Query} objects are equal if their examples are equal

@param [Query] other The query to compare to

# File lib/guacamole/query.rb, line 69
def ==(other)
  other.instance_of?(self.class) &&
    example == other.example
end
Also aliased as: eql?
each(&block) click to toggle source

Iterate over the result of the query

This will execute the query you have build

# File lib/guacamole/query.rb, line 40
def each(&block)
  return to_enum(__callee__) unless block_given?

  perfom_query ->(document) { block.call mapper.document_to_model(document) }, &block
end
eql?(other)
Alias for: ==
limit(limit) click to toggle source

Limit the results of the query

@param [Fixnum] limit @return [self]

# File lib/guacamole/query.rb, line 50
def limit(limit)
  options[:limit] = limit
  self
end
skip(skip) click to toggle source

The number of results to skip

@param [Fixnum] skip @return [self]

# File lib/guacamole/query.rb, line 59
def skip(skip)
  options[:skip] = skip
  self
end

Private Instance Methods

perfom_query(iterator, &block) click to toggle source

Performs the query against the database connection.

This can be changed by subclasses to implement other types of queries, such as AQL queries.

@param [Lambda] iterator To be called on each document returned from

the database
# File lib/guacamole/query.rb, line 84
def perfom_query(iterator, &block)
  if example
    connection.by_example(example, options).each(&iterator)
  else
    connection.all(options).each(&iterator)
  end
end