Permalink
Browse files

Implement collection caching

  • Loading branch information...
1 parent 9ae4621 commit d484d7d7d7a0956f9b4fa6791a911ed7c9522cba @sferik committed Jul 25, 2013
Showing with 14 additions and 5 deletions.
  1. +7 −5 lib/twitter/cursor.rb
  2. +7 −0 spec/twitter/cursor_spec.rb
View
12 lib/twitter/cursor.rb
@@ -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, start].max
fetch_next_page
- each(&block)
+ each(start, &block)
end
self
end
@@ -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
View
7 spec/twitter/cursor_spec.rb
@@ -18,6 +18,13 @@
@client.follower_ids("sferik").each{count += 1}
expect(count).to eq 6
end
+ context "with start" do
+ it "iterates" do
+ count = 0
+ @client.follower_ids("sferik").each(5){count += 1}
+ expect(count).to eq 1
+ end
+ end
end
describe "#first?" do

0 comments on commit d484d7d

Please sign in to comment.