Class: RedisPagination::Paginator::SortedSetPaginator

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_pagination/paginator/sorted_set_paginator.rb

Instance Method Summary collapse

Constructor Details

#initialize(key, options = {}) ⇒ SortedSetPaginator

Initialize a new instance with a given Redis key and options.

Parameters:

  • key (String)

    Redis sorted set key.

  • options (Hash) (defaults to: {})

    Options for paginator.



8
9
10
# File 'lib/redis_pagination/paginator/sorted_set_paginator.rb', line 8

def initialize(key, options = {})
  @key = key
end

Instance Method Details

#all(options = {}) ⇒ Object

Retrieve all items for key.

Parameters:

  • options (Hash) (defaults to: {})

    Options. Valid options are :with_scores and :reverse. :with_scores controls whether the score is returned along with the item. Default is true. :reverse controls whether to return items in highest-to-lowest (true) or loweest-to-highest order (false). Default is true.

Returns:

  • a Hash containing :total_items and :items.



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/redis_pagination/paginator/sorted_set_paginator.rb', line 66

def all(options = {})
  with_scores = options.has_key?(:with_scores) ? options[:with_scores] : true
  reverse = options.has_key?(:reverse) ? options[:reverse] : true

  {
    :total_items => total_items,
    :items => if reverse
      RedisPagination.redis.zrevrange(@key, 0, -1, :with_scores => with_scores)
    else
      RedisPagination.redis.zrange(@key, 0, -1, :with_scores => with_scores)
    end
  }
end

#page(page, options = {}) ⇒ Object

Retrieve a page of items for key.

Parameters:

  • page (int)

    Page of items to retrieve.

  • options (Hash) (defaults to: {})

    Options. Valid options are :page_size, :with_scores and :reverse. :page_size controls the page size for the call. Default is RedisPagination.page_size. :with_scores controls whether the score is returned along with the item. Default is true. :reverse controls whether to return items in highest-to-lowest (true) or loweest-to-highest order (false). Default is true.

Returns:

  • a Hash containing :current_page, :total_pages, :total_items and :items.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/redis_pagination/paginator/sorted_set_paginator.rb', line 37

def page(page, options = {})
  current_page = page < 1 ? 1 : page
  index_for_redis = current_page - 1
  page_size = options[:page_size] || RedisPagination.page_size
  starting_offset = index_for_redis * page_size
  ending_offset = (starting_offset + page_size) - 1

  with_scores = options.has_key?(:with_scores) ? options[:with_scores] : true
  reverse = options.has_key?(:reverse) ? options[:reverse] : true

  {
    :current_page => current_page,
    :total_pages => total_pages(page_size),
    :total_items => total_items,
    :items => if reverse
      RedisPagination.redis.zrevrange(@key, starting_offset, ending_offset, :with_scores => with_scores)
    else
      RedisPagination.redis.zrange(@key, starting_offset, ending_offset, :with_scores => with_scores)
    end
  }
end

#total_itemsObject

Return the total number of items for key.

Returns:

  • the total number of items for key.



24
25
26
# File 'lib/redis_pagination/paginator/sorted_set_paginator.rb', line 24

def total_items
  RedisPagination.redis.zcard(@key)
end

#total_pages(page_size = RedisPagination.page_size) ⇒ Object

Return the total number of pages for key.

Parameters:

  • page_size (int) (defaults to: RedisPagination.page_size)

    Page size to calculate total number of pages.

Returns:

  • the total number of pages for key.



17
18
19
# File 'lib/redis_pagination/paginator/sorted_set_paginator.rb', line 17

def total_pages(page_size = RedisPagination.page_size)
  (RedisPagination.redis.zcard(@key) / page_size.to_f).ceil
end