Class: UnderOs::File

Inherits:
Object show all
Defined in:
lib/under_os/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, mode = "r") {|_self| ... } ⇒ File

Returns a new instance of File.

Yields:

  • (_self)

Yield Parameters:

  • _self (UnderOs::File)

    the object that the method was called on



44
45
46
47
48
49
# 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

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



2
3
4
# File 'lib/under_os/file.rb', line 2

def mode
  @mode
end

#pathObject (readonly)

Returns the value of attribute path.



2
3
4
# File 'lib/under_os/file.rb', line 2

def path
  @path
end

Class Method Details

.delete(path) ⇒ Object



20
21
22
# File 'lib/under_os/file.rb', line 20

def self.delete(path)
  NSFileManager.defaultManager.removeItemAtPath(UnderOs::File.path(path), error:nil)
end

.dir?(path) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/under_os/file.rb', line 40

def self.dir?(path)
  exists?(path, true)
end

.exists?(path, is_dir = nil) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# 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) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/under_os/file.rb', line 36

def self.file?(path)
  exists?(path, false)
end

.open(path, *args, &block) ⇒ Object



4
5
6
# File 'lib/under_os/file.rb', line 4

def self.open(path, *args, &block)
  new path, *args, &block
end

.path(path) ⇒ Object



24
25
26
# File 'lib/under_os/file.rb', line 24

def self.path(path)
  path.to_s[0] == '/' ? path : NSHomeDirectory().stringByAppendingPathComponent(path)
end

.read(path, *args) ⇒ Object



8
9
10
# File 'lib/under_os/file.rb', line 8

def self.read(path, *args)
  new(path, *args).read
end

.size(path) ⇒ Object



16
17
18
# File 'lib/under_os/file.rb', line 16

def self.size(path)
  new(path).size
end

.tmp(filename, mode = "w", &block) ⇒ Object



28
29
30
# 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) ⇒ Object



12
13
14
# File 'lib/under_os/file.rb', line 12

def self.write(path, content)
  new(path, "w"){|f| f.write(content) }
end

Instance Method Details

#closeObject



89
90
91
92
# File 'lib/under_os/file.rb', line 89

def close
  @handle.closeFile
  @handle = nil
end

#openObject



79
80
81
82
83
84
85
86
87
# 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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# 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

#rewindObject



98
99
100
# File 'lib/under_os/file.rb', line 98

def rewind
  seek 0
end

#seek(offset) ⇒ Object



94
95
96
# File 'lib/under_os/file.rb', line 94

def seek(offset)
  @handle.seekToFileOffset(offset)
end

#sizeObject



102
103
104
# File 'lib/under_os/file.rb', line 102

def size
  NSFileManager.defaultManager.attributesOfItemAtPath(@path, error: nil).fileSize
end

#truncate(size) ⇒ Object



106
107
108
# File 'lib/under_os/file.rb', line 106

def truncate(size)
  @handle.truncateFileAtOffset(size)
end

#urlObject



51
52
53
# File 'lib/under_os/file.rb', line 51

def url
  @url ||= NSURL.fileURLWithPath(@path)
end

#write(content) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# 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