Class: Bookbinder::FileSystem::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/bookbinder/file_system/directory.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Directory

Returns a new instance of Directory.



3
4
5
# File 'lib/bookbinder/file_system/directory.rb', line 3

def initialize(path)
  @dir = path
end

Instance Method Details

#eachObject



47
48
49
50
51
52
# File 'lib/bookbinder/file_system/directory.rb', line 47

def each
  Dir.glob(File.join(@dir, '**', '*')) { |path|
    next  if File.directory?(path)
    yield(path.sub(/#{@dir}\//, ''))
  }
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/bookbinder/file_system/directory.rb', line 8

def exists?(path)
  File.exists?(full_path(path))
end

#get_file(path, mode = 'r', &blk) ⇒ Object



33
34
35
36
37
# File 'lib/bookbinder/file_system/directory.rb', line 33

def get_file(path, mode = 'r', &blk)
  fpath = full_path(path)
  must_exist(fpath)  if mode[0] != 'w'
  File.open(fpath, mode, &blk)
end

#get_io(path, &blk) ⇒ Object



28
29
30
# File 'lib/bookbinder/file_system/directory.rb', line 28

def get_io(path, &blk)
  get_file(path, 'r', &blk)
end

#read(path) ⇒ Object



13
14
15
16
17
# File 'lib/bookbinder/file_system/directory.rb', line 13

def read(path)
  fpath = full_path(path)
  must_exist(fpath)
  File.read(fpath)
end

#set_file(path, file) ⇒ Object



40
41
42
43
44
# File 'lib/bookbinder/file_system/directory.rb', line 40

def set_file(path, file)
  fpath = full_path(path)
  FileUtils.mkdir_p(File.dirname(fpath))
  FileUtils.cp(file.path, fpath)
end

#write(path, data) ⇒ Object



20
21
22
23
24
25
# File 'lib/bookbinder/file_system/directory.rb', line 20

def write(path, data)
  fpath = full_path(path)
  FileUtils.mkdir_p(File.dirname(fpath))
  File.open(fpath, 'w') { |f| f.write(data) }
  nil
end