Sha256: b19ae525145446c4058314c945c7659713af40f8ecdb5ac11db2a5d2699dadae

Contents?: true

Size: 1.63 KB

Versions: 13

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require_relative '../checks'
require_relative 'file'

module Github
  # A GitHub file tree.
  class Tree
    include Checks
    include Enumerable

    attr_reader :owner, :repo, :ref, :truncated, :files

    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

    class << self
      include Checks

      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
    end

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

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

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

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

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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
sqlui-0.1.84 app/github/tree.rb
sqlui-0.1.83 app/github/tree.rb
sqlui-0.1.82 app/github/tree.rb
sqlui-0.1.81 app/github/tree.rb
sqlui-0.1.80 app/github/tree.rb
sqlui-0.1.79 app/github/tree.rb
sqlui-0.1.78 app/github/tree.rb
sqlui-0.1.77 app/github/tree.rb
sqlui-0.1.76 app/github/tree.rb
sqlui-0.1.75 app/github/tree.rb
sqlui-0.1.74 app/github/tree.rb
sqlui-0.1.73 app/github/tree.rb
sqlui-0.1.72 app/github/tree.rb