class Workspace::File

Attributes

path[RW]
workspace[RW]

Public Class Methods

new(workspace, path) click to toggle source
# File lib/workspace/file.rb, line 7
def initialize(workspace, path)
  @workspace = workspace
  @path = path
end

Public Instance Methods

absolute_path() click to toggle source
# File lib/workspace/file.rb, line 60
def absolute_path
  ::File.absolute_path(to_s)
end
align_encoding!() click to toggle source
# File lib/workspace/file.rb, line 41
def align_encoding!
  match = self.dir.files.find do |f|
    f.name.mb_chars.normalize.to_s == self.name.mb_chars.normalize.to_s
  end
  self.path = match.path if match
  self
end
basename() click to toggle source
# File lib/workspace/file.rb, line 24
def basename
  ::File.basename(path, ".*")
end
copy(target_file) click to toggle source
# File lib/workspace/file.rb, line 93
def copy(target_file)
  target_file.dir.create unless target_file.dir.exists?
  FileUtils.cp(to_s, target_file.to_s)
end
delete() click to toggle source
# File lib/workspace/file.rb, line 109
def delete
  FileUtils.rm_f(to_s)
end
dir() click to toggle source
# File lib/workspace/file.rb, line 64
def dir
  Workspace::Dir.new(@workspace, ::File.dirname(@path))
end
exists?() click to toggle source
# File lib/workspace/file.rb, line 68
def exists?
  ::File.exist?(to_s)
end
extension() click to toggle source
# File lib/workspace/file.rb, line 28
def extension
  ::File.extname(to_s).gsub(/^\./, "")
end
mimetype() click to toggle source
# File lib/workspace/file.rb, line 32
def mimetype
  type = MIME::Types.of(name).first
  type ? type.to_s : nil
end
move(target_file) click to toggle source
# File lib/workspace/file.rb, line 103
def move(target_file)
  target_file.dir.create unless target_file.dir.exists?
  FileUtils.mv(to_s, target_file.to_s)
  target_file
end
name() click to toggle source
# File lib/workspace/file.rb, line 16
def name
  if extension.nil? || extension.empty?
    basename
  else
    "#{basename}.#{extension}"
  end
end
read() click to toggle source
# File lib/workspace/file.rb, line 72
def read
  @contents ||= ::File.open(to_s).read
end
relative_path(relative_dir = nil) click to toggle source
# File lib/workspace/file.rb, line 49
def relative_path(relative_dir = nil)
  if relative_dir
    relative_dir = relative_dir.dir if relative_dir.class == Workspace::File
    first = Pathname.new(relative_dir.path)
    second = Pathname.new(path)
    second.relative_path_from(first).to_s
  else
    @path.gsub(%r{^/}, "")
  end
end
rename(filename) click to toggle source
# File lib/workspace/file.rb, line 98
def rename(filename)
  FileUtils.mv(to_s, dir.file(filename).to_s) if exists?
  @path = dir.file(filename).path
end
replace(key, value) click to toggle source
# File lib/workspace/file.rb, line 81
def replace(key, value)
  read.gsub!(key, value)
  self
end
set(data) click to toggle source
# File lib/workspace/file.rb, line 76
def set(data)
  @contents = data
  self
end
size() click to toggle source
# File lib/workspace/file.rb, line 37
def size
  ::File.size(to_s)
end
stream(args = "r") { |io| ... } click to toggle source
# File lib/workspace/file.rb, line 113
def stream(args = "r", &block)
  io = ::File.open(to_s, args)
  yield io
  io.close
end
to_s() click to toggle source
# File lib/workspace/file.rb, line 12
def to_s
  ::File.join(@workspace, @path)
end
write(data = nil) click to toggle source
# File lib/workspace/file.rb, line 86
def write(data = nil)
  data ||= @contents
  dir.create unless dir.exists?
  ::File.open(to_s, "wb") { |file| file << data }
  self
end