Class: Codeowners::Checker::FileAsArray

Inherits:
Object
  • Object
show all
Defined in:
lib/codeowners/checker/file_as_array.rb

Overview

Convert CODEOWNERS file content to an array.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ FileAsArray

Returns a new instance of FileAsArray.



10
11
12
13
# File 'lib/codeowners/checker/file_as_array.rb', line 10

def initialize(filename)
  @filename = filename
  @target_dir, = File.split(@filename)
end

Instance Attribute Details

#contentArray

Returns of lines chomped.

Returns:

  • (Array)

    of lines chomped



16
17
18
19
20
# File 'lib/codeowners/checker/file_as_array.rb', line 16

def content
  @content ||= File.readlines(@filename).map(&:chomp)
rescue Errno::ENOENT
  @content = []
end

#filenameObject (readonly)

Returns the value of attribute filename.



8
9
10
# File 'lib/codeowners/checker/file_as_array.rb', line 8

def filename
  @filename
end

Instance Method Details

#persist!Object

Save content to the @file Creates the directory of the file if needed



24
25
26
27
28
29
30
# File 'lib/codeowners/checker/file_as_array.rb', line 24

def persist!
  Dir.mkdir(@target_dir) unless Dir.exist?(@target_dir)

  File.open(@filename, 'w+') do |f|
    f.puts content
  end
end