class UnderOs::File

Attributes

mode[R]
path[R]

Public Class Methods

delete(path) click to toggle source
# File lib/under_os/file.rb, line 20
def self.delete(path)
  NSFileManager.defaultManager.removeItemAtPath(UnderOs::File.path(path), error:nil)
end
dir?(path) click to toggle source
# File lib/under_os/file.rb, line 40
def self.dir?(path)
  exists?(path, true)
end
exists?(path, is_dir=nil) click to toggle source
# File lib/under_os/file.rb, line 32
def self.exists?(path, is_dir=nil)
  NSFileManager.defaultManager.fileExistsAtPath(UnderOs::File.path(path), isDirectory:is_dir)
end
file?(path) click to toggle source
# File lib/under_os/file.rb, line 36
def self.file?(path)
  exists?(path, false)
end
new(path, mode="r") { |self| ... } click to toggle source
# File lib/under_os/file.rb, line 44
def initialize(path, mode="r", &block)
  @path = UnderOs::File.path(path)
  @mode = mode

  yield(self) if block_given?
end
open(path, *args, &block) click to toggle source
# File lib/under_os/file.rb, line 4
def self.open(path, *args, &block)
  new path, *args, &block
end
path(path) click to toggle source
# File lib/under_os/file.rb, line 24
def self.path(path)
  path.to_s[0] == '/' ? path : NSHomeDirectory().stringByAppendingPathComponent(path)
end
read(path, *args) click to toggle source
# File lib/under_os/file.rb, line 8
def self.read(path, *args)
  new(path, *args).read
end
size(path) click to toggle source
# File lib/under_os/file.rb, line 16
def self.size(path)
  new(path).size
end
tmp(filename, mode="w", &block) click to toggle source
# File lib/under_os/file.rb, line 28
def self.tmp(filename, mode="w", &block)
  new NSTemporaryDirectory().stringByAppendingPathComponent(filename), mode, &block
end
write(path, content) click to toggle source
# File lib/under_os/file.rb, line 12
def self.write(path, content)
  new(path, "w"){|f| f.write(content) }
end

Public Instance Methods

close() click to toggle source
# File lib/under_os/file.rb, line 89
def close
  @handle.closeFile
  @handle = nil
end
open() click to toggle source
# File lib/under_os/file.rb, line 79
def open
  @handle = if @mode == "r"
    NSFileHandle.fileHandleForReadingAtPath(@path)
  else
    NSFileHandle.fileHandleForWritingAtPath(@path)
  end

  @handle.seekToEndOfFile if ["w+", "a"].include?(@mode)
end
read(size=nil) click to toggle source
# File lib/under_os/file.rb, line 55
def read(size=nil)
  open
  data = if size == nil
    @handle.readDataToEndOfFile
  else
    @handle.readDataOfLength(size)
  end
  close

  @mode == "b" ? data : NSString.alloc.initWithData(data, encoding:NSUTF8StringEncoding)
end
rewind() click to toggle source
# File lib/under_os/file.rb, line 98
def rewind
  seek 0
end
seek(offset) click to toggle source
# File lib/under_os/file.rb, line 94
def seek(offset)
  @handle.seekToFileOffset(offset)
end
size() click to toggle source
# File lib/under_os/file.rb, line 102
def size
  NSFileManager.defaultManager.attributesOfItemAtPath(@path, error: nil).fileSize
end
truncate(size) click to toggle source
# File lib/under_os/file.rb, line 106
def truncate(size)
  @handle.truncateFileAtOffset(size)
end
url() click to toggle source
# File lib/under_os/file.rb, line 51
def url
  @url ||= NSURL.fileURLWithPath(@path)
end
write(content) click to toggle source
# File lib/under_os/file.rb, line 67
def write(content)
  if ! UnderOs::File.exists?(@path)
    NSFileManager.defaultManager.createFileAtPath @path, contents:nil, attributes:nil
  end

  content = content.to_data('utf-8') if content.is_a?(String)

  open
  @handle.writeData content
  close
end