Skip to content

Commit

Permalink
add ability for iteration past page boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
theganyo committed Dec 19, 2012
1 parent 19fc4e5 commit 20b07bd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -181,6 +181,10 @@ usergrid_iron/spec/spec_settings.yaml to match.)

## Release notes

### 0.0.6 (unreleased)
* New features
1. iterators can now optionally cross page boundaries, use `collection.follow_cursor.each`

### 0.0.5
* New features
1. added create_* method for application
Expand Down
16 changes: 14 additions & 2 deletions lib/usergrid/core/collection.rb
Expand Up @@ -2,10 +2,12 @@ module Usergrid
class Collection < Entity
include Enumerable

attr_accessor :iterator_follows_cursor
attr_reader :query_params

def initialize(url, api_url, options={}, response=nil)
super url, api_url, options, response
@iterator_follows_cursor = false
end

def collection
Expand All @@ -32,6 +34,17 @@ def save

def each(&block)
entities.each &block
while cursor
next_page
entities.each &block
end if iterator_follows_cursor
end

# use in conjunction with each() like: collection.follow_cursor.each {|e| }
def follow_cursor
my_clone = self.clone
my_clone.iterator_follows_cursor = true
my_clone
end

def create_entity(data)
Expand Down Expand Up @@ -64,12 +77,11 @@ def empty?
end

def cursor
response.data.cursor || nil
response.data['cursor']
end

def next_page
query(nil, @query_params.merge({cursor: cursor}))
end

end
end
2 changes: 1 addition & 1 deletion lib/usergrid/version.rb
@@ -1,3 +1,3 @@
module Usergrid
VERSION = '0.0.5'
VERSION = '0.0.6'
end
27 changes: 21 additions & 6 deletions spec/usergrid/core/collection_spec.rb
Expand Up @@ -5,10 +5,8 @@
@user = create_random_user @application, true

@collection = @application['tests'].collection
@entity_data = []
(1..10).each do |i|
test = { name: "name_#{i}", value: "value_#{i+1}" }
@entity_data << test
@entity_data = (1..25).collect do |i|
{ name: "name_#{i}", value: "value_#{i+1}" }
end
@collection.create_entities @entity_data
end
Expand Down Expand Up @@ -51,8 +49,8 @@
it "should be able to respect query reversal and limits" do
@collection.query nil, reversed: true, start: 5, cursor: nil, limit: 2, permission: nil
@collection.size.should eq 2
@collection[0].name.should eq @entity_data[9][:name]
@collection[1].name.should eq @entity_data[8][:name]
@collection[0].name.should eq @entity_data[@entity_data.size-1][:name]
@collection[1].name.should eq @entity_data[@entity_data.size-2][:name]
end

it "should be able to select the start by uuid" do
Expand All @@ -79,4 +77,21 @@
entity.name.should eq @entity_data[4][:name]
end

it "should be able to iterate within the page" do
@collection.query
@collection.cursor.should_not be_nil
count = 0
page_size = @collection.count
@collection.each {|e| count += 1 }
count.should eq page_size
end

it "should be able to iterate over pages" do
@collection.query
@collection.cursor.should_not be_nil
count = 0
@collection.follow_cursor.each {|e| count += 1 }
count.should eq @entity_data.size
end

end

0 comments on commit 20b07bd

Please sign in to comment.