Sha256: 37638b230ccc643bee0e7e00b73050f6cefeefcd402623e43a86b7b96ffb2eb8

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 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 = {})
      @uri = uri
      @filters = filters
      @results_list = Clever::APIOperations::PageList.new(uri, filters).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

2 entries across 2 versions & 1 rubygems

Version Path
clever-ruby-0.8.0 lib/clever-ruby/nested_resource.rb
clever-ruby-0.7.0 lib/clever-ruby/nested_resource.rb