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, criteria) ⇒ Retrieval

Returns a new instance of Retrieval.



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

def initialize mapper, criteria
  @mapper = mapper
  @class = mapper.mapped_class
  @criteria = criteria
  @data_source = mapper.data_source
end

Instance Attribute Details

#quantity_per_pageObject

Returns the value of attribute quantity_per_page.



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

def quantity_per_page
  @quantity_per_page
end

#result_limitObject

Returns the value of attribute result_limit.



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

def result_limit
  @result_limit
end

#result_pageObject

Returns the value of attribute result_page.



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

def result_page
  @result_page
end

#sort_attributeObject

Returns the value of attribute sort_attribute.



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

def sort_attribute
  @sort_attribute
end

#sort_directionObject

Returns the value of attribute sort_direction.



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

def sort_direction
  @sort_direction
end

Instance Method Details

#[](index) ⇒ Object



64
65
66
# File 'lib/perpetuity/retrieval.rb', line 64

def [] index
  to_a[index]
end

#countObject



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

def count
  @data_source.count(@class, @criteria)
end

#each(&block) ⇒ Object



43
44
45
# File 'lib/perpetuity/retrieval.rb', line 43

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

#empty?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/perpetuity/retrieval.rb', line 68

def empty?
  to_a.empty?
end

#limit(lim) ⇒ Object



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

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

#optionsObject



55
56
57
58
59
60
61
62
# File 'lib/perpetuity/retrieval.rb', line 55

def options
  {
    attribute: sort_attribute,
    direction: sort_direction,
    limit: result_limit || quantity_per_page,
    page: result_page
  }
end

#page(page) ⇒ Object



30
31
32
33
34
35
# File 'lib/perpetuity/retrieval.rb', line 30

def page page
  retrieval = clone
  retrieval.result_page = page
  retrieval.quantity_per_page = 20
  retrieval
end

#per_page(per) ⇒ Object



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

def per_page per
  retrieval = clone
  retrieval.quantity_per_page = per
  retrieval
end

#reverseObject



23
24
25
26
27
28
# File 'lib/perpetuity/retrieval.rb', line 23

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

  retrieval
end

#sort(attribute = :name) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/perpetuity/retrieval.rb', line 15

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

  retrieval
end

#to_aObject



47
48
49
# File 'lib/perpetuity/retrieval.rb', line 47

def to_a
  @results ||= @data_source.unserialize(@data_source.retrieve(@class, @criteria, options), @mapper)
end