Class: DropletKit::PaginatedResource
- Inherits:
-
Object
- Object
- DropletKit::PaginatedResource
- Includes:
- Enumerable
- Defined in:
- lib/droplet_kit/paginated_resource.rb
Constant Summary collapse
- PER_PAGE =
20
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#total ⇒ Object
Returns the value of attribute total.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](index) ⇒ Object
- #each(start = 0, &block) ⇒ Object
-
#initialize(action, resource, *args) ⇒ PaginatedResource
constructor
A new instance of PaginatedResource.
- #last? ⇒ Boolean
- #per_page ⇒ Object
- #total_pages ⇒ Object
Constructor Details
#initialize(action, resource, *args) ⇒ PaginatedResource
Returns a new instance of PaginatedResource.
12 13 14 15 16 17 18 19 20 |
# File 'lib/droplet_kit/paginated_resource.rb', line 12 def initialize(action, resource, *args) @current_page = 0 @total = nil @action = action @resource = resource @collection = [] @args = args @options = args.last.is_a?(Hash) ? args.last : {} end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
9 10 11 |
# File 'lib/droplet_kit/paginated_resource.rb', line 9 def action @action end |
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
9 10 11 |
# File 'lib/droplet_kit/paginated_resource.rb', line 9 def collection @collection end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
9 10 11 |
# File 'lib/droplet_kit/paginated_resource.rb', line 9 def resource @resource end |
#total ⇒ Object
Returns the value of attribute total.
10 11 12 |
# File 'lib/droplet_kit/paginated_resource.rb', line 10 def total @total end |
Instance Method Details
#==(other) ⇒ Object
59 60 61 |
# File 'lib/droplet_kit/paginated_resource.rb', line 59 def ==(other) each_with_index.each.all? { |object, index| object == other[index] } end |
#[](index) ⇒ Object
26 27 28 |
# File 'lib/droplet_kit/paginated_resource.rb', line 26 def [](index) @collection[index] end |
#each(start = 0, &block) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/droplet_kit/paginated_resource.rb', line 30 def each(start = 0, &block) # Start off with the first page if we have no idea of anything yet fetch_next_page if total.nil? return to_enum(:each, start) unless block Array(@collection[start..-1]).each(&block) unless last? start = [@collection.size, start].max fetch_next_page each(start, &block) end self end |
#last? ⇒ Boolean
47 48 49 50 51 |
# File 'lib/droplet_kit/paginated_resource.rb', line 47 def last? return true if total.nil? @current_page == total_pages || total.zero? end |
#per_page ⇒ Object
22 23 24 |
# File 'lib/droplet_kit/paginated_resource.rb', line 22 def per_page @options[:per_page] || PER_PAGE end |
#total_pages ⇒ Object
53 54 55 56 57 |
# File 'lib/droplet_kit/paginated_resource.rb', line 53 def total_pages return nil if total.nil? (total.to_f / per_page).ceil end |