Class: BaseCRM::PaginatedResource
- Inherits:
-
Object
- Object
- BaseCRM::PaginatedResource
- Includes:
- Enumerable
- Defined in:
- lib/basecrm/paginated_resource.rb
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#initialize(service, *args) ⇒ PaginatedResource
constructor
A new instance of PaginatedResource.
Constructor Details
#initialize(service, *args) ⇒ PaginatedResource
Returns a new instance of PaginatedResource.
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_page ⇒ Object (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 = @options.merge(page: current_page, per_page: per_page) until (resources = @service.where(*@args, )).empty? resources.each(&block) current_page += 1 [:page] = current_page end self end |