Class: Fixably::ActiveResource::PaginatedCollection

Inherits:
ActiveResource::Collection
  • Object
show all
Defined in:
lib/fixably/active_resource/paginated_collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#limitObject (readonly)

Returns the value of attribute limit.



26
27
28
# File 'lib/fixably/active_resource/paginated_collection.rb', line 26

def limit
  @limit
end

#offsetObject (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_associationObject

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_resourceObject

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_itemsObject (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

Returns:

  • (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_pageObject

Raises:

  • (StopIteration)


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

Returns:

  • (Boolean)


71
72
73
# File 'lib/fixably/active_resource/paginated_collection.rb', line 71

def next_page?
  (limit + offset) < total_items
end

#paginated_eachObject



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_mapObject



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_pageObject

Raises:

  • (StopIteration)


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

Returns:

  • (Boolean)


86
87
88
# File 'lib/fixably/active_resource/paginated_collection.rb', line 86

def previous_page?
  offset.positive?
end