Class: R10K::Git::Alternates
- Inherits:
-
Object
- Object
- R10K::Git::Alternates
- Defined in:
- lib/r10k/git/alternates.rb
Overview
Manage ‘$GIT_DIR/objects/info/alternates`
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
- #add(path) ⇒ Object (also: #<<)
-
#add?(path) ⇒ true, false
Conditionally add path to the alternates file.
- #include?(path) ⇒ Boolean
-
#initialize(git_dir) ⇒ Alternates
constructor
A new instance of Alternates.
- #read ⇒ Object (also: #to_a)
- #write(entries) ⇒ Object
Constructor Details
#initialize(git_dir) ⇒ Alternates
Returns a new instance of Alternates.
13 14 15 16 |
# File 'lib/r10k/git/alternates.rb', line 13 def initialize(git_dir) @file = git_dir + File.join('objects', 'info', 'alternates') @entries = [] end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
10 11 12 |
# File 'lib/r10k/git/alternates.rb', line 10 def file @file end |
Instance Method Details
#add(path) ⇒ Object Also known as: <<
18 19 20 |
# File 'lib/r10k/git/alternates.rb', line 18 def add(path) write(to_a << path) end |
#add?(path) ⇒ true, false
Conditionally add path to the alternates file
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/r10k/git/alternates.rb', line 27 def add?(path) paths = read() add_entry = !paths.include?(path) if add_entry paths << path write(paths) end add_entry end |
#include?(path) ⇒ Boolean
40 41 42 |
# File 'lib/r10k/git/alternates.rb', line 40 def include?(path) to_a.include?(path) end |
#read ⇒ Object Also known as: to_a
55 56 57 58 59 60 61 |
# File 'lib/r10k/git/alternates.rb', line 55 def read entries = [] if @file.file? entries = @file.readlines.map(&:chomp) end entries end |
#write(entries) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/r10k/git/alternates.rb', line 44 def write(entries) if ! @file.parent.directory? raise R10K::Git::GitError, _("Cannot write %{file}; parent directory does not exist") % {file: @file} end @file.open("w") do |fh| entries.each do |entry| fh.puts(entry) end end end |