Class: Gitmain::Tree

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, id, path = nil, name = nil) ⇒ Tree

Returns a new instance of Tree.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
# File 'lib/gitmain/tree.rb', line 5

def initialize(repository, id, path = nil, name = nil)
  raise ArgumentError, 'provide a valid repository' unless repository.kind_of?(Gitmain::Repository)
  raise ArgumentError, 'provide a valid tree id' unless id.to_s.match(/\A[0-9a-f]{40}\z/)
  @repository = repository
  @id = id
  @path = path
  @name = name
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/gitmain/tree.rb', line 3

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/gitmain/tree.rb', line 3

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/gitmain/tree.rb', line 3

def path
  @path
end

#repositoryObject (readonly)

Returns the value of attribute repository.



3
4
5
# File 'lib/gitmain/tree.rb', line 3

def repository
  @repository
end

Instance Method Details

#blobs(klass: Gitmain::Blob, recursive: false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gitmain/tree.rb', line 14

def blobs(klass: Gitmain::Blob, recursive: false)
  if recursive
    _tree.walk(:postorder).map do |root, object|
      if object[:type] == :blob
        blob_path = File.join(*[path, name, root].map(&:to_s).reject(&:empty?))
        blob_path.sub!(/\/\z/, '')
        blob_path = nil if blob_path.empty?
        klass.new(repository, object[:oid], blob_path, object[:name])
      end
    end.compact
  else
    _tree.each.map do |object|
      if object[:type] == :blob
        blob_path = File.join(*[path, name].map(&:to_s).reject(&:empty?))
        blob_path.sub!(/\/\z/, '')
        blob_path = nil if blob_path.empty?
        klass.new(repository, object[:oid], blob_path, object[:name])
      end
    end.compact
  end
end

#file_nameObject



36
37
38
# File 'lib/gitmain/tree.rb', line 36

def file_name
  File.join(*[path, name].map(&:to_s).reject(&:empty?))
end

#trees(klass: Gitmain::Tree, recursive: false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gitmain/tree.rb', line 40

def trees(klass: Gitmain::Tree, recursive: false)
  if recursive
    _tree.walk(:postorder).map do |root, object|
      if object[:type] == :tree
        tree_path = File.join(*[path, name, root].map(&:to_s).reject(&:empty?))
        tree_path.sub!(/\/\z/, '')
        tree_path = nil if tree_path.empty?
        klass.new(repository, object[:oid], tree_path, object[:name])
      end
    end.compact
  else
    _tree.each.map do |object|
      if object[:type] == :tree
        tree_path = File.join(*[path, name].map(&:to_s).reject(&:empty?))
        tree_path.sub!(/\/\z/, '')
        tree_path = nil if tree_path.empty?
        klass.new(repository, object[:oid], tree_path, object[:name])
      end
    end.compact
  end
end