Class: Essential::Resource::PaginatorProxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Create
Defined in:
lib/essential/resource/paginator_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Create

#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

#headersObject (readonly)

Returns the value of attribute headers.



8
9
10
# File 'lib/essential/resource/paginator_proxy.rb', line 8

def headers
  @headers
end

#pagesObject (readonly)

Returns the value of attribute pages.



9
10
11
# File 'lib/essential/resource/paginator_proxy.rb', line 9

def pages
  @pages
end

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/essential/resource/paginator_proxy.rb', line 8

def params
  @params
end

#per_pageObject (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_classObject (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

#totalObject (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

#urlObject (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_jsonObject



60
61
62
# File 'lib/essential/resource/paginator_proxy.rb', line 60

def as_json
  self.map(&:as_json)
end

#eachObject



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

#inspectObject



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

#lastObject



43
44
45
# File 'lib/essential/resource/paginator_proxy.rb', line 43

def last
  self[-1]
end

#refreshObject



55
56
57
58
# File 'lib/essential/resource/paginator_proxy.rb', line 55

def refresh
  @fetched_pages.clear
  fetch_page(nil)
end