Class: Picky::Results

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/picky/results.rb,
lib/picky/results/exact_first.rb

Overview

This is the internal results object. Usually, to_marshal, or to_json is called on it to get a string for the answer.

Defined Under Namespace

Modules: ExactFirst

Constant Summary collapse

@@log_time_format =

For logging.

"%Y-%m-%d %H:%M:%S".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query = nil, amount = 0, offset = 0, allocations = Query::Allocations.new, extra_allocations = nil, unique = false) ⇒ Results

Takes instances of Query::Allocations as param.



20
21
22
23
24
25
26
27
# File 'lib/picky/results.rb', line 20

def initialize query = nil, amount = 0, offset = 0, allocations = Query::Allocations.new, extra_allocations = nil, unique = false
  @amount      = amount
  @query       = query
  @offset      = offset
  @allocations = allocations
  @extra_allocations = extra_allocations
  @unique      = unique
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



13
14
15
# File 'lib/picky/results.rb', line 13

def amount
  @amount
end

#durationObject

Duration default is 0.



78
79
80
# File 'lib/picky/results.rb', line 78

def duration
  @duration || 0
end

#offsetObject (readonly)

Returns the value of attribute offset.



13
14
15
# File 'lib/picky/results.rb', line 13

def offset
  @offset
end

#queryObject (readonly)

Returns the value of attribute query.



13
14
15
# File 'lib/picky/results.rb', line 13

def query
  @query
end

#sortingObject (readonly)

Returns the value of attribute sorting.



13
14
15
# File 'lib/picky/results.rb', line 13

def sorting
  @sorting
end

Instance Method Details

#allocationsObject



36
37
38
39
# File 'lib/picky/results.rb', line 36

def allocations
  prepare! *(@prepared || [@extra_allocations, @unique])
  @allocations
end

#each(&block) ⇒ Object



54
55
56
# File 'lib/picky/results.rb', line 54

def each &block
  allocations.each &block
end

#ids(only = amount) ⇒ Object

Forwards to allocations.

Note that this is an expensive call and should not be done repeatedly. Just keep a reference to the result.

TODO Rewrite such that this triggers calculation, not prepare!



66
67
68
# File 'lib/picky/results.rb', line 66

def ids only = amount
  allocations.ids only
end

#log_typeObject

The first character in the blog designates what type of query it is.

No calculated ids means: No results.



110
111
112
# File 'lib/picky/results.rb', line 110

def log_type
  amount.zero?? :'.' : :'>'
end

#prepare!(extra_allocations = nil, unique = false) ⇒ Object

This starts the actual processing.

Without this, the allocations are not processed, and no ids are calculated.



46
47
48
49
50
51
52
# File 'lib/picky/results.rb', line 46

def prepare! extra_allocations = nil, unique = false
  return if @prepared == [extra_allocations, unique] # cached?
  @prepared = [extra_allocations, unique] # cache!
  unique ?
    @allocations.process_unique!(amount, offset, extra_allocations, sorting) :
    @allocations.process!(amount, offset, extra_allocations, sorting)
end

#sort_by(&sorting) ⇒ Object

Provide a block which accepts a result id.



32
33
34
# File 'lib/picky/results.rb', line 32

def sort_by &sorting
  @sorting = sorting
end

#to_hashObject

Returns a hash with the allocations, offset, duration and total.



84
85
86
87
88
89
90
91
# File 'lib/picky/results.rb', line 84

def to_hash
  {
    allocations: allocations.to_result,
    offset:      offset,
    duration:    duration,
    total:       total
  }
end

#to_json(options = {}) ⇒ Object

Convert to json format.



95
96
97
# File 'lib/picky/results.rb', line 95

def to_json options = {}
  MultiJson.encode to_hash, options
end

#to_sObject



102
103
104
# File 'lib/picky/results.rb', line 102

def to_s
  "#{log_type}|#{Time.now.strftime @@log_time_format}|#{'%8f' % duration}|#{'%-50s' % query}|#{'%8d' % total}|#{'%4d' % offset}|#{'%2d' % allocations.size}|"
end

#totalObject

The total results. Forwards to the allocations.



72
73
74
# File 'lib/picky/results.rb', line 72

def total
  @total ||= allocations.total || 0
end