Class: Perpetuity::Retrieval

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/perpetuity/retrieval.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapper, query, options = {}) ⇒ Retrieval

Returns a new instance of Retrieval.



10
11
12
13
14
15
16
# File 'lib/perpetuity/retrieval.rb', line 10

def initialize mapper, query, options={}
  @mapper = mapper
  @collection_name = mapper.collection_name
  @query = query
  @identity_map = options.fetch(:identity_map) { IdentityMap.new }
  @data_source = mapper.data_source
end

Instance Attribute Details

#identity_mapObject (readonly)

Returns the value of attribute identity_map.



8
9
10
# File 'lib/perpetuity/retrieval.rb', line 8

def identity_map
  @identity_map
end

#result_cacheObject

Returns the value of attribute result_cache.



7
8
9
# File 'lib/perpetuity/retrieval.rb', line 7

def result_cache
  @result_cache
end

#result_limitObject

Returns the value of attribute result_limit.



7
8
9
# File 'lib/perpetuity/retrieval.rb', line 7

def result_limit
  @result_limit
end

#result_offsetObject

Returns the value of attribute result_offset.



7
8
9
# File 'lib/perpetuity/retrieval.rb', line 7

def result_offset
  @result_offset
end

#result_pageObject

Returns the value of attribute result_page.



7
8
9
# File 'lib/perpetuity/retrieval.rb', line 7

def result_page
  @result_page
end

#sort_attributeObject

Returns the value of attribute sort_attribute.



7
8
9
# File 'lib/perpetuity/retrieval.rb', line 7

def sort_attribute
  @sort_attribute
end

#sort_directionObject

Returns the value of attribute sort_direction.



7
8
9
# File 'lib/perpetuity/retrieval.rb', line 7

def sort_direction
  @sort_direction
end

Instance Method Details

#[](index) ⇒ Object



95
96
97
# File 'lib/perpetuity/retrieval.rb', line 95

def [] index
  to_a[index]
end

#clear_cacheObject



120
121
122
# File 'lib/perpetuity/retrieval.rb', line 120

def clear_cache
  @result_cache = nil
end

#countObject



72
73
74
# File 'lib/perpetuity/retrieval.rb', line 72

def count
  @data_source.count(@collection_name, @query)
end

#drop(count) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/perpetuity/retrieval.rb', line 112

def drop count
  retrieval = clone
  retrieval.result_offset = count
  retrieval.clear_cache

  retrieval
end

#each(&block) ⇒ Object



52
53
54
# File 'lib/perpetuity/retrieval.rb', line 52

def each &block
  to_a.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/perpetuity/retrieval.rb', line 99

def empty?
  to_a.empty?
end

#firstObject



76
77
78
# File 'lib/perpetuity/retrieval.rb', line 76

def first
  limit(1).to_a.first
end

#limit(lim) ⇒ Object Also known as: take



103
104
105
106
107
108
109
# File 'lib/perpetuity/retrieval.rb', line 103

def limit lim
  retrieval = clone
  retrieval.result_limit = lim
  retrieval.clear_cache
  
  retrieval
end

#optionsObject



86
87
88
89
90
91
92
93
# File 'lib/perpetuity/retrieval.rb', line 86

def options
  {
    attribute: sort_attribute,
    direction: sort_direction,
    limit: result_limit,
    skip: result_offset
  }
end

#page(page) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/perpetuity/retrieval.rb', line 35

def page page
  retrieval = clone
  retrieval.result_limit ||= 20
  retrieval.result_page = page
  retrieval.result_offset = (page - 1) * retrieval.result_limit
  retrieval.clear_cache
  retrieval
end

#per_page(per) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/perpetuity/retrieval.rb', line 44

def per_page per
  retrieval = clone
  retrieval.result_limit = per
  retrieval.result_offset = (retrieval.result_page - 1) * per
  retrieval.clear_cache
  retrieval
end

#reverseObject



27
28
29
30
31
32
33
# File 'lib/perpetuity/retrieval.rb', line 27

def reverse
  retrieval = clone
  retrieval.sort_direction = retrieval.sort_direction == :descending ? :ascending : :descending
  retrieval.clear_cache

  retrieval
end

#sampleObject



80
81
82
83
84
# File 'lib/perpetuity/retrieval.rb', line 80

def sample
  sample_size = [count, result_limit].compact.max
  result = drop(rand(sample_size)).first
  result
end

#sort(attribute = :name) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/perpetuity/retrieval.rb', line 18

def sort attribute=:name
  retrieval = clone
  retrieval.sort_attribute = attribute
  retrieval.sort_direction = :ascending
  retrieval.clear_cache

  retrieval
end

#to_aObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/perpetuity/retrieval.rb', line 56

def to_a
  @result_cache ||= @data_source.unserialize(@data_source.retrieve(@collection_name, @query, options), @mapper)

  @result_cache.map do |result|
    klass = result.class
    id = @mapper.id_for(result)

    if cached_result = identity_map[klass, id]
      cached_result
    else
      identity_map << result
      result
    end
  end
end