Class: Vfs::File

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

Instance Attribute Summary

Attributes inherited from Entry

#driver, #path, #path_cache

Instance Method Summary collapse

Methods inherited from Entry

#==, #created_at, #destroy, #dir, #dir?, #entry, #eql?, #file, #file?, #get, #hash, #initialize, #inspect, #local?, #name, #parent, #remove, #set, #tmp, #updated_at

Constructor Details

This class inherits a constructor from Vfs::Entry

Instance Method Details

#append(*args, &block) ⇒ Object



83
84
85
86
87
# File 'lib/vfs/entries/file.rb', line 83

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

#basenameObject



139
140
141
# File 'lib/vfs/entries/file.rb', line 139

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

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

Transfers.

Raises:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/vfs/entries/file.rb', line 100

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

#create(options = {}) ⇒ Object

def content options = {}

read options

end



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

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

#deleteObject



94
95
96
# File 'lib/vfs/entries/file.rb', line 94

def delete
  delete_entry
end

#extensionObject



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

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

#move_to(to) ⇒ Object



120
121
122
123
124
# File 'lib/vfs/entries/file.rb', line 120

def move_to to
  copy_to to
  delete
  to
end

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

CRUD.



8
9
10
11
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
# File 'lib/vfs/entries/file.rb', line 8

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]
        # 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.



128
129
130
131
132
133
134
135
# File 'lib/vfs/entries/file.rb', line 128

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



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

def size; get :size end

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



89
90
91
92
# File 'lib/vfs/entries/file.rb', line 89

def update options = {}, &block
  data = read options
  write block.call(data), options
end

#write(*args, &block) ⇒ Object



47
48
49
50
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
# File 'lib/vfs/entries/file.rb', line 47

def write *args, &block
  if block
    options = args.first || {}
  else
    data, options = *args
    data ||= ""
    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.delete
      elsif !parent.exist?
        parent.create(options)
      else
        # unknown error
        raise error
      end

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