Class: Benoit::Utils::PaginatedList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/benoit/utils/paginated_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#paginate

Constructor Details

#initialize(list, per_page = nil, &block) ⇒ PaginatedList

Returns a new instance of PaginatedList.



14
15
16
17
18
# File 'lib/benoit/utils/paginated_list.rb', line 14

def initialize(list, per_page=nil, &block)
  @ops = []
  @list = list
  @per_page = per_page
end

Instance Attribute Details

#enumObject (readonly)

Returns the value of attribute enum.



12
13
14
# File 'lib/benoit/utils/paginated_list.rb', line 12

def enum
  @enum
end

#opsObject (readonly)

Returns the value of attribute ops.



12
13
14
# File 'lib/benoit/utils/paginated_list.rb', line 12

def ops
  @ops
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



12
13
14
# File 'lib/benoit/utils/paginated_list.rb', line 12

def per_page
  @per_page
end

#sourceObject (readonly)

Returns the value of attribute source.



12
13
14
# File 'lib/benoit/utils/paginated_list.rb', line 12

def source
  @source
end

Instance Method Details

#at_end?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
# File 'lib/benoit/utils/paginated_list.rb', line 79

def at_end?
  return false if @enum.nil?
  @enum.peek
  false
rescue StopIteration
  true
end

#each(&block) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/benoit/utils/paginated_list.rb', line 21

def each(&block)
  run_queued_operations!
  return paginate! && @enum unless block_given?
  paginated.each do |page|
    block.call(page)
  end
end

#each_with_index(&block) ⇒ Object



29
30
31
32
33
34
# File 'lib/benoit/utils/paginated_list.rb', line 29

def each_with_index(&block)
  run_queued_operations!
  paginated.each_with_index do |p,i|
    block.call(p, i)
  end
end

#map(&block) ⇒ Object



52
53
54
55
56
57
# File 'lib/benoit/utils/paginated_list.rb', line 52

def map(&block)
  @ops << ->(list){
    list.map(&block)
  }
  self
end

#paginate!Object



36
37
38
# File 'lib/benoit/utils/paginated_list.rb', line 36

def paginate!
  @enum ||= @list.each_slice(@per_page)
end

#paginatedObject



40
41
42
43
# File 'lib/benoit/utils/paginated_list.rb', line 40

def paginated
  paginate!
  @enum.next
end

#peekObject



75
76
77
# File 'lib/benoit/utils/paginated_list.rb', line 75

def peek
  @enum.peek if @enum
end

#reverseObject



67
68
69
70
71
72
73
# File 'lib/benoit/utils/paginated_list.rb', line 67

def reverse
  return self if @enum
  @ops << ->(list){
    list.reverse
  }
  self
end

#rewind_list!Object



87
88
89
# File 'lib/benoit/utils/paginated_list.rb', line 87

def rewind_list!
  @enum.rewind
end

#sort_by(&block) ⇒ Object



45
46
47
48
49
50
# File 'lib/benoit/utils/paginated_list.rb', line 45

def sort_by(&block)
  @ops << ->(list){
    list.sort_by(&block)
  }
  self
end

#take(n) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/benoit/utils/paginated_list.rb', line 59

def take(n)
  return self if @enum
  @ops << ->(list){
    list.take(n)
  }
  self
end