Sha256: a61d255ef0c94f936c755caba75b2640bbe91bb0251b5c7338643ebfde1b420a

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

require 'fileutils'

module BuildMaster
class CottaFile
  attr_reader :system, :path

  def initialize(system, path)
    @system = system
    @path = path
  end
  
  def cotta
    return Cotta.new(system)
  end
  
  def name
    return @path.basename.to_s
  end
  
  def parent
    return CottaDir.new(@system, @path.parent)
  end
  
  def extname
    return @path.extname
  end
  
  def basename
    return @path.basename(extname).to_s
  end
  
  def exists?
    return @system.file_exists?(@path)
  end
  
  def copy_to(target_file)
    target_file.parent.mkdirs
    @system.copy(path, target_file.path)
  end
  
  def move_to(target_file)
    target_file.parent.mkdirs
    @system.move(path, target_file.path)
  end
  
  def save(content = '')
    write {|file| file.printf content.to_s}
  end
  
  def write(&block)
    parent.mkdirs
    open('w', &block)
  end
  
  def load
    content = nil
    read {|file| content = file.read}
    return content
  end
  
  def read(&block)
    open('r', &block)
  end
  
  def foreach()
    open('r') do |file|
      file.each {|line| yield line}
    end
  end
  
  def delete
    @system.delete_file(@path)
  end
  
  def ==(other)
    return false unless other.kind_of? CottaFile
    return @system == other.system && @path == other.path
  end
  
  def to_s
    return "#{super}-#@system-#@path"
  end
  
  private
  
  def open(*args)
    result = f = @system.io(@path, *args)
    if block_given?
      begin
        result = yield f
      ensure
        f.close
      end
    end
    return result
  end
  
end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
BuildMaster-0.9.0 lib/buildmaster/cotta/cotta_file.rb