Class: Workspace::File
- Inherits:
-
Object
- Object
- Workspace::File
- Defined in:
- lib/workspace/file.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
-
#workspace ⇒ Object
Returns the value of attribute workspace.
Instance Method Summary collapse
- #absolute_path ⇒ Object
- #align_encoding! ⇒ Object
- #basename ⇒ Object
- #copy(target_file) ⇒ Object
- #delete ⇒ Object
- #dir ⇒ Object
- #exists? ⇒ Boolean
- #extension ⇒ Object
-
#initialize(workspace, path) ⇒ File
constructor
A new instance of File.
- #mimetype ⇒ Object
- #move(target_file) ⇒ Object
- #name ⇒ Object
- #read ⇒ Object
- #relative_path(relative_dir = nil) ⇒ Object
- #rename(filename) ⇒ Object
- #replace(key, value) ⇒ Object
- #set(data) ⇒ Object
- #size ⇒ Object
- #stream(args = "r") {|io| ... } ⇒ Object
- #to_s ⇒ Object
- #write(data = nil) ⇒ Object
Constructor Details
#initialize(workspace, path) ⇒ File
Returns a new instance of File.
7 8 9 10 |
# File 'lib/workspace/file.rb', line 7 def initialize(workspace, path) @workspace = workspace @path = path end |
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
5 6 7 |
# File 'lib/workspace/file.rb', line 5 def path @path end |
#workspace ⇒ Object
Returns the value of attribute workspace.
5 6 7 |
# File 'lib/workspace/file.rb', line 5 def workspace @workspace end |
Instance Method Details
#absolute_path ⇒ Object
60 61 62 |
# File 'lib/workspace/file.rb', line 60 def absolute_path ::File.absolute_path(to_s) end |
#align_encoding! ⇒ Object
41 42 43 44 45 46 47 |
# 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 ⇒ Object
24 25 26 |
# File 'lib/workspace/file.rb', line 24 def basename ::File.basename(path, ".*") end |
#copy(target_file) ⇒ Object
93 94 95 96 |
# 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 ⇒ Object
109 110 111 |
# File 'lib/workspace/file.rb', line 109 def delete FileUtils.rm_f(to_s) end |
#dir ⇒ Object
64 65 66 |
# File 'lib/workspace/file.rb', line 64 def dir Workspace::Dir.new(@workspace, ::File.dirname(@path)) end |
#exists? ⇒ Boolean
68 69 70 |
# File 'lib/workspace/file.rb', line 68 def exists? ::File.exist?(to_s) end |
#extension ⇒ Object
28 29 30 |
# File 'lib/workspace/file.rb', line 28 def extension ::File.extname(to_s).gsub(/^\./, "") end |
#mimetype ⇒ Object
32 33 34 35 |
# File 'lib/workspace/file.rb', line 32 def mimetype type = MIME::Types.of(name).first type ? type.to_s : nil end |
#move(target_file) ⇒ Object
103 104 105 106 107 |
# 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 ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/workspace/file.rb', line 16 def name if extension.nil? || extension.empty? basename else "#{basename}.#{extension}" end end |
#read ⇒ Object
72 73 74 |
# File 'lib/workspace/file.rb', line 72 def read @contents ||= ::File.open(to_s).read end |
#relative_path(relative_dir = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# 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) ⇒ Object
98 99 100 101 |
# 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) ⇒ Object
81 82 83 84 |
# File 'lib/workspace/file.rb', line 81 def replace(key, value) read.gsub!(key, value) self end |
#set(data) ⇒ Object
76 77 78 79 |
# File 'lib/workspace/file.rb', line 76 def set(data) @contents = data self end |
#size ⇒ Object
37 38 39 |
# File 'lib/workspace/file.rb', line 37 def size ::File.size(to_s) end |
#stream(args = "r") {|io| ... } ⇒ Object
113 114 115 116 117 |
# 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 ⇒ Object
12 13 14 |
# File 'lib/workspace/file.rb', line 12 def to_s ::File.join(@workspace, @path) end |
#write(data = nil) ⇒ Object
86 87 88 89 90 91 |
# 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 |