Sha256: ff6a76495b7850d48461d29ea942b6638527ba8ca23c384133e4ea565fb7b7f8

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

module Hockey

  class PageRangeError < RangeError; end

  class PagingArray < Array
    attr_reader :current_page
    attr_reader :per_page         # = 25
    attr_reader :total_entries
    attr_reader :total_pages

    def self.paginate(with: [], page: 1)
      obj = self.new
      obj.replace(with[(page - 1) * obj.per_page, obj.per_page])
      obj.update_page_with(page, with.size)
      obj
    rescue
      raise PageRangeError, 'your specified page is out of range in array'
    end

    def initialize
      super
      @per_page = 25
    end

    def update_page(hashobj)
      @current_page  = hashobj['current_page'].to_i
      @total_entries = hashobj['total_entries'].to_i
      @total_pages   = hashobj['total_pages'].to_i

      self
    end

    def update_page_with(page, size)
      @current_page = page
      @total_entries = size
      @total_pages = (size / @per_page) + 1

      self
    end

  end

  class OrderedPagingArray < PagingArray
    attr :order_type
  end

  class ClashesPagingArray < OrderedPagingArray
    attr :symbolicated
    attr :sort_type
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hockeyhelper-0.0.7 lib/hockeyhelper/paging_array.rb
hockeyhelper-0.0.6 lib/hockeyhelper/paging_array.rb
hockeyhelper-0.0.4 lib/hockeyhelper/paging_array.rb