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(root = '') ⇒ Local

Returns a new instance of Local.



14
15
16
# File 'lib/vfs/drivers/local.rb', line 14

def initialize root = ''
  @root = root
end

Instance Attribute Details

#bufferObject



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

def buffer
  @buffer || DEFAULT_BUFFER
end

Instance Method Details

#attributes(path) ⇒ Object

Attributes



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vfs/drivers/local.rb', line 31

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



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

def close; end

#create_dir(path) ⇒ Object

Dir



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

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

#delete_dir(original_path) ⇒ Object



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

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

#delete_file(path) ⇒ Object



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

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

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/vfs/drivers/local.rb', line 100

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

#local?Boolean

Other

Returns:

  • (Boolean)


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

def local?; true end

#open(&block) ⇒ Object



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

def open &block
  block.call self if block
end

#read_file(path, &block) ⇒ Object

File



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

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



50
51
52
53
# File 'lib/vfs/drivers/local.rb', line 50

def set_attributes path, attrs
  # TODO2 set attributes
  not_implemented
end

#tmp(&block) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/vfs/drivers/local.rb', line 147

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

#to_sObject



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

def to_s; '' end

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



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

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

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