Class: VirtualFileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/virtual_file_system.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVirtualFileSystem

Returns a new instance of VirtualFileSystem.



11
12
13
# File 'lib/virtual_file_system.rb', line 11

def initialize()
  @root = VirtualTree.new({path: ""}, {})
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



9
10
11
# File 'lib/virtual_file_system.rb', line 9

def root
  @root
end

Instance Method Details

#[](file_name) ⇒ Object



15
16
17
# File 'lib/virtual_file_system.rb', line 15

def [](file_name)
  @root.get_file(file_name)
end

#file_for_diff(commit, diff) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/virtual_file_system.rb', line 19

def file_for_diff(commit, diff)
  if diff.operation == :move
    @root.move_file(diff.a_file_name, diff.b_file_name)
  elsif diff.operation == :delete
    @root.delete_file(diff.a_file_name)
  else
    file_name = diff.b_file_name
    file = @root.get_file(file_name)
    file = @root.set_file(file_name, VirtualFile.new(file_name, diff)) if file.nil?
    file
  end
end

#file_pathsObject



60
61
62
63
64
# File 'lib/virtual_file_system.rb', line 60

def file_paths
  root
    .all_files()
    .map { |file| file.path }
end

#load!(commits, meta) ⇒ Object



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

def load!(commits, meta)
  commit = commits.first
  return commits if commit.nil? || commit.cached_files.nil?
  commit.cached_trees.each do |tree_object|
    tree_id = tree_object[:object_id]
    cached_tree = Cache.read_object(tree_id)
    @root.add_tree(VirtualTree.from_json(cached_tree))
  end
  commit.cached_files.each do |file_object|
    file_id = file_object[:object_id]
    cached_file = Cache.read_object(file_id)
    if not cached_file.nil?
      @root.set_file(file_object[:path], VirtualFile.from_json(file_id, cached_file))
    end
  end
  sync_meta!(commit, meta)
  commits
end

#save(commit) ⇒ Object



55
56
57
58
# File 'lib/virtual_file_system.rb', line 55

def save(commit)
  prune(commit)
  @root.save(commit)
end

#touch_tree(diff_tree, commit) ⇒ Object



51
52
53
# File 'lib/virtual_file_system.rb', line 51

def touch_tree(diff_tree, commit)
  @root.touch(diff_tree, commit)
end