Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add ability for iteration past page boundaries

  • Loading branch information...
commit 20b07bdfceb969e352e0e2689ecf313aa0695e21 1 parent 19fc4e5
@theganyo authored
View
4 README.md
@@ -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
View
16 lib/usergrid/core/collection.rb
@@ -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
@@ -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)
@@ -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
View
2  lib/usergrid/version.rb
@@ -1,3 +1,3 @@
module Usergrid
- VERSION = '0.0.5'
+ VERSION = '0.0.6'
end
View
27 spec/usergrid/core/collection_spec.rb
@@ -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
@@ -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
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.