Sha256: f3dbffff8e54246d878e8615065cb957c700060f4e97a4ac81ad57f9acaec264

Contents?: true

Size: 1.22 KB

Versions: 10

Compression:

Stored size: 1.22 KB

Contents

module Clever
  # An interface for querying nested resources
  class NestedResource
    include Clever::APIOperations::NestedList
    include Enumerable

    # Create a nested resource
    # @api private
    # @return [Clever::APIOperations::NestedList]
    def initialize(uri, filters = {}, headers = {})
      @uri = uri
      @filters = filters
      @headers = headers
      @results_list = Clever::APIOperations::PageList.new(uri, filters, headers).to_results_list
    end

    # Query and iterate over results for the params provided during initialization
    # @api public
    # @return [Clever::APIOperations::NestedResource] self
    # @example
    #   district = Clever::Districts.retrieve id
    #   district.schools.each { |school| puts school.name }
    def each(&blk)
      @results_list.each(&blk)
      self
    end

    # Query for all results and count them
    # @api public
    # @note This queries for the actual elements! If you wish to just do a
    #   count query, use the count method
    # @return [Integer]
    # @example
    #   district = Clever::District.retrieve id
    #   num_schools_for_district = district.schools.length
    def length
      @results_list.count
    end
    alias_method :size, :length
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
clever-ruby-0.14.0 lib/clever-ruby/nested_resource.rb
clever-ruby-0.13.2 lib/clever-ruby/nested_resource.rb
clever-ruby-0.13.1 lib/clever-ruby/nested_resource.rb
clever-ruby-0.13.0 lib/clever-ruby/nested_resource.rb
clever-ruby-0.11.1 lib/clever-ruby/nested_resource.rb
clever-ruby-0.11.0 lib/clever-ruby/nested_resource.rb
clever-ruby-0.10.2 lib/clever-ruby/nested_resource.rb
clever-ruby-0.10.1 lib/clever-ruby/nested_resource.rb
clever-ruby-0.10.0 lib/clever-ruby/nested_resource.rb
clever-ruby-0.9.0 lib/clever-ruby/nested_resource.rb