lib/guacamole/query.rb in guacamole-0.1.0 vs lib/guacamole/query.rb in guacamole-0.2.0
- old
+ new
@@ -2,11 +2,10 @@
module Guacamole
# Build a query for ArangoDB
class Query
include Enumerable
-
# Connection to the database
#
# @return [Ashikawa::Core::Collection]
attr_reader :connection
@@ -37,20 +36,14 @@
end
# Iterate over the result of the query
#
# This will execute the query you have build
- def each
+ def each(&block)
return to_enum(__callee__) unless block_given?
- iterator = ->(document) { yield mapper.document_to_model(document) }
-
- if example
- connection.by_example(example, options).each(&iterator)
- else
- connection.all(options).each(&iterator)
- end
+ perfom_query ->(document) { block.call mapper.document_to_model(document) }, &block
end
# Limit the results of the query
#
# @param [Fixnum] limit
@@ -77,7 +70,24 @@
def ==(other)
other.instance_of?(self.class) &&
example == other.example
end
alias_method :eql?, :==
+
+ private
+
+ # 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
+ def perfom_query(iterator, &block)
+ if example
+ connection.by_example(example, options).each(&iterator)
+ else
+ connection.all(options).each(&iterator)
+ end
+ end
end
end