Class: Fixably::ActiveResource::PaginatedCollection
- Inherits:
-
ActiveResource::Collection
- Object
- ActiveResource::Collection
- Fixably::ActiveResource::PaginatedCollection
- Defined in:
- lib/fixably/active_resource/paginated_collection.rb
Instance Attribute Summary collapse
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#parent_association ⇒ Object
Returns the value of attribute parent_association.
-
#parent_resource ⇒ Object
Returns the value of attribute parent_resource.
-
#total_items ⇒ Object
readonly
Returns the value of attribute total_items.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(record) ⇒ Object
-
#initialize(collection_wrapper = nil) ⇒ PaginatedCollection
constructor
A new instance of PaginatedCollection.
- #next_page ⇒ Object
- #next_page? ⇒ Boolean
- #paginated_each ⇒ Object
- #paginated_map ⇒ Object
- #previous_page ⇒ Object
- #previous_page? ⇒ Boolean
Constructor Details
#initialize(collection_wrapper = nil) ⇒ PaginatedCollection
Returns a new instance of PaginatedCollection.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/fixably/active_resource/paginated_collection.rb', line 33 def initialize(collection_wrapper = nil) @limit = collection_wrapper&.fetch("limit") || 0 @offset = collection_wrapper&.fetch("offset") || 0 @total_items = collection_wrapper&.fetch("totalItems") do collection_wrapper.fetch("total_items") end || 0 collection = collection_wrapper&.fetch("items") || [] super(collection) end |
Instance Attribute Details
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
26 27 28 |
# File 'lib/fixably/active_resource/paginated_collection.rb', line 26 def limit @limit end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
27 28 29 |
# File 'lib/fixably/active_resource/paginated_collection.rb', line 27 def offset @offset end |
#parent_association ⇒ Object
Returns the value of attribute parent_association.
31 32 33 |
# File 'lib/fixably/active_resource/paginated_collection.rb', line 31 def parent_association @parent_association end |
#parent_resource ⇒ Object
Returns the value of attribute parent_resource.
30 31 32 |
# File 'lib/fixably/active_resource/paginated_collection.rb', line 30 def parent_resource @parent_resource end |
#total_items ⇒ Object (readonly)
Returns the value of attribute total_items.
28 29 30 |
# File 'lib/fixably/active_resource/paginated_collection.rb', line 28 def total_items @total_items end |
Class Method Details
.attributes(collection_wrapper) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/fixably/active_resource/paginated_collection.rb', line 17 def attributes(collection_wrapper) if collection_wrapper.respond_to?(:attributes) collection_wrapper.attributes else collection_wrapper end end |
.paginatable?(value) ⇒ Boolean
9 10 11 12 13 14 15 |
# File 'lib/fixably/active_resource/paginated_collection.rb', line 9 def paginatable?(value) collection = attributes(value) return false unless collection.is_a?(Hash) interface = %w[limit offset total_items items] (interface - collection.keys).empty? end |
Instance Method Details
#<<(record) ⇒ Object
44 45 46 |
# File 'lib/fixably/active_resource/paginated_collection.rb', line 44 def <<(record) CreateHasManyRecord.(record: record, collection: self) end |
#next_page ⇒ Object
65 66 67 68 69 |
# File 'lib/fixably/active_resource/paginated_collection.rb', line 65 def next_page raise StopIteration, "There are no more pages" unless next_page? where(limit: limit, offset: offset + limit) end |
#next_page? ⇒ Boolean
71 72 73 |
# File 'lib/fixably/active_resource/paginated_collection.rb', line 71 def next_page? (limit + offset) < total_items end |
#paginated_each ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/fixably/active_resource/paginated_collection.rb', line 48 def paginated_each page = self loop do page.each { yield(_1) } break unless page.next_page? page = page.next_page end end |
#paginated_map ⇒ Object
59 60 61 62 63 |
# File 'lib/fixably/active_resource/paginated_collection.rb', line 59 def paginated_map [].tap do |records| paginated_each { records << _1 } end end |
#previous_page ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/fixably/active_resource/paginated_collection.rb', line 75 def previous_page raise StopIteration, "There are no more pages" unless previous_page? new_offset = offset - limit new_limit = limit new_limit += new_offset if new_offset.negative? new_offset = 0 if new_offset.negative? where(limit: new_limit, offset: new_offset) end |
#previous_page? ⇒ Boolean
86 87 88 |
# File 'lib/fixably/active_resource/paginated_collection.rb', line 86 def previous_page? offset.positive? end |