Sha256: 6175f818696be4b7bcb5cb567655a7d02f0378463a2596da80f2c961997d5b5b

Contents?: true

Size: 707 Bytes

Versions: 21

Compression:

Stored size: 707 Bytes

Contents

module BaseCRM
  class PaginatedResource
    include Enumerable

    attr_reader :current_page

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

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

    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
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
basecrm-2.0.0 lib/basecrm/paginated_resource.rb
basecrm-1.3.10 lib/basecrm/paginated_resource.rb
basecrm-1.3.9 lib/basecrm/paginated_resource.rb
basecrm-1.3.8 lib/basecrm/paginated_resource.rb
basecrm-1.3.7 lib/basecrm/paginated_resource.rb
basecrm-1.3.6 lib/basecrm/paginated_resource.rb
basecrm-1.3.5 lib/basecrm/paginated_resource.rb
basecrm-1.3.4 lib/basecrm/paginated_resource.rb
basecrm-1.3.3 lib/basecrm/paginated_resource.rb
basecrm-1.3.2 lib/basecrm/paginated_resource.rb
basecrm-1.3.1 lib/basecrm/paginated_resource.rb
basecrm-1.3.0 lib/basecrm/paginated_resource.rb
basecrm-1.2.3 lib/basecrm/paginated_resource.rb
basecrm-1.2.2 lib/basecrm/paginated_resource.rb
basecrm-1.2.1 lib/basecrm/paginated_resource.rb
basecrm-1.2.0 lib/basecrm/paginated_resource.rb
basecrm-1.1.3 lib/basecrm/paginated_resource.rb
basecrm-1.1.2 lib/basecrm/paginated_resource.rb
basecrm-1.1.1 lib/basecrm/paginated_resource.rb
basecrm-1.1.0 lib/basecrm/paginated_resource.rb