Skip to content

Commit

Permalink
Add currentOffset to RKPaginator
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuchid authored and blakewatters committed Mar 15, 2013
1 parent 96b9d6f commit 2f11970
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Code/Network/RKPaginator.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@
*/
@property (nonatomic, readonly) NSUInteger currentPage;

/**
Returns the offset based off the page for the most recently loaded objects.
@return The offset for the current page of objects.
@exception NSInternalInconsistencyException Raised if `isLoaded` is equal to `NO`.
*/
@property (nonatomic, readonly) NSUInteger offset;

/**
Returns the number of pages in the total resource collection.
Expand Down
26 changes: 25 additions & 1 deletion Code/Network/RKPaginator.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ @interface RKPaginator ()
@property (nonatomic, strong) RKObjectRequestOperation *objectRequestOperation;
@property (nonatomic, copy) NSArray *responseDescriptors;
@property (nonatomic, assign, readwrite) NSUInteger currentPage;
@property (nonatomic, assign, readwrite) NSUInteger offset;
@property (nonatomic, assign, readwrite) NSUInteger pageCount;
@property (nonatomic, assign, readwrite) NSUInteger objectCount;
@property (nonatomic, assign, readwrite) BOOL loaded;
Expand Down Expand Up @@ -71,6 +72,7 @@ - (id)initWithRequest:(NSURLRequest *)request
self.currentPage = NSNotFound;
self.pageCount = NSNotFound;
self.objectCount = NSNotFound;
self.offset = NSNotFound;
self.perPage = RKPaginatorDefaultPerPage;
self.loaded = NO;
}
Expand Down Expand Up @@ -114,6 +116,11 @@ - (BOOL)hasCurrentPage
return _currentPage != NSNotFound;
}

- (BOOL)hasOffset
{
return _offset != NSNotFound;
}

- (BOOL)hasPageCount
{
return _pageCount != NSNotFound;
Expand All @@ -131,6 +138,12 @@ - (NSUInteger)currentPage
return _currentPage;
}

- (NSUInteger)offset
{
if ([self hasOffset]) return _offset;
return [self hasCurrentPage] ? ((_currentPage - 1) * _perPage) : 0;
}

- (BOOL)hasNextPage
{
NSAssert(self.isLoaded, @"Cannot determine hasNextPage: paginator is not loaded.");
Expand Down Expand Up @@ -227,9 +240,10 @@ - (void)waitUntilFinished

- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p patternURL=%@ isLoaded=%@ perPage=%ld currentPage=%@ pageCount=%@ objectCount=%@>",
return [NSString stringWithFormat:@"<%@: %p patternURL=%@ isLoaded=%@ perPage=%ld currentPage=%@ offset=%@ pageCount=%@ objectCount=%@>",
NSStringFromClass([self class]), self, self.patternURL, self.isLoaded ? @"YES" : @"NO", (long) self.perPage,
[self hasCurrentPage] ? @(self.currentPage) : @"???",
[self hasOffset] ? @(self.offset) : @"???",
[self hasPageCount] ? @(self.pageCount) : @"???",
[self hasObjectCount] ? @(self.objectCount) : @"???"];
}
Expand Down Expand Up @@ -292,4 +306,14 @@ - (void)setObjectCountNumber:(NSNumber *)objectCountNumber
self.objectCount = [objectCountNumber unsignedIntegerValue];
}

- (NSNumber *)offsetNumber
{
return [NSNumber numberWithUnsignedInteger:self.offset];
}

- (void)setOffsetNumber:(NSNumber *)offsetNumber
{
self.offset = [offsetNumber unsignedIntegerValue];
}

@end
30 changes: 30 additions & 0 deletions Tests/Logic/ObjectMapping/RKPaginatorTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ @interface RKPaginatorTest : RKTestCase
@implementation RKPaginatorTest

static NSString * const RKPaginatorTestResourcePathPattern = @"/paginate?per_page=:perPage&page=:currentPage";
static NSString * const RKPaginatorTestResourcePathPatternWithOffset = @"/paginate?limit=:perPage&offset=:offset";

- (void)setUp
{
Expand All @@ -61,6 +62,7 @@ - (RKObjectMapping *)paginationMapping
[paginationMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"current_page" toKeyPath:@"currentPage"]];
[paginationMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"per_page" toKeyPath:@"perPage"]];
[paginationMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"total_entries" toKeyPath:@"objectCount"]];
[paginationMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"offset" toKeyPath:@"offset"]];

return paginationMapping;
}
Expand All @@ -70,6 +72,11 @@ - (NSURL *)paginationURL
return [NSURL URLWithString:RKPaginatorTestResourcePathPattern relativeToURL:[RKTestFactory baseURL]];
}

- (NSURL *)paginationOffsetURL
{
return [NSURL URLWithString:RKPaginatorTestResourcePathPatternWithOffset relativeToURL:[RKTestFactory baseURL]];
}

#pragma mark - Test Cases

- (void)testInitCopiesPatternURL
Expand Down Expand Up @@ -145,6 +152,17 @@ - (void)testThatURLReturnedReflectsStateOfPaginator
expect([[mockPaginator URL] query]).to.equal(@"per_page=25&page=1");
}

- (void)testThatURLReturnedReflectsStateOfPaginatorWithOffset
{
NSURLRequest *request = [NSURLRequest requestWithURL:self.paginationOffsetURL];
RKPaginator *paginator = [[RKPaginator alloc] initWithRequest:request paginationMapping:self.paginationMapping responseDescriptors:@[ self.responseDescriptor ]];
id mockPaginator = [OCMockObject partialMockForObject:paginator];
NSUInteger currentPage = 1;
[[[mockPaginator stub] andReturnValue:OCMOCK_VALUE(currentPage)] currentPage];
expect([[mockPaginator URL] query]).to.equal(@"limit=25&offset=0");
}


- (void)testLoadingAPageOfObjects
{
NSURLRequest *request = [NSURLRequest requestWithURL:self.paginationURL];
Expand Down Expand Up @@ -410,4 +428,16 @@ - (void)testPaginatorWithPaginationURLThatIncludesTrailingSlash
expect(paginator.objectCount).to.equal(0);
}

- (void)testOffsetNumberOfNextPage
{
NSURLRequest *request = [NSURLRequest requestWithURL:self.paginationURL];
RKPaginator *paginator = [[RKPaginator alloc] initWithRequest:request paginationMapping:self.paginationMapping responseDescriptors:@[ self.responseDescriptor ]];
[paginator loadPage:1];
[paginator waitUntilFinished];
expect(paginator.offset).to.equal(0);
[paginator loadNextPage];
[paginator waitUntilFinished];
expect(paginator.offset).to.equal(3);
}

@end

0 comments on commit 2f11970

Please sign in to comment.