Class: Shamu::Entities::PagedList

Inherits:
List
  • Object
show all
Includes:
Enumerable
Defined in:
lib/shamu/entities/paged_list.rb

Overview

A list of Entity records.

Instance Method Summary collapse

Methods inherited from List

#each, #get

Constructor Details

#initialize(entities, total_count: :not_set, limit: :not_set, offset: :not_set, has_next: :not_set, has_previous: :not_set) ⇒ PagedList

to a number) of records in the entire set. number) of records in the page represented by the list. number) offset from the start of the set that this list represents. available or a proc that returns a bool. available or a proc that returns a bool.

Parameters:

  • entities (Enumerable)

    the raw list of entities.

  • total_count (Integer, #call) (defaults to: :not_set)

    the number (or a proc that resolves

  • limit (Integer, #call) (defaults to: :not_set)

    the maximum number (or a proc that resolves to a

  • offset (Integer, #call) (defaults to: :not_set)

    the number (or a proc that resolves to a

  • has_next (Boolean, #call) (defaults to: :not_set)

    true if there is another page

  • has_previous (Boolean, #call) (defaults to: :not_set)

    true if there is a previous page



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/shamu/entities/paged_list.rb', line 19

def initialize( entities,
                total_count: :not_set,
                limit: :not_set,
                offset: :not_set,
                has_next: :not_set,
                has_previous: :not_set )
  super( entities )

  @total_count  = total_count
  @limit        = limit
  @offset       = offset
  @has_next     = has_next
  @has_previous = has_previous
end

Instance Method Details

#current_pageInteger

Returns the current page number.

Returns:

  • (Integer)

    the current page number.



83
84
85
86
87
88
89
# File 'lib/shamu/entities/paged_list.rb', line 83

def current_page
  if limit > 0
    ( offset / limit ).to_i + 1
  else
    1
  end
end

#first?Boolean

set.

Returns:

  • (Boolean)

    true if this list represents the first page in the



132
133
134
# File 'lib/shamu/entities/paged_list.rb', line 132

def first?
  !previous?
end

#last?Boolean

set.

Returns:

  • (Boolean)

    true if this list represents the last page in the



109
110
111
# File 'lib/shamu/entities/paged_list.rb', line 109

def last?
  !next?
end

#limitInteger Also known as: per_page

Returns the maximum number of records to return in each page.

Returns:

  • (Integer)

    the maximum number of records to return in each page.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/shamu/entities/paged_list.rb', line 51

def limit
  if @limit == :not_set
    if raw_entities.respond_to?( :limit_value )
      raw_entities.limit_value
    elsif raw_entities.respond_to?( :limit )
      raw_entities.limit
    end
  elsif @limit.respond_to?( :call )
    @limit = @limit.call
  else
    @limit
  end
end

#next?Boolean Also known as: has_next?

Returns true if there is another page of data available.

Returns:

  • (Boolean)

    true if there is another page of data available.



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/shamu/entities/paged_list.rb', line 92

def next?
  if @has_next == :not_set
    if raw_entities.respond_to?( :has_next? )
      raw_entities.has_next?
    elsif raw_entities.respond_to?( :last_page? )
      !raw_entities.last_page?
    end
  elsif @has_next.respond_to?( :call )
    @has_next = @has_next.call
  else
    @has_next
  end
end

#offsetInteger

data that that this list contains.

Returns:

  • (Integer)

    the absolute offset into the set for the window of



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/shamu/entities/paged_list.rb', line 68

def offset
  if @offset == :not_set
    if raw_entities.respond_to?( :offset_value )
      raw_entities.offset_value
    elsif raw_entities.respond_to?( :offset )
      raw_entities.offset
    end
  elsif @offset.respond_to?( :call )
    @offset = @offset.call
  else
    @offset
  end
end

#paged?Boolean

See Shamu::Entities::PagedList for paged implementation.

Returns:

  • (Boolean)

    true if the list represents a slice of a larger set.



35
36
37
# File 'lib/shamu/entities/paged_list.rb', line 35

def paged?
  true
end

#previous?Boolean Also known as: has_prev?, has_previous

Returns true if there is another page of data available.

Returns:

  • (Boolean)

    true if there is another page of data available.



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/shamu/entities/paged_list.rb', line 114

def previous?
  if @has_previous == :not_set
    if raw_entities.respond_to?( :has_previous? )
      raw_entities.has_previous?
    elsif raw_entities.respond_to?( :first_page? )
      !raw_entities.first_page?
    end
  elsif @has_previous.respond_to?( :call )
    @has_previous = @has_previous.call
  else
    @has_previous
  end
end

#total_countInteger

Returns the total number of records in the set.

Returns:

  • (Integer)

    the total number of records in the set.



40
41
42
43
44
45
46
47
48
# File 'lib/shamu/entities/paged_list.rb', line 40

def total_count
  if @total_count == :not_set
    raw_entities.total_count
  elsif @total_count.respond_to?( :call )
    @total_count = @total_count.call
  else
    @total_count
  end
end