Class: SCV::FileStore

Inherits:
VCSToolkit::FileStore
  • Object
show all
Defined in:
lib/scv/file_store.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileStore

Returns a new instance of FileStore.



6
7
8
9
10
# File 'lib/scv/file_store.rb', line 6

def initialize(path)
  raise 'The path is not a directory' unless File.directory? path

  @base_path = path
end

Instance Method Details

#changed?(path, blob) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/scv/file_store.rb', line 46

def changed?(path, blob)
  fetch(path) != blob.content
end

#delete_dir(path) ⇒ Object



34
35
36
# File 'lib/scv/file_store.rb', line 34

def delete_dir(path)
  Dir.unlink path_for(path)
end

#delete_file(path) ⇒ Object



30
31
32
# File 'lib/scv/file_store.rb', line 30

def delete_file(path)
  File.unlink path_for(path)
end

#directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/scv/file_store.rb', line 42

def directory?(path)
  File.directory? path_for(path)
end

#each_directory(path = '', &block) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/scv/file_store.rb', line 58

def each_directory(path='', &block)
  path = '.' if path.empty?

  Dir.entries(path_for(path)).select do |name|
    not %w(. ..).include? name and directory? path_for(name, path)
  end.each(&block)
end

#each_file(path = '', &block) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/scv/file_store.rb', line 50

def each_file(path='', &block)
  path = '.' if path.empty?

  Dir.entries(path_for(path)).select do |file_name|
    file? path_for(file_name, path)
  end.each(&block)
end

#fetch(path) ⇒ Object

Raises:

  • (KeyError)


22
23
24
25
26
27
28
# File 'lib/scv/file_store.rb', line 22

def fetch(path)
  raise KeyError unless file? path

  File.open(path_for(path), 'rb') do |file|
    file.read
  end
end

#file?(path) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/scv/file_store.rb', line 38

def file?(path)
  File.file? path_for(path)
end

#store(path, content) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/scv/file_store.rb', line 12

def store(path, content)
  path = path_for path

  FileUtils.mkdir_p File.dirname(path)

  File.open(path, 'wb') do |file|
    file.write(content)
  end
end