Sha256: d8de360ac7b44dd944c085fb6c23fe08ca51e664f3162d91f70347ebe55d5577

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

module Resync
  # Lazily retrieves and caches the zip packages for the specified
  # list of resources. The packages are cached to temporary files
  # which are deleted on exit; if they are deleted while the
  # interpreter is running, the behavior is undefined (but bad things
  # are likely to happen).
  class ZipPackages
    include Enumerable

    # Creates a new {ZipPackages} wrapper for the specified list
    # of resources.
    # @param resources [Array<Resource>] The list of resources to
    #   get zip packages for.
    def initialize(resources)
      @resources = resources
      @packages = {}
    end

    # Gets the size of this list of packages.
    # @return the size of the underlying array.
    def size
      @resources.size
    end

    # Gets the zip package at the specified index, downloading it
    # if necessary.
    #
    # @return [ZipPackage] the zip package for the resource at the
    #   specified index in the underlying array.
    def [](key)
      resource = @resources[key]
      package_for(resource)
    end

    # Gets the zip package for the specified resource, downloading it
    # if necessary.
    # @return [ZipPackage] the package for the resource
    def package_for(resource)
      @packages[resource] ||= resource.zip_package
    end

    # Lazily iterates the given block for each zip package, downloading
    # as necessary.
    # @param &block [Block] The block to iterate
    def each
      @resources.lazy.each do |resource|
        yield package_for(resource)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
resync-client-0.1.2 lib/resync/client/zip_packages.rb