Class: BaseCRM::PaginatedResource

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/basecrm/paginated_resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, *args) ⇒ PaginatedResource

Returns a new instance of PaginatedResource.

Raises:

  • (TypeError)


7
8
9
10
11
12
13
# File 'lib/basecrm/paginated_resource.rb', line 7

def initialize(service, *args)
  raise TypeError unless service.respond_to?(:where)

  @service = service
  @args = args
  @options = args.last.kind_of?(Hash) ? args.pop : {}
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



5
6
7
# File 'lib/basecrm/paginated_resource.rb', line 5

def current_page
  @current_page
end

Instance Method Details

#each(&block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/basecrm/paginated_resource.rb', line 15

def each(&block)
  return to_enum(:each) unless block_given?

  current_page = 1
  per_page = 25

  search_options = @options.merge(page: current_page, per_page: per_page)

  until (resources = @service.where(*@args, search_options)).empty?
    resources.each(&block)
    current_page += 1
    search_options[:page] = current_page
  end

  self
end