Class: Vfs::File
Instance Attribute Summary
Attributes inherited from Entry
#driver, #path, #path_cache
Instance Method Summary
collapse
Methods inherited from Entry
#==, #created_at, #dir, #dir?, #entry, #eql?, #file, #file?, #get, #hash, #initialize, #inspect, #local?, #name, #parent, #set, #tmp, #updated_at
Constructor Details
This class inherits a constructor from Vfs::Entry
Instance Method Details
#append(*args, &block) ⇒ Object
86
87
88
89
90
|
# File 'lib/vfs/entries/file.rb', line 86
def append *args, &block
options = (args.last.is_a?(Hash) && args.pop) || {}
options[:append] = true
write(*(args << options), &block)
end
|
#basename ⇒ Object
146
147
148
|
# File 'lib/vfs/entries/file.rb', line 146
def basename
::File.basename(name, File.extname(name))
end
|
#copy_to(to, options = {}) ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/vfs/entries/file.rb', line 105
def copy_to to, options = {}
raise Error, "you can't copy to itself" if self == to
target = if to.is_a? File
to
elsif to.is_a? Dir
to.file elsif to.is_a? UniversalEntry
to.file
else
raise "can't copy to unknown Entry!"
end
target.write options do |writer|
read(options){|buff| writer.write buff}
end
target
end
|
#create(options = {}) ⇒ Object
def content options = {}
read options
end
46
47
48
49
|
# File 'lib/vfs/entries/file.rb', line 46
def create options = {}
write '', options
self
end
|
#destroy ⇒ Object
97
98
99
|
# File 'lib/vfs/entries/file.rb', line 97
def destroy
destroy_entry
end
|
#extension ⇒ Object
150
151
152
|
# File 'lib/vfs/entries/file.rb', line 150
def extension
::File.extname(name).sub(/^\./, '')
end
|
#move_to(to) ⇒ Object
125
126
127
128
129
|
# File 'lib/vfs/entries/file.rb', line 125
def move_to to
copy_to to
destroy
to
end
|
#read(options = {}, &block) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/vfs/entries/file.rb', line 12
def read options = {}, &block
options[:bang] = true unless options.include? :bang
driver.open do
begin
if block
driver.read_file path, &block
else
data = ""
driver.read_file(path){|buff| data << buff}
data
end
rescue StandardError => e
raise Vfs::Error, "can't read Dir #{self}!" if dir.exist?
attrs = get
if attrs and attrs[:file]
raise e
elsif attrs and attrs[:dir]
raise Error, "You are trying to read Dir '#{self}' as if it's a File!"
else
if options[:bang]
raise Error, "file #{self} not exist!"
else
block ? block.call('') : ''
end
end
end
end
end
|
#render(*args) ⇒ Object
135
136
137
138
139
140
141
142
|
# File 'lib/vfs/entries/file.rb', line 135
def render *args
require 'tilt'
args.unshift Object.new if args.size == 1 and args.first.is_a?(Hash)
template = Tilt.new(path){read}
template.render *args
end
|
#size ⇒ Object
144
|
# File 'lib/vfs/entries/file.rb', line 144
def size; get :size end
|
#update(options = {}, &block) ⇒ Object
92
93
94
95
|
# File 'lib/vfs/entries/file.rb', line 92
def update options = {}, &block
data = read options
write block.call(data), options
end
|
#write(*args, &block) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/vfs/entries/file.rb', line 51
def write *args, &block
if block
options = args.first || {}
else
data, options = *args
options ||= {}
end
raise "can't do :override and :append at the same time!" if options[:override] and options[:append]
driver.open do
try = 0
begin
try += 1
if block
driver.write_file(path, options[:append], &block)
else
driver.write_file(path, options[:append]){|writer| writer.write data}
end
rescue StandardError => error
parent = self.parent
if entry.exist?
entry.destroy
elsif !parent.exist?
parent.create(options)
else
raise error
end
try < 2 ? retry : raise(error)
end
end
self
end
|