Sha256: 733980e15db00c1126bde632ae22b32e4534c13c51bd925e26afeb900b86fc6a

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require 'fileutils'

class RemoteTable
  class LocalFile #:nodoc:all
    
    attr_reader :t
    
    def initialize(t)
      @t = t
    end
    
    def path
      generate unless generated?
      @path
    end
    
    def encoded_io
      @encoded_io ||= if ::RUBY_VERSION >= '1.9'
        ::File.open path, 'rb', :internal_encoding => t.properties.internal_encoding, :external_encoding => t.properties.external_encoding
      else
        ::File.open path, 'rb'
      end
    end
    
    def cleanup
      if @encoded_io.respond_to?(:closed?) and !@encoded_io.closed?
        @encoded_io.close
      end
      @encoded_io = nil
      if @path and ::File.exist?(@path)
        ::FileUtils.rm_f @path
      end
      @path = nil
      @generated = nil
    end
    
    private
    
    def generated?
      @generated == true
    end
        
    def generate
      tmp_path = Utils.download t.properties.uri, t.properties.form_data
      if compression = t.properties.compression
        tmp_path = Utils.decompress tmp_path, compression
      end
      if packing = t.properties.packing
        tmp_path = Utils.unpack tmp_path, packing
      end
      @path = Utils.pick tmp_path, :filename => t.properties.filename, :glob => t.properties.glob
      @generated = true
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
remote_table-1.3.0 lib/remote_table/local_file.rb