Class: Vfs::Drivers::Local

Inherits:
Object
  • Object
show all
Defined in:
lib/vfs/drivers/local.rb

Defined Under Namespace

Classes: Writer

Constant Summary

DEFAULT_BUFFER =
1000 * 1024

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Local) initialize(options = {})



17
18
19
20
21
# File 'lib/vfs/drivers/local.rb', line 17

def initialize options = {}
  options = options.clone
  @root = options.delete(:root) || ''
  raise "invalid options #{options}" unless options.empty?
end

Instance Attribute Details

- (Object) buffer



29
30
31
# File 'lib/vfs/drivers/local.rb', line 29

def buffer
  @buffer || DEFAULT_BUFFER
end

Instance Method Details

- (Object) attributes(path)

Attributes



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vfs/drivers/local.rb', line 36

def attributes path
  path = with_root path

  stat = ::File.stat path
  attrs = {}
  attrs[:file] = !!stat.file?
  attrs[:dir]  = !!stat.directory?

  # attributes special for file system
  attrs[:created_at] = stat.ctime
  attrs[:updated_at] = stat.mtime
  attrs[:size]       = stat.size if attrs[:file]
  attrs
rescue Errno::ENOTDIR
  nil
rescue Errno::ENOENT
  nil
end

- (Object) close



26
# File 'lib/vfs/drivers/local.rb', line 26

def close; end

- (Object) create_dir(path)

Dir



95
96
97
98
# File 'lib/vfs/drivers/local.rb', line 95

def create_dir path
  path = with_root path
  ::Dir.mkdir path
end

- (Object) delete_dir(original_path)



100
101
102
103
# File 'lib/vfs/drivers/local.rb', line 100

def delete_dir original_path
  path = with_root original_path
  ::FileUtils.rm_r path
end

- (Object) delete_file(path)



82
83
84
85
# File 'lib/vfs/drivers/local.rb', line 82

def delete_file path
  path = with_root path
  ::File.delete path
end

- (Object) each_entry(path, query, &block)



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/vfs/drivers/local.rb', line 105

def each_entry path, query, &block
  path = with_root path

  if query
    path_with_trailing_slash = path == '/' ? path : "#{path}/"
    ::Dir["#{path_with_trailing_slash}#{query}"].each do |absolute_path|
      name = absolute_path.sub path_with_trailing_slash, ''
      block.call name, ->{::File.directory?(absolute_path) ? :dir : :file}
      # if ::File.directory? absolute_path
      #   block.call relative_path, :dir
      # else
      #   block.call relative_path, :file
      # end
    end
  else
    ::Dir.foreach path do |name|
      next if name == '.' or name == '..'
      block.call name, ->{::File.directory?("#{path}/#{name}") ? :dir : :file}
      # if ::File.directory? "#{path}/#{relative_name}"
      #   block.call relative_name, :dir
      # else
      #   block.call relative_name, :file
      # end
    end
  end
end

- (Boolean) local?

Other



150
# File 'lib/vfs/drivers/local.rb', line 150

def local?; true end

- (Object) open(&block)



23
24
25
# File 'lib/vfs/drivers/local.rb', line 23

def open &block
  block.call self if block
end

- (Object) read_file(path, &block)

File



64
65
66
67
68
69
70
71
# File 'lib/vfs/drivers/local.rb', line 64

def read_file path, &block
  path = with_root path
  ::File.open path, 'r' do |is|
    while buff = is.gets(self.buffer || DEFAULT_BUFFER)
      block.call buff
    end
  end
end

- (Object) set_attributes(path, attrs)



55
56
57
58
# File 'lib/vfs/drivers/local.rb', line 55

def set_attributes path, attrs
  # TODO2 set attributes
  not_implemented
end

- (Object) tmp(&block)



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/vfs/drivers/local.rb', line 152

def tmp &block
  path = "/tmp/#{rand(10**6)}"
  # tmp_dir = "#{::Dir.tmpdir}/#{rand(10**6)}"
  if block
    begin
      ::FileUtils.mkdir_p with_root(path)
      block.call path
    ensure
      ::FileUtils.rm_r with_root(path) if ::File.exist? with_root(path)
    end
  else
    ::FileUtils.mkdir_p with_root(path)
    path
  end
end

- (Object) to_s



168
# File 'lib/vfs/drivers/local.rb', line 168

def to_s; '' end

- (Object) write_file(original_path, append, &block)



73
74
75
76
77
78
79
80
# File 'lib/vfs/drivers/local.rb', line 73

def write_file original_path, append, &block
  path = with_root original_path

  option = append ? 'a' : 'w'
  ::File.open path, "#{option}b" do |out|
    block.call Writer.new(out)
  end
end