Class: Vfs::Drivers::Local

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

Defined Under Namespace

Classes: Writer

Constant Summary collapse

DEFAULT_BUFFER =
1000 * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Local

Returns a new instance of Local.



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

#bufferObject



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

def buffer
  @buffer || DEFAULT_BUFFER
end

Instance Method Details

#attributes(path) ⇒ Object

Attributes.



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

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

#closeObject



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

def close; end

#create_dir(path) ⇒ Object

Dir.



89
90
91
92
# File 'lib/vfs/drivers/local.rb', line 89

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

#delete_dir(original_path) ⇒ Object



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

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

#delete_file(path) ⇒ Object



79
80
81
82
# File 'lib/vfs/drivers/local.rb', line 79

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

#each_entry(path, query, &block) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/vfs/drivers/local.rb', line 99

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}
    end
  else
    ::Dir.foreach path do |name|
      next if name == '.' or name == '..'
      block.call name, ->{::File.directory?("#{path}/#{name}") ? :dir : :file}
    end
  end
end

#local?Boolean

Other.

Returns:

  • (Boolean)


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

def local?; true end

#open(&block) ⇒ Object



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

def open &block
  block.call self if block
end

#read_file(path, &block) ⇒ Object

File.



61
62
63
64
65
66
67
68
# File 'lib/vfs/drivers/local.rb', line 61

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

#set_attributes(path, attrs) ⇒ Object



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

def set_attributes path, attrs
  # TODO2 set attributes.
  not_implemented
end

#tmp(&block) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/vfs/drivers/local.rb', line 135

def tmp &block
  path = "/tmp/#{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

#to_sObject



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

def to_s; '' end

#write_file(original_path, append, &block) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/vfs/drivers/local.rb', line 70

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