Class: VCSToolkit::Objects::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/vcs_toolkit/objects/tree.rb

Instance Attribute Summary collapse

Attributes inherited from Object

#id, #object_type

Instance Method Summary collapse

Methods inherited from Object

#==, #hash, #named?

Methods included from Serializable

#from_hash, #serialize_on

Methods included from Utils::HashableObject

included

Constructor Details

#initialize(files:, trees:, id: nil, **context) ⇒ Tree

Returns a new instance of Tree.



14
15
16
17
18
19
20
21
# File 'lib/vcs_toolkit/objects/tree.rb', line 14

def initialize(files:, trees:, id: nil, **context)
  @files = files
  @trees = trees

  super id:          id,
        object_type: :tree,
        **context
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



11
12
13
# File 'lib/vcs_toolkit/objects/tree.rb', line 11

def files
  @files
end

#treesObject (readonly)

Returns the value of attribute trees.



11
12
13
# File 'lib/vcs_toolkit/objects/tree.rb', line 11

def trees
  @trees
end

Instance Method Details

#all_files(object_store, ignore: []) ⇒ Object

Iterates over all [file, blob_id] pairs recursively (including files in child trees).



27
28
29
# File 'lib/vcs_toolkit/objects/tree.rb', line 27

def all_files(object_store, ignore: [])
  enum_for :yield_all_files, object_store, ignore: ignore
end

#find(object_store, path) ⇒ Object

Finds the object id of a blob or tree by its relative path to the current tree.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vcs_toolkit/objects/tree.rb', line 35

def find(object_store, path)
  if [nil, '', '/', '.'].include? path
    id
  elsif files.key? path
    files[path]
  else
    dir_name, sub_path = path.split('/', 2)

    return nil unless trees.key? dir_name

    subtree = object_store.fetch trees[dir_name]
    subtree.find(object_store, sub_path)
  end
end