Class: Essential::Resource::PaginatorProxy
- Inherits:
-
Object
- Object
- Essential::Resource::PaginatorProxy
- Includes:
- Enumerable, Create
- Defined in:
- lib/essential/resource/paginator_proxy.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#pages ⇒ Object
readonly
Returns the value of attribute pages.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#proxied_class ⇒ Object
readonly
Returns the value of attribute proxied_class.
-
#total ⇒ Object
(also: #size, #count)
readonly
Returns the value of attribute total.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #[](i) ⇒ Object
- #as_json ⇒ Object
- #each ⇒ Object
-
#initialize(proxied_class, url: nil, params: {}, headers: {}, attrs: {}) ⇒ PaginatorProxy
constructor
A new instance of PaginatorProxy.
- #inspect ⇒ Object
- #last ⇒ Object
- #refresh ⇒ Object
Methods included from Create
Constructor Details
#initialize(proxied_class, url: nil, params: {}, headers: {}, attrs: {}) ⇒ PaginatorProxy
Returns a new instance of PaginatorProxy.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/essential/resource/paginator_proxy.rb', line 13 def initialize(proxied_class, url: nil, params: {}, headers: {}, attrs: {}) @fetched_pages = {} @proxied_class = proxied_class @url = url || @proxied_class.url @params = params || {} @headers = headers || {} @attrs = attrs || {} @total = 0 @pages = 0 @per_page = 0 self.refresh end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
111 112 113 |
# File 'lib/essential/resource/paginator_proxy.rb', line 111 def method_missing(method, *args, &block) @proxied_class.send(method, *args, &block) end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
8 9 10 |
# File 'lib/essential/resource/paginator_proxy.rb', line 8 def headers @headers end |
#pages ⇒ Object (readonly)
Returns the value of attribute pages.
9 10 11 |
# File 'lib/essential/resource/paginator_proxy.rb', line 9 def pages @pages end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
8 9 10 |
# File 'lib/essential/resource/paginator_proxy.rb', line 8 def params @params end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
9 10 11 |
# File 'lib/essential/resource/paginator_proxy.rb', line 9 def per_page @per_page end |
#proxied_class ⇒ Object (readonly)
Returns the value of attribute proxied_class.
7 8 9 |
# File 'lib/essential/resource/paginator_proxy.rb', line 7 def proxied_class @proxied_class end |
#total ⇒ Object (readonly) Also known as: size, count
Returns the value of attribute total.
9 10 11 |
# File 'lib/essential/resource/paginator_proxy.rb', line 9 def total @total end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
7 8 9 |
# File 'lib/essential/resource/paginator_proxy.rb', line 7 def url @url end |
Instance Method Details
#[](i) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/essential/resource/paginator_proxy.rb', line 28 def [](i) i = i.to_i # support reverse wrap i += @total if i < 0 return nil if i >= @total || i < 0 page = (i / @per_page) + 1 index = i % @per_page fetch_page(page) unless @fetched_pages.key?(page) @fetched_pages[page][index] end |
#as_json ⇒ Object
60 61 62 |
# File 'lib/essential/resource/paginator_proxy.rb', line 60 def as_json self.map(&:as_json) end |
#each ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/essential/resource/paginator_proxy.rb', line 47 def each return enum_for(:each) unless block_given? (0...self.total).each do |idx| yield self[idx] end end |
#inspect ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/essential/resource/paginator_proxy.rb', line 64 def inspect innards = [:proxied_class, :total, :per_page, :headers, :params].map do |m| attr = self.send(m) if attr format(' @%s=%s', m, attr.inspect) else nil end end format( "#<%s:0x%s\n%s\n>", self.class.name, (self.object_id << 1).to_s(16), innards.compact.join(",\n") ) end |
#last ⇒ Object
43 44 45 |
# File 'lib/essential/resource/paginator_proxy.rb', line 43 def last self[-1] end |
#refresh ⇒ Object
55 56 57 58 |
# File 'lib/essential/resource/paginator_proxy.rb', line 55 def refresh @fetched_pages.clear fetch_page(nil) end |