Class: Git::DirstatInfo Private

Inherits:
Data
  • Object
show all
Includes:
Enumerable
Defined in:
lib/git/dirstat_info.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Immutable result object from git --dirstat output

Contains the list of directories and their contribution percentages to the diff.

Work in progress; this class is internal for now and may be made public in a future release.

Examples:

Create a DirstatInfo

dirstat = Git::DirstatInfo.new(
  entries: [
    Git::DirstatEntry.new(directory: 'lib/commands/', percentage: 45.2),
    Git::DirstatEntry.new(directory: 'spec/unit/', percentage: 30.1)
  ]
)
dirstat.entries.first.directory  #=> "lib/commands/"
dirstat['lib/commands/']         #=> 45.2
dirstat.to_h  #=> { "lib/commands/" => 45.2, "spec/unit/" => 30.1 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#entriesArray<DirstatEntry> (readonly)

Returns directory statistics in order from git output.

Returns:

  • (Array<DirstatEntry>)

    directory statistics in order from git output



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/git/dirstat_info.rb', line 45

DirstatInfo = Data.define(:entries) do
  # Look up percentage by directory path
  #
  # @param directory [String] the directory path
  #
  # @return [Float, nil] the percentage or nil if not found
  #
  def [](directory)
    entries.find { |e| e.directory == directory }&.percentage
  end

  # Convert to a Hash mapping directory to percentage
  #
  # @return [Hash<String, Float>]
  #
  def to_h
    entries.to_h { |e| [e.directory, e.percentage] }
  end

  # Number of directories in the dirstat
  #
  # @return [Integer]
  #
  def size
    entries.size
  end

  # Check if dirstat is empty
  #
  # @return [Boolean]
  #
  def empty?
    entries.empty?
  end

  # Iterate over entries
  #
  # @overload each
  #
  #   @return [Enumerator<Git::DirstatEntry>] an enumerator over all dirstat entries
  #
  # @overload each(&block)
  #
  #   @return [Array<Git::DirstatEntry>] the full list of dirstat entries
  #
  #   @yield [entry] each dirstat entry
  #
  #   @yieldparam entry [Git::DirstatEntry] a single dirstat entry
  #
  #   @yieldreturn [void]
  #
  def each(&block)
    entries.each(&block)
  end

  include Enumerable
end

Instance Method Details

#[](directory) ⇒ Float?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Look up percentage by directory path

Parameters:

  • directory (String)

    the directory path

Returns:

  • (Float, nil)

    the percentage or nil if not found



52
53
54
# File 'lib/git/dirstat_info.rb', line 52

def [](directory)
  entries.find { |e| e.directory == directory }&.percentage
end

#eachEnumerator<Git::DirstatEntry> #each {|entry| ... } ⇒ Array<Git::DirstatEntry>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Iterate over entries

Overloads:

  • #eachEnumerator<Git::DirstatEntry>

    Returns an enumerator over all dirstat entries.

    Returns:

  • #each {|entry| ... } ⇒ Array<Git::DirstatEntry>

    Returns the full list of dirstat entries.

    Yields:

    • (entry)

      each dirstat entry

    Yield Parameters:

    Yield Returns:

    • (void)

    Returns:



96
97
98
# File 'lib/git/dirstat_info.rb', line 96

def each(&block)
  entries.each(&block)
end

#empty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if dirstat is empty

Returns:

  • (Boolean)


76
77
78
# File 'lib/git/dirstat_info.rb', line 76

def empty?
  entries.empty?
end

#sizeInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Number of directories in the dirstat

Returns:

  • (Integer)


68
69
70
# File 'lib/git/dirstat_info.rb', line 68

def size
  entries.size
end

#to_hHash<String, Float>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert to a Hash mapping directory to percentage

Returns:

  • (Hash<String, Float>)


60
61
62
# File 'lib/git/dirstat_info.rb', line 60

def to_h
  entries.to_h { |e| [e.directory, e.percentage] }
end