Class: Github::Tree

Inherits:
Object
  • Object
show all
Extended by:
Checks
Includes:
Checks, Enumerable
Defined in:
app/github/tree.rb

Overview

A GitHub file tree.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Checks

check_boolean, check_enumerable_of, check_is_a, check_non_empty_string, check_non_nil, check_positive_integer

Constructor Details

#initialize(owner:, repo:, ref:, files:, truncated: false) ⇒ Tree

Returns a new instance of Tree.



14
15
16
17
18
19
20
# File 'app/github/tree.rb', line 14

def initialize(owner:, repo:, ref:, files:, truncated: false)
  @owner = check_non_empty_string(owner: owner)
  @repo = check_non_empty_string(repo: repo)
  @ref = check_non_empty_string(ref: ref)
  @files = check_enumerable_of(files, File)
  @truncated = check_boolean(truncated: truncated)
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



12
13
14
# File 'app/github/tree.rb', line 12

def files
  @files
end

#ownerObject (readonly)

Returns the value of attribute owner.



12
13
14
# File 'app/github/tree.rb', line 12

def owner
  @owner
end

#refObject (readonly)

Returns the value of attribute ref.



12
13
14
# File 'app/github/tree.rb', line 12

def ref
  @ref
end

#repoObject (readonly)

Returns the value of attribute repo.



12
13
14
# File 'app/github/tree.rb', line 12

def repo
  @repo
end

#truncatedObject (readonly)

Returns the value of attribute truncated.



12
13
14
# File 'app/github/tree.rb', line 12

def truncated
  @truncated
end

Class Method Details

.for(owner:, repo:, ref:, tree_response:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/github/tree.rb', line 25

def for(owner:, repo:, ref:, tree_response:)
  check_non_empty_string(owner: owner, repo: repo, ref: ref)
  check_is_a(tree_response: [Hash, tree_response])

  truncated = check_boolean(truncated: tree_response['truncated'])
  tree = check_is_a(tree: [Array, tree_response['tree']])
  files = tree.map do |blob|
    File.new(
      owner: owner,
      repo: repo,
      ref: ref,
      tree_sha: tree_response['sha'],
      path: blob['path'],
      content: blob['content']
    )
  end

  Tree.new(owner: owner, repo: repo, ref: ref, files: files, truncated: truncated)
end

Instance Method Details

#<<(file) ⇒ Object



60
61
62
63
64
# File 'app/github/tree.rb', line 60

def <<(file)
  check_is_a(file: [File, file])

  @files << file unless @files.include?(file)
end

#==(other) ⇒ Object



54
55
56
57
58
# File 'app/github/tree.rb', line 54

def ==(other)
  self.class == other.class &&
    @files == other.files &&
    @truncated == other.truncated
end

#[](path) ⇒ Object



46
47
48
# File 'app/github/tree.rb', line 46

def [](path)
  @files.find { |f| f.path == path }
end

#each(&block) ⇒ Object



50
51
52
# File 'app/github/tree.rb', line 50

def each(&block)
  @files.each(&block)
end