Class: Resync::Client::Zip::ZipPackages

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/resync/client/zip/zip_packages.rb

Overview

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).

Instance Method Summary collapse

Constructor Details

#initialize(resources) ⇒ ZipPackages

Creates a new Resync::Client::Zip::ZipPackages wrapper for the specified list of resources.

Parameters:

  • resources (Array<Resource>)

    The list of resources to get zip packages for.



20
21
22
23
# File 'lib/resync/client/zip/zip_packages.rb', line 20

def initialize(resources)
  @resources = resources
  @packages = {}
end

Instance Method Details

#[](key) ⇒ ZipPackage

Gets the zip package at the specified index, downloading it if necessary.

Returns:

  • (ZipPackage)

    the zip package for the resource at the specified index in the underlying array.



36
37
38
39
# File 'lib/resync/client/zip/zip_packages.rb', line 36

def [](key)
  resource = @resources[key]
  package_for(resource)
end

#each {|ZipPackage| ... } ⇒ Object

Lazily iterates the given block for each zip package, downloading as necessary.

Yields:

  • (ZipPackage)

    the zip package for each resource



51
52
53
54
55
# File 'lib/resync/client/zip/zip_packages.rb', line 51

def each
  @resources.lazy.each do |resource|
    yield package_for(resource)
  end
end

#forceArray<ZipPackage> Also known as: to_ary

Eagerly gets the zip package for each resource, downloading as necessary. (For compatbility with Enumerator::Lazy#flat_map)

Returns:

  • (Array<ZipPackage>)

    the zip package for each resource



60
61
62
# File 'lib/resync/client/zip/zip_packages.rb', line 60

def force
  @resources.map { |r| package_for(r) }.to_a
end

#package_for(resource) ⇒ ZipPackage

Gets the zip package for the specified resource, downloading it if necessary.

Returns:



44
45
46
# File 'lib/resync/client/zip/zip_packages.rb', line 44

def package_for(resource)
  @packages[resource] ||= resource.zip_package
end

#sizeObject

Gets the size of this list of packages.

Returns:

  • the size of the underlying array.



27
28
29
# File 'lib/resync/client/zip/zip_packages.rb', line 27

def size
  @resources.size
end