Class: Git::DirstatInfo

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

Overview

Immutable result object from git --dirstat output

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

Examples:

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

#entriesObject (readonly)

Returns the value of attribute entries

Returns:

  • (Object)

    the current value of entries



41
42
43
44
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
# File 'lib/git/dirstat_info.rb', line 41

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
  #
  # @yield [DirstatEntry] each entry
  # @return [Enumerator] if no block given
  #
  def each(&block)
    entries.each(&block)
  end

  include Enumerable
end

Instance Method Details

#[](directory) ⇒ Float?

Look up percentage by directory path

Parameters:

  • directory (String)

    the directory path

Returns:

  • (Float, nil)

    the percentage or nil if not found



47
48
49
# File 'lib/git/dirstat_info.rb', line 47

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

#each {|DirstatEntry| ... } ⇒ Enumerator

Iterate over entries

Yields:

Returns:

  • (Enumerator)

    if no block given



80
81
82
# File 'lib/git/dirstat_info.rb', line 80

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

#empty?Boolean

Check if dirstat is empty

Returns:

  • (Boolean)


71
72
73
# File 'lib/git/dirstat_info.rb', line 71

def empty?
  entries.empty?
end

#sizeInteger

Number of directories in the dirstat

Returns:

  • (Integer)


63
64
65
# File 'lib/git/dirstat_info.rb', line 63

def size
  entries.size
end

#to_hHash<String, Float>

Convert to a Hash mapping directory to percentage

Returns:

  • (Hash<String, Float>)


55
56
57
# File 'lib/git/dirstat_info.rb', line 55

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