Class: Finix::Pagination

Inherits:
Object
  • Object
show all
Includes:
Enumerable, HalResource
Defined in:
lib/finix/pagination.rb

Instance Attribute Summary collapse

Attributes included from HalResource

#attributes, #hyperlinks

Instance Method Summary collapse

Methods included from HalResource

#method_missing

Constructor Details

#initialize(href, opts = {}) ⇒ Pagination

Returns a new instance of Pagination.



12
13
14
15
16
17
18
# File 'lib/finix/pagination.rb', line 12

def initialize(href, opts={})
  @hyperlinks = {}
  @attributes = {}

  @hyperlinks[:self] = href
  @resource_class = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Finix::HalResource

Instance Attribute Details

#resource_classObject

Returns the value of attribute resource_class.



10
11
12
# File 'lib/finix/pagination.rb', line 10

def resource_class
  @resource_class
end

Instance Method Details

#create(attrs = {}) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/finix/pagination.rb', line 48

def create(attrs={})
  attrs = attrs.attributes if attrs.is_a?(Finix::Resource)
  attrs = Finix::Utils.indifferent_read_access attrs
  href = @hyperlinks[:self]
  @resource_class = Finix.from_hypermedia_registry href, attrs
  @resource_class.new(attrs, href).save
end

#currentObject



29
30
31
32
# File 'lib/finix/pagination.rb', line 29

def current
  refresh unless items
  items
end

#eachObject



20
21
22
23
24
25
26
27
# File 'lib/finix/pagination.rb', line 20

def each
  return enum_for :each unless block_given?
  current
  loop do
    items.each { |item| yield item }
    fetch :next
  end
end

#fetch(scope) ⇒ Object

:next, :last, :first, :prev, :self

Raises:

  • (StopIteration)


34
35
36
37
38
39
40
41
42
# File 'lib/finix/pagination.rb', line 34

def fetch(scope) # :next, :last, :first, :prev, :self
  scope = scope.to_s.to_sym
  if @hyperlinks[scope]
    load_from @hyperlinks[scope]
    return self.items
  end

  raise StopIteration
end

#refreshObject



44
45
46
# File 'lib/finix/pagination.rb', line 44

def refresh
  fetch :self
end