Skip to content

Commit

Permalink
Implement collection caching
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jul 25, 2013
1 parent 6c5c431 commit e5470d2
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/twitter/cursor.rb
Expand Up @@ -37,18 +37,20 @@ def initialize(attrs, key, klass, client, request_method, path, options)
@request_method = request_method.to_sym
@path = path
@options = options
@collection = []
set_attrs(attrs)
end

# @return [Enumerator]
def each(&block)
def each(start = 0, &block)
return to_enum(:each) unless block_given?
@page.each do |element|
Array(@collection[start..-1]).each do |element|
yield element
end
unless last?
start = @collection.size
fetch_next_page
each(&block)
each(start, &block)
end
self
end
Expand Down Expand Up @@ -82,8 +84,8 @@ def fetch_next_page

def set_attrs(attrs)
@attrs = attrs
@page = Array(attrs[@key]).map do |element|
@klass ? @klass.new(element) : element
Array(attrs[@key]).each do |element|
@collection << (@klass ? @klass.new(element) : element)
end
end

Expand Down

1 comment on commit e5470d2

@paracycle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sferik Consider the case where start is 5 and each fetched page has 3 elements. On the first entry to the unless last? block, start will be set to 3, and the next call to each will start yielding elements from the beginning of the next fetched page. However, we wanted to start reading from offset 5. Isn't this a problem?

I suggest something like:

(idx, start) = [start, @collection.size].minmax
@collection[idx..-1].each do |element|
  yield element
end
unless last?
  fetch_next_page
  each(start, &block)
end

Please sign in to comment.