Class: Dimelo::CCP::API::LazzyCollection::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/dimelo/ccp/api/lazzy_collection.rb

Constant Summary collapse

DEFAULT_PAGE_SIZE =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, &block) ⇒ Paginator

Returns a new instance of Paginator.



75
76
77
78
79
# File 'lib/dimelo/ccp/api/lazzy_collection.rb', line 75

def initialize(params, &block)
  @params = params.dup
  @block = block
  @page_cache = []
end

Instance Attribute Details

#page_cacheObject (readonly)

Returns the value of attribute page_cache.



73
74
75
# File 'lib/dimelo/ccp/api/lazzy_collection.rb', line 73

def page_cache
  @page_cache
end

Instance Method Details

#[](index) ⇒ Object



89
90
91
# File 'lib/dimelo/ccp/api/lazzy_collection.rb', line 89

def [](index)
  page_cache[index.to_i] ||= @block.call(params_for_page(index))
end

#firstObject



105
106
107
# File 'lib/dimelo/ccp/api/lazzy_collection.rb', line 105

def first
  self[0]
end

#flattenObject



101
102
103
# File 'lib/dimelo/ccp/api/lazzy_collection.rb', line 101

def flatten
  page_cache.flatten(1)
end

#get_item_at(position) ⇒ Object



97
98
99
# File 'lib/dimelo/ccp/api/lazzy_collection.rb', line 97

def get_item_at(position)
  self[position / page_size][position % page_size]
end

#page_offset(page_index) ⇒ Object



85
86
87
# File 'lib/dimelo/ccp/api/lazzy_collection.rb', line 85

def page_offset(page_index)
  page_index * page_size
end

#page_sizeObject



81
82
83
# File 'lib/dimelo/ccp/api/lazzy_collection.rb', line 81

def page_size
  @page_size ||= (@params[:limit] || DEFAULT_PAGE_SIZE).to_i
end

#params_for_page(index) ⇒ Object



93
94
95
# File 'lib/dimelo/ccp/api/lazzy_collection.rb', line 93

def params_for_page(index)
  @params.merge(:offset => page_offset(index.to_i), :limit => page_size)
end