-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from gadabout/feature/pagination
Add pagination and collections.
- Loading branch information
Showing
21 changed files
with
420 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
module Lurch | ||
class Collection | ||
include Enumerable | ||
|
||
attr_reader :resources | ||
|
||
def initialize(resources, paginator) | ||
@resources = resources | ||
@paginator = paginator | ||
end | ||
|
||
def each(&block) | ||
block_given? ? enum.each(&block) : enum | ||
end | ||
|
||
def each_page(&block) | ||
block_given? ? page_enum.each(&block) : page_enum | ||
end | ||
|
||
def size | ||
@paginator ? @paginator.record_count : @resources.size | ||
end | ||
|
||
def page_size | ||
@resources.size | ||
end | ||
|
||
def page_count | ||
@paginator.page_count if @paginator | ||
end | ||
|
||
def next_collection | ||
return @next_collection if defined?(@next_collection) | ||
@next_collection = @paginator ? @paginator.next_collection : nil | ||
end | ||
|
||
def prev_collection | ||
return @prev_collection if defined?(@prev_collection) | ||
@prev_collection = @paginator ? @paginator.prev_collection : nil | ||
end | ||
|
||
def first_collection | ||
return @first_collection if defined?(@first_collection) | ||
@first_collection = @paginator ? @paginator.first_collection : nil | ||
end | ||
|
||
def last_collection | ||
return @last_collection if defined?(@last_collection) | ||
@last_collection = @paginator ? @paginator.last_collection : nil | ||
end | ||
|
||
def next? | ||
@paginator ? @paginator.next? : false | ||
end | ||
|
||
def prev? | ||
@paginator ? @paginator.prev? : false | ||
end | ||
|
||
def first? | ||
@paginator ? @paginator.first? : false | ||
end | ||
|
||
def last? | ||
@paginator ? @paginator.last? : false | ||
end | ||
|
||
def inspect | ||
suffix = @resources.first ? "[#{Inflector.classify(@resources.first.type)}]" : "" | ||
inspection = size ? ["size: #{size}"] : [] | ||
inspection << ["pages: #{page_count}"] if page_count | ||
"#<#{self.class}#{suffix} #{inspection.join(', ')}>" | ||
end | ||
|
||
private | ||
|
||
def enum | ||
Enumerator.new(-> { size }) do |yielder| | ||
@resources.each do |resource| | ||
yielder.yield(resource) | ||
end | ||
|
||
if next_collection | ||
next_collection.each do |resource| | ||
yielder.yield(resource) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def page_enum | ||
Enumerator.new(-> { page_count }) do |yielder| | ||
yielder.yield(@resources) | ||
|
||
if next_collection | ||
next_collection.each_page do |page| | ||
yielder.yield(page) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
module Lurch | ||
class Paginator | ||
def initialize(store, document, inflector, config) | ||
@store = store | ||
@links = document["links"] | ||
@meta = document["meta"] | ||
@config = config | ||
@inflector = inflector | ||
end | ||
|
||
def record_count | ||
key = @inflector.encode_key(@config.pagination_record_count_key) | ||
@meta[key] | ||
end | ||
|
||
def page_count | ||
key = @inflector.encode_key(@config.pagination_page_count_key) | ||
@meta[key] | ||
end | ||
|
||
def next_collection | ||
next_link && @store.load_from_url(next_link) | ||
end | ||
|
||
def prev_collection | ||
prev_link && @store.load_from_url(prev_link) | ||
end | ||
|
||
def first_collection | ||
first_link && @store.load_from_url(first_link) | ||
end | ||
|
||
def last_collection | ||
last_link && @store.load_from_url(last_link) | ||
end | ||
|
||
def next? | ||
!!next_link | ||
end | ||
|
||
def prev? | ||
!!prev_link | ||
end | ||
|
||
def first? | ||
!!first_link | ||
end | ||
|
||
def last? | ||
!!last_link | ||
end | ||
|
||
private | ||
|
||
def next_link | ||
@links["next"] | ||
end | ||
|
||
def prev_link | ||
@links["prev"] | ||
end | ||
|
||
def first_link | ||
@links["first"] | ||
end | ||
|
||
def last_link | ||
@links["last"] | ||
end | ||
end | ||
end |
Oops, something went wrong.