Class: Balanced::Pager

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/balanced/pager.rb

Constant Summary collapse

DEFAULT_SEP =
/[&;] */n
DEFAULT_LIMIT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(href, options = {}) ⇒ Pager

A pager for paginating through resource records.

Parameters:

  • uri (String)

    the uri of the resource

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

Options Hash (options):

  • limit (Integer)
  • offset (Integer)
  • per (Integer)

    an alias for the :limit option



20
21
22
23
24
25
# File 'lib/balanced/pager.rb', line 20

def initialize(href, options = {})
  @href = href
  @options = options
  @page = nil
  @resource_class = nil
end

Instance Attribute Details

#hrefObject

Returns the value of attribute href.



10
11
12
# File 'lib/balanced/pager.rb', line 10

def href
  @href
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/balanced/pager.rb', line 11

def options
  @options
end

Instance Method Details

#all(options = {}) ⇒ Object



156
157
158
# File 'lib/balanced/pager.rb', line 156

def all(options = {})
  paginate(options).to_a
end

#create(options = {}) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/balanced/pager.rb', line 169

def create(options={})
  opts = Balanced::Utils.indifferent_read_access options
  opts[:href] = @href
  # if we don't have a media type for the href,
  # let's try to inspect the url to look it up from the
  # registry
  if @resource_class.nil?
    @resource_class = Balanced.from_href(@href)
  end
  @resource_class.new(opts).save
end

#current_pageObject



73
74
75
# File 'lib/balanced/pager.rb', line 73

def current_page
  (offset / limit) + 1
end

#each {|record| ... } ⇒ Array

Returns Iterates through the current page of records.

Yields:

  • (record)

Returns:

  • (Array)

    Iterates through the current page of records.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/balanced/pager.rb', line 85

def each
  return enum_for :each unless block_given?

  load! unless @page
  loop do
    items.each do |r|
      envelope = {
        :meta => @page[:meta],
        :links => @page[:links],
        @resource_class.collection_name.to_sym => [r]
      }
      yield resource_class.construct_from_response(envelope)
    end
    raise StopIteration if @page[:meta][:next].nil?
    self.next
  end
end

#fetch(uri) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/balanced/pager.rb', line 160

def fetch(uri)
  if resource_class.respond_to? :find
    raise NoMethodError,
        "#find must be called on #{resource_class} directly"
  end

  resource_class.fetch uri
end

#fetch_each {|record| ... } ⇒ nil

Yields:

  • (record)

Returns:

  • (nil)

See Also:

  • Resource.fetch_each


106
107
108
109
110
111
# File 'lib/balanced/pager.rb', line 106

def fetch_each
  return enum_for :fetch_each unless block_given?
  begin
    each { |record| yield record }
  end while self.next
end

#firstObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/balanced/pager.rb', line 33

def first
  load! unless @page
  if items.first.nil?
    nil
  else
    envelope = {
      :meta => @page[:meta],
      :links => @page[:links],
      @resource_class.collection_name.to_sym => [items.first]
    }
    resource_class.construct_from_response(envelope)
  end
end

#itemsObject



64
65
66
67
68
69
70
71
# File 'lib/balanced/pager.rb', line 64

def items
  load! unless @page
  if @resource_class.nil?
    []
  else
    @page[@resource_class.collection_name]
  end
end

#limitObject Also known as: limit_value



52
53
54
55
# File 'lib/balanced/pager.rb', line 52

def limit
  load! unless @page
  @page[:meta][:limit]
end

#load!Array? Also known as: reload

Returns Load (or reload) the pager’s collection from the original, supplied options.

Returns:

  • (Array, nil)

    Load (or reload) the pager’s collection from the original, supplied options.



139
140
141
# File 'lib/balanced/pager.rb', line 139

def load!
  load_from @href, @options
end

#nextArray?

Returns Refreshes the pager’s collection of records with the next page.

Returns:

  • (Array, nil)

    Refreshes the pager’s collection of records with the next page.



115
116
117
118
119
# File 'lib/balanced/pager.rb', line 115

def next
  load! unless @page
  next_uri = @page[:meta][:next]
  load_from next_uri, nil unless next_uri.nil?
end

#num_pagesObject



77
78
79
80
81
# File 'lib/balanced/pager.rb', line 77

def num_pages
  num = total / limit
  num += 1 if total % limit > 0
  num
end

#offsetObject Also known as: offset_value



58
59
60
61
# File 'lib/balanced/pager.rb', line 58

def offset
  load! unless @page
  @page[:meta][:offset]
end

#paginate(options = {}) ⇒ Pager Also known as: scoped, where

Returns Duplicates the pager, updating it with the options supplied. Useful for resource scopes.

Returns:

  • (Pager)

    Duplicates the pager, updating it with the options supplied. Useful for resource scopes.

See Also:



147
148
149
150
151
152
# File 'lib/balanced/pager.rb', line 147

def paginate(options = {})
  dup.instance_eval {
    @page = nil
    @options.update options and self
  }
end

#prevArray?

Returns Refreshes the pager’s collection of records with the previous page.

Returns:

  • (Array, nil)

    Refreshes the pager’s collection of records with the previous page.



123
124
125
126
127
# File 'lib/balanced/pager.rb', line 123

def prev
  load! unless @page
  prev_uri = @page[:meta][:prev]
  load_from prev_uri, nil unless prev_uri.nil?
end

#resource_classObject



27
28
29
30
31
# File 'lib/balanced/pager.rb', line 27

def resource_class
  return @resource_class unless @resource_class.nil?
  load! unless @page
  @resource_class
end

#startArray?

Returns Refreshes the pager’s collection of records with the first page.

Returns:

  • (Array, nil)

    Refreshes the pager’s collection of records with the first page.



131
132
133
134
135
# File 'lib/balanced/pager.rb', line 131

def start
  load! unless @page
  first_page = @page[:meta][:first]
  load_from first_page, nil unless first_page.nil?
end

#totalObject



47
48
49
50
# File 'lib/balanced/pager.rb', line 47

def total
  load! unless @page
  @page[:meta][:total]
end