Class: UnderOs::File
- Inherits:
-
Object
- Object
- UnderOs::File
- Defined in:
- lib/under_os/file.rb
Instance Attribute Summary collapse
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .delete(path) ⇒ Object
- .dir?(path) ⇒ Boolean
- .exists?(path, is_dir = nil) ⇒ Boolean
- .file?(path) ⇒ Boolean
- .open(path, *args, &block) ⇒ Object
- .path(path) ⇒ Object
- .read(path, *args) ⇒ Object
- .size(path) ⇒ Object
- .tmp(filename, mode = "w", &block) ⇒ Object
- .write(path, content) ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(path, mode = "r") {|_self| ... } ⇒ File
constructor
A new instance of File.
- #open ⇒ Object
- #read(size = nil) ⇒ Object
- #rewind ⇒ Object
- #seek(offset) ⇒ Object
- #size ⇒ Object
- #truncate(size) ⇒ Object
- #url ⇒ Object
- #write(content) ⇒ Object
Constructor Details
Instance Attribute Details
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
2 3 4 |
# File 'lib/under_os/file.rb', line 2 def mode @mode end |
#path ⇒ Object (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
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
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
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
#close ⇒ Object
89 90 91 92 |
# File 'lib/under_os/file.rb', line 89 def close @handle.closeFile @handle = nil end |
#open ⇒ Object
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 |
#rewind ⇒ Object
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 |
#size ⇒ Object
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 |
#url ⇒ Object
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.dataUsingEncoding(NSUTF8StringEncoding) if content.is_a?(String) open @handle.writeData content close end |