Class: Recurly::Resource::Pager
- Inherits:
-
Object
- Object
- Recurly::Resource::Pager
- Includes:
- Enumerable
- Defined in:
- lib/recurly/resource/pager.rb
Overview
Instance Attribute Summary collapse
-
#etag ⇒ String?
readonly
An ETag for the current page.
-
#links ⇒ Hash?
readonly
A hash of links to which the pager can page.
-
#resource_class ⇒ Resource
readonly
The resource class of the pager.
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
-
#count ⇒ Integer
The total record count of the resource in question.
-
#create(attributes = {}) ⇒ Resource
Instantiates and saves a record in the scope of the pager.
-
#create!(attributes = {}) ⇒ Resource
Instantiates and saves a record in the scope of the pager.
-
#each {|record| ... } ⇒ Array
Iterates through the current page of records.
- #find(uuid) ⇒ Object
- #find_each {|record| ... } ⇒ nil
-
#initialize(resource_class, options = {}) ⇒ Pager
constructor
A pager for paginating through resource records.
-
#load! ⇒ Array?
(also: #reload)
Load (or reload) the pager’s collection from the original, supplied options.
-
#new(attributes = {}) {|record| ... } ⇒ Resource
Instantiates a new record in the scope of the pager.
-
#next ⇒ Array?
Refreshes the pager’s collection of records with the next page.
-
#paginate(options = {}) ⇒ Pager
(also: #scoped, #where)
Duplicates the pager, updating it with the options supplied.
- #respond_to?(method_name, include_private = false) ⇒ true, false
-
#start ⇒ Array?
Refreshes the pager’s collection of records with the first page.
-
#uri ⇒ String
The URI of the paginated resource.
Constructor Details
#initialize(resource_class, options = {}) ⇒ Pager
A pager for paginating through resource records.
47 48 49 50 51 52 53 54 |
# File 'lib/recurly/resource/pager.rb', line 47 def initialize resource_class, = {} [:cursor] &&= [:cursor].to_i @parent = .delete :parent @uri = .delete :uri @etag = .delete :etag @resource_class, @options = resource_class, @collection = @count = nil end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
207 208 209 210 211 212 213 214 215 216 |
# File 'lib/recurly/resource/pager.rb', line 207 def method_missing name, *args, &block scope = resource_class.scopes[name] and return paginate scope if [].respond_to? name load! unless defined? @collection return @collection.send name, *args, &block end super end |
Instance Attribute Details
#etag ⇒ String? (readonly)
Returns An ETag for the current page.
31 32 33 |
# File 'lib/recurly/resource/pager.rb', line 31 def etag @etag end |
#links ⇒ Hash? (readonly)
Returns A hash of links to which the pager can page.
28 29 30 |
# File 'lib/recurly/resource/pager.rb', line 28 def links @links end |
#resource_class ⇒ Resource (readonly)
Returns The resource class of the pager.
25 26 27 |
# File 'lib/recurly/resource/pager.rb', line 25 def resource_class @resource_class end |
Instance Method Details
#all(options = {}) ⇒ Object
118 119 120 |
# File 'lib/recurly/resource/pager.rb', line 118 def all = {} paginate().to_a end |
#count ⇒ Integer
Returns The total record count of the resource in question.
63 64 65 |
# File 'lib/recurly/resource/pager.rb', line 63 def count @count ||= API.head(uri, @options)['X-Records'].to_i end |
#create(attributes = {}) ⇒ Resource
Instantiates and saves a record in the scope of the pager.
146 147 148 |
# File 'lib/recurly/resource/pager.rb', line 146 def create attributes = {} new(attributes) { |record| record.save } end |
#create!(attributes = {}) ⇒ Resource
Instantiates and saves a record in the scope of the pager.
159 160 161 |
# File 'lib/recurly/resource/pager.rb', line 159 def create! attributes = {} new(attributes) { |record| record.save! } end |
#each {|record| ... } ⇒ Array
Returns Iterates through the current page of records.
69 70 71 72 73 |
# File 'lib/recurly/resource/pager.rb', line 69 def each return enum_for :each unless block_given? load! unless @collection @collection.each { |record| yield record } end |
#find(uuid) ⇒ Object
163 164 165 166 167 168 169 170 |
# File 'lib/recurly/resource/pager.rb', line 163 def find uuid if resource_class.respond_to? :find raise NoMethodError, "#find must be called on #{resource_class} directly" end resource_class.from_response API.get("#{uri}/#{uuid}") end |
#find_each {|record| ... } ⇒ nil
78 79 80 81 82 83 |
# File 'lib/recurly/resource/pager.rb', line 78 def find_each return enum_for :find_each unless block_given? begin each { |record| yield record } end while self.next end |
#load! ⇒ Array? Also known as: reload
Returns Load (or reload) the pager’s collection from the original, supplied options.
99 100 101 |
# File 'lib/recurly/resource/pager.rb', line 99 def load! load_from uri, @options end |
#new(attributes = {}) {|record| ... } ⇒ Resource
Instantiates a new record in the scope of the pager.
129 130 131 132 133 134 135 136 |
# File 'lib/recurly/resource/pager.rb', line 129 def new attributes = {} record = resource_class.send(:new, attributes) { |r| r.attributes[@parent.class.member_name] ||= @parent if @parent r.uri = uri } yield record if block_given? record end |
#next ⇒ Array?
Returns Refreshes the pager’s collection of records with the next page.
87 88 89 |
# File 'lib/recurly/resource/pager.rb', line 87 def next load_from links['next'], nil if links.key? 'next' end |
#paginate(options = {}) ⇒ Pager Also known as: scoped, where
Returns Duplicates the pager, updating it with the options supplied. Useful for resource scopes.
109 110 111 112 113 114 |
# File 'lib/recurly/resource/pager.rb', line 109 def paginate = {} dup.instance_eval { @collection = @count = @etag = nil @options.update and self } end |
#respond_to?(method_name, include_private = false) ⇒ true, false
174 175 176 |
# File 'lib/recurly/resource/pager.rb', line 174 def respond_to? method_name, include_private = false super || [].respond_to?(method_name, include_private) end |
#start ⇒ Array?
Returns Refreshes the pager’s collection of records with the first page.
93 94 95 |
# File 'lib/recurly/resource/pager.rb', line 93 def start load_from links['start'], nil if links.key? 'start' end |
#uri ⇒ String
Returns The URI of the paginated resource.
57 58 59 |
# File 'lib/recurly/resource/pager.rb', line 57 def uri @uri ||= resource_class.collection_path end |