Sha256: e0b97f2592bc84ae97662001ec9d701980d7bb2c4e616ac23203bc04da2e1e53

Contents?: true

Size: 998 Bytes

Versions: 5

Compression:

Stored size: 998 Bytes

Contents

module Nanoc::Extra

  # A FileProxy is a proxy for a File object. It is used to prevent a File
  # object from being created until it is actually necessary.
  #
  # For example, a site with a few thousand pages would fail to compile
  # because the massive amount of file descriptors necessary, but the file
  # proxy will make sure the File object is not created until it is used.
  class FileProxy

    instance_methods.each { |m| undef_method m unless m =~ /^__/ }

    # Creates a new file proxy for the given path. This is similar to
    # creating a File object with the same path, except that the File object
    # will not be created until it is accessed.
    def initialize(path)
      @path = path
    end

    # Makes sure all method calls are relayed to a File object, which will
    # be created right before the method call takes place and destroyed
    # right after.
    def method_missing(sym, *args, &block)
      File.new(@path).__send__(sym, *args, &block)
    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
nanoc-2.1 lib/nanoc/extra/file_proxy.rb
nanoc-2.1.1 lib/nanoc/extra/file_proxy.rb
nanoc-2.1.2 lib/nanoc/extra/file_proxy.rb
nanoc-2.1.3 lib/nanoc/extra/file_proxy.rb
nanoc-2.1.4 lib/nanoc/extra/file_proxy.rb