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
- #each(start = 0) ⇒ 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.
10 11 12 13 14 15 16 17 18 |
# File 'lib/droplet_kit/paginated_resource.rb', line 10 def initialize(action, resource, *args) @current_page = 0 @total = nil @action = action @resource = resource @collection = [] @args = args @options = args.last.kind_of?(Hash) ? args.last : {} end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
7 8 9 |
# File 'lib/droplet_kit/paginated_resource.rb', line 7 def action @action end |
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
7 8 9 |
# File 'lib/droplet_kit/paginated_resource.rb', line 7 def collection @collection end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
7 8 9 |
# File 'lib/droplet_kit/paginated_resource.rb', line 7 def resource @resource end |
#total ⇒ Object
Returns the value of attribute total.
8 9 10 |
# File 'lib/droplet_kit/paginated_resource.rb', line 8 def total @total end |
Instance Method Details
#==(other) ⇒ Object
52 53 54 |
# File 'lib/droplet_kit/paginated_resource.rb', line 52 def ==(other) each_with_index.each.all? {|object, index| object == other[index] } end |
#each(start = 0) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/droplet_kit/paginated_resource.rb', line 24 def each(start = 0) # 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_given? Array(@collection[start..-1]).each do |element| yield(element) end unless last? start = [@collection.size, start].max fetch_next_page each(start, &Proc.new) end self end |
#last? ⇒ Boolean
42 43 44 |
# File 'lib/droplet_kit/paginated_resource.rb', line 42 def last? @current_page == total_pages || self.total.zero? end |
#per_page ⇒ Object
20 21 22 |
# File 'lib/droplet_kit/paginated_resource.rb', line 20 def per_page @options[:per_page] || PER_PAGE end |
#total_pages ⇒ Object
46 47 48 49 50 |
# File 'lib/droplet_kit/paginated_resource.rb', line 46 def total_pages return nil if self.total.nil? (self.total.to_f / per_page.to_f).ceil end |