Class: Vfs::File

Inherits:
Entry show all
Defined in:
lib/vfs/entries/file.rb

Instance Attribute Summary

Attributes inherited from Entry

#path, #path_cache, #storage

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



106
107
108
109
110
# File 'lib/vfs/entries/file.rb', line 106

def append *args, &block
  options = (args.last.is_a?(Hash) && args.pop) || {}
  options[:append] = true
  write(*(args << options), &block)
end

#basenameObject



199
200
201
# File 'lib/vfs/entries/file.rb', line 199

def basename
  ::File.basename(name, File.extname(name))
end

#content(options = {}) ⇒ Object



42
43
44
# File 'lib/vfs/entries/file.rb', line 42

def content options = {}
  read options
end

#copy_to(to, options = {}) ⇒ Object

Transfers

Raises:



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/vfs/entries/file.rb', line 150

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 #(name)
  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

#copy_to!(to, options = {}) ⇒ Object



169
170
171
172
# File 'lib/vfs/entries/file.rb', line 169

def copy_to! to, options = {}
  options[:override] = true
  copy_to to, options
end

#create(options = {}) ⇒ Object



46
47
48
49
# File 'lib/vfs/entries/file.rb', line 46

def create options = {}
  write '', options
  self
end

#create!(options = {}) ⇒ Object



50
51
52
53
# File 'lib/vfs/entries/file.rb', line 50

def create! options = {}
  options[:override] = true
  create options
end

#destroy(options = {}) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/vfs/entries/file.rb', line 118

def destroy options = {}
  storage.open do |fs|
    begin
      fs.delete_file path
      self
    rescue StandardError => e
      attrs = get
      if attrs and attrs[:dir]
        if options[:force]
          dir.destroy
        else
          raise Error, "can't destroy Dir #{dir} (you are trying to destroy it as if it's a File)"
        end
      elsif attrs and attrs[:file]
        # unknown internal error
        raise e
      else
        # do nothing, file already not exist
      end
    end
  end
  self
end

#destroy!(options = {}) ⇒ Object



141
142
143
144
# File 'lib/vfs/entries/file.rb', line 141

def destroy! options = {}
  options[:force] = true
  destroy options
end

#extensionObject



203
204
205
# File 'lib/vfs/entries/file.rb', line 203

def extension
  ::File.extname(name).sub(/^\./, '')
end

#move_to(to, options = {}) ⇒ Object



174
175
176
177
178
# File 'lib/vfs/entries/file.rb', line 174

def move_to to, options = {}
  copy_to to, options
  destroy options
  to
end

#move_to!(to, options = {}) ⇒ Object



179
180
181
182
# File 'lib/vfs/entries/file.rb', line 179

def move_to! to, options = {}
  options[:override] = true
  move_to to, options
end

#read(options = {}, &block) ⇒ Object

CRUD



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
  storage.open do |fs|
    begin
      if block
        fs.read_file path, &block
      else
        data = ""
        fs.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]
        # unknown internal error
        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

Extra Stuff



188
189
190
191
192
193
194
195
# File 'lib/vfs/entries/file.rb', line 188

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

#sizeObject



197
# File 'lib/vfs/entries/file.rb', line 197

def size; get :size end

#update(options = {}, &block) ⇒ Object



112
113
114
115
116
# File 'lib/vfs/entries/file.rb', line 112

def update options = {}, &block
  options[:override] = true
  data = read options
  write block.call(data), options
end

#write(*args, &block) ⇒ Object



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/vfs/entries/file.rb', line 55

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]

  storage.open do |fs|
    # TODO2 Performance lost, extra call to check file existence
    # We need to check if the file exist before writing to it, otherwise it's
    # impossible to distinguish if the StandardError caused by the 'already exist' error or
    # some other error.
    entry = self.entry
    if entry.exist?
      if options[:override]
        entry.destroy
      else
        raise Error, "entry #{self} already exist!"
      end
    end

    try = 0
    begin
      try += 1
      if block
        fs.write_file(path, options[:append], &block)
      else
        fs.write_file(path, options[:append]){|writer| writer.write data}
      end
    rescue StandardError => error
      parent = self.parent
      if parent.exist?
        # some unknown error
        raise error
      else
        parent.create(options)
      end

      try < 2 ? retry : raise(error)
    end
  end
  self
end

#write!(*args, &block) ⇒ Object



100
101
102
103
104
# File 'lib/vfs/entries/file.rb', line 100

def write! *args, &block
  args << {} unless args.last.is_a? Hash
  args.last[:override] = true
  write *args, &block
end