Class: VCSToolkit::Utils::MemoryFileStore

Inherits:
FileStore
  • Object
show all
Defined in:
lib/vcs_toolkit/utils/memory_file_store.rb

Instance Method Summary collapse

Methods inherited from FileStore

#all_files, #delete, #directories, #exist?, #files

Constructor Details

#initialize(file_hash = {}) ⇒ MemoryFileStore

Returns a new instance of MemoryFileStore.



7
8
9
10
11
12
13
# File 'lib/vcs_toolkit/utils/memory_file_store.rb', line 7

def initialize(file_hash = {})
  @files = {}

  file_hash.each do |path, content|
    store path, content
  end
end

Instance Method Details

#changed?(path, blob) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/vcs_toolkit/utils/memory_file_store.rb', line 51

def changed?(path, blob)
  content = fetch path

  content != blob.content
end

#delete_dir(path) ⇒ Object



29
30
31
# File 'lib/vcs_toolkit/utils/memory_file_store.rb', line 29

def delete_dir(path)
  # Do nothing as the directories are infered from the file paths
end

#delete_file(path) ⇒ Object



25
26
27
# File 'lib/vcs_toolkit/utils/memory_file_store.rb', line 25

def delete_file(path)
  @files.delete sanitize_path(path, false)
end

#directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
# File 'lib/vcs_toolkit/utils/memory_file_store.rb', line 41

def directory?(path)
  return false if file? path

  path = sanitize_path(path, true)

  @files.keys.any? do |file_path|
    file_path.start_with? path
  end
end

#each_directory(path = '') ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/vcs_toolkit/utils/memory_file_store.rb', line 70

def each_directory(path='')
  yielded_dirs = {}

  path = sanitize_path(path, true) + '/' unless path.empty?

  @files.each do |file_path, _|
    name = file_path.sub(path, '')

    if file_path.start_with?(path) and not name.empty? and name.include?('/')
      name = name.split('/').first

      yield name unless name.empty? or yielded_dirs.key? name
      yielded_dirs[name] = true
    end
  end
end

#each_file(path = '') ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/vcs_toolkit/utils/memory_file_store.rb', line 57

def each_file(path='')
  path = path.sub(/\/+$/, '')
  path = "#{path}/" unless path.empty?

  @files.each do |file_path, file|
    name = file_path.sub(path, '').sub(/^\/+/, '')

    if file_path.start_with?(path) and not name.empty? and not name.include?('/')
      yield name
    end
  end
end

#fetch(path) ⇒ Object



33
34
35
# File 'lib/vcs_toolkit/utils/memory_file_store.rb', line 33

def fetch(path)
  @files.fetch sanitize_path(path, false)
end

#file?(path) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/vcs_toolkit/utils/memory_file_store.rb', line 37

def file?(path)
  @files.key? sanitize_path(path, false)
end

#store(path, content) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/vcs_toolkit/utils/memory_file_store.rb', line 15

def store(path, content)
  path = sanitize_path(path, false)

  if path.end_with? '/'
    raise 'You can store only files. The directories will be infered from the path'
  end

  @files[path] = content
end