lib/picky/results.rb in picky-2.7.0 vs lib/picky/results.rb in picky-3.0.0.pre1
- old
+ new
@@ -1,89 +1,93 @@
-# This is the internal results object. Usually, to_marshal, or to_json
-# is called on it to get a string for the answer.
-#
-class Results
+module Picky
- # Duration is set externally by the query.
+ # This is the internal results object. Usually, to_marshal, or to_json
+ # is called on it to get a string for the answer.
#
- attr_writer :duration
- attr_reader :allocations, :offset, :amount
+ class Results
- # Takes instances of Query::Allocations as param.
- #
- def initialize amount = 0, offset = 0, allocations = Query::Allocations.new
- @offset = offset
- @amount = amount
- @allocations = allocations
- end
- # Create new results and calculate the ids.
- #
- def self.from amount, offset, allocations
- results = new amount, offset, allocations
- results.prepare!
- results
- end
+ # Duration is set externally by the query.
+ #
+ attr_writer :duration
+ attr_reader :allocations, :offset, :amount
- # Returns a hash with the allocations, offset, duration and total.
- #
- def serialize
- { allocations: allocations.to_result,
- offset: offset,
- duration: duration,
- total: total }
- end
- # The default format is json.
- #
- def to_response options = {}
- to_json options
- end
- # Convert to json format.
- #
- def to_json options = {}
- serialize.to_json options
- end
+ # Takes instances of Query::Allocations as param.
+ #
+ def initialize amount = 0, offset = 0, allocations = Query::Allocations.new
+ @offset = offset
+ @amount = amount
+ @allocations = allocations
+ end
+ # Create new results and calculate the ids.
+ #
+ def self.from amount, offset, allocations
+ results = new amount, offset, allocations
+ results.prepare!
+ results
+ end
- # This starts the actual processing.
- #
- # Without this, the allocations are not processed,
- # and no ids are calculated.
- #
- def prepare!
- allocations.process! amount, offset
- end
+ # Returns a hash with the allocations, offset, duration and total.
+ #
+ def serialize
+ { allocations: allocations.to_result,
+ offset: offset,
+ duration: duration,
+ total: total }
+ end
+ # Convert to json format.
+ #
+ def to_json options = {}
+ serialize.to_json options
+ end
+ # The default format is json.
+ #
+ alias to_response to_json
- # Duration default is 0.
- #
- def duration
- @duration || 0
- end
- # The total results. Delegates to the allocations.
- #
- # Caches.
- #
- def total
- @total || @total = allocations.total || 0
- end
+ # This starts the actual processing.
+ #
+ # Without this, the allocations are not processed,
+ # and no ids are calculated.
+ #
+ def prepare!
+ allocations.process! amount, offset
+ end
- # Convenience methods.
- #
+ # Duration default is 0.
+ #
+ def duration
+ @duration || 0
+ end
+ # The total results. Delegates to the allocations.
+ #
+ # Caches.
+ #
+ def total
+ @total || @total = allocations.total || 0
+ end
- # Delegates to allocations.
- #
- def ids amount = 20
- allocations.ids amount
- end
+ # Convenience methods.
+ #
- # Human readable log.
- #
- def to_log query
- "#{log_type}|#{Time.now.to_s(:db)}|#{'%8f' % duration}|#{'%-50s' % query}|#{'%8d' % total}|#{'%4d' % offset}|#{'%2d' % allocations.size}|"
- end
- # The first character in the blog designates what type of query it is.
- #
- # No calculated ids means: No results.
- #
- def log_type
- amount.zero?? :'.' : :'>'
+ # Delegates to allocations.
+ #
+ def ids amount = 20
+ allocations.ids amount
+ end
+
+ # Human readable log.
+ #
+ # TODO Should this be to_s? (And it should also hold the original query?)
+ #
+ def to_log query
+ "#{log_type}|#{Time.now.to_s(:db)}|#{'%8f' % duration}|#{'%-50s' % query}|#{'%8d' % total}|#{'%4d' % offset}|#{'%2d' % allocations.size}|"
+ end
+ # The first character in the blog designates what type of query it is.
+ #
+ # No calculated ids means: No results.
+ #
+ def log_type
+ amount.zero?? :'.' : :'>'
+ end
+
end
end
\ No newline at end of file