Class: Workspace::File

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/workspace/file.rb', line 5

def path
  @path
end

#workspaceObject

Returns the value of attribute workspace.



5
6
7
# File 'lib/workspace/file.rb', line 5

def workspace
  @workspace
end

Instance Method Details

#absolute_pathObject



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

#basenameObject



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

#deleteObject



109
110
111
# File 'lib/workspace/file.rb', line 109

def delete
  FileUtils.rm_f(to_s)
end

#dirObject



64
65
66
# File 'lib/workspace/file.rb', line 64

def dir
  Workspace::Dir.new(@workspace, ::File.dirname(@path))
end

#exists?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/workspace/file.rb', line 68

def exists?
  ::File.exist?(to_s)
end

#extensionObject



28
29
30
# File 'lib/workspace/file.rb', line 28

def extension
  ::File.extname(to_s).gsub(/^\./, "")
end

#mimetypeObject



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

#nameObject



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

#readObject



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

#sizeObject



37
38
39
# File 'lib/workspace/file.rb', line 37

def size
  ::File.size(to_s)
end

#stream(args = "r") {|io| ... } ⇒ Object

Yields:

  • (io)


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_sObject



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