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



49
50
51
# File 'lib/workspace/file.rb', line 49

def absolute_path
  ::File.absolute_path(to_s)
end

#basenameObject



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

def basename
  ::File.basename(path, ".*")
end

#copy(target_file) ⇒ Object



82
83
84
85
# File 'lib/workspace/file.rb', line 82

def copy(target_file)
  target_file.dir.create unless target_file.dir.exists?
  FileUtils.cp(to_s, target_file.to_s)
end

#deleteObject



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

def delete
  FileUtils.rm_f(to_s)
end

#dirObject



53
54
55
# File 'lib/workspace/file.rb', line 53

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

#exists?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/workspace/file.rb', line 57

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

#extensionObject



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

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

#mimetypeObject



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

def mimetype
  type = MIME::Types.of(name).first
  type ? type.to_s : nil
end

#move(target_file) ⇒ Object



92
93
94
95
96
# File 'lib/workspace/file.rb', line 92

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
# File 'lib/workspace/file.rb', line 16

def name
  "#{basename}.#{extension}"
end

#readObject



61
62
63
# File 'lib/workspace/file.rb', line 61

def read
  @contents ||= ::File.open(to_s).read
end

#relative_path(relative_dir = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/workspace/file.rb', line 37

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)
    result = second.relative_path_from(first).to_s
    result
  else
    @path.gsub(%r{^/}, "")
  end
end

#rename(filename) ⇒ Object



87
88
89
90
# File 'lib/workspace/file.rb', line 87

def rename(filename)
  FileUtils.mv(to_s, dir.file(filename).to_s) if exists?
  @path = dir.file(filename).path
end

#replace(key, value) ⇒ Object



70
71
72
73
# File 'lib/workspace/file.rb', line 70

def replace(key, value)
  read.gsub!(key, value)
  self
end

#set(data) ⇒ Object



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

def set(data)
  @contents = data
  self
end

#sizeObject



33
34
35
# File 'lib/workspace/file.rb', line 33

def size
  ::File.size(to_s)
end

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

Yields:

  • (io)


102
103
104
105
106
# File 'lib/workspace/file.rb', line 102

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



75
76
77
78
79
80
# File 'lib/workspace/file.rb', line 75

def write(data = nil)
  data ||= @contents
  dir.create unless dir.exists?
  ::File.open(to_s, "wb") { |file| file << data }
  self
end