Class: FastIgnore::Candidate

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_ignore/candidate.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(full_path, filename, directory, exists, content) ⇒ Candidate

Returns a new instance of Candidate.



11
12
13
14
15
16
17
# File 'lib/fast_ignore/candidate.rb', line 11

def initialize(full_path, filename, directory, exists, content)
  @full_path = full_path
  @filename = filename
  (@directory = directory) unless directory.nil?
  (@exists = exists) unless exists.nil?
  (@first_line = content.slice(/.*/)) if content # we only care about the first line
end

Class Method Details

.rootObject



6
7
8
# File 'lib/fast_ignore/candidate.rb', line 6

def root
  @root ||= new('/', nil, true, true, nil)
end

Instance Method Details

#directory?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
# File 'lib/fast_ignore/candidate.rb', line 40

def directory?
  return @directory if defined?(@directory)

  @directory = ::File.lstat(@full_path).directory?
rescue ::Errno::ENOENT, ::Errno::EACCES, ::Errno::ENAMETOOLONG
  @exists ||= false
  @directory = false
end

#exists?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
# File 'lib/fast_ignore/candidate.rb', line 49

def exists?
  return @exists if defined?(@exists)

  @exists = ::File.exist?(@full_path)
rescue ::Errno::EACCES, ::Errno::ELOOP, ::Errno::ENAMETOOLONG
  # :nocov: can't quite get this set up in a test
  @exists = false
  # :nocov:
end

#filenameObject



59
60
61
# File 'lib/fast_ignore/candidate.rb', line 59

def filename
  @filename ||= ::File.basename(@full_path)
end

#first_lineObject

how long can a shebang be? www.in-ulm.de/~mascheck/various/shebang/



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fast_ignore/candidate.rb', line 65

def first_line # rubocop:disable Metrics/MethodLength
  @first_line ||= begin
    file = ::File.new(@full_path)
    first_line = file.sysread(64)
    if first_line.start_with?('#!')
      first_line += file.readline unless first_line.include?("\n")
      file.close
      first_line
    else
      file.close
      ''
    end
  rescue ::EOFError, ::SystemCallError
    # :nocov:
    file&.close
    # :nocov:
    ''
  end
end

#keyObject

use 0 because it can’t be in paths



24
25
26
27
28
29
30
31
32
# File 'lib/fast_ignore/candidate.rb', line 24

def key
  @key ||= :"#{
    "\0" if defined?(@directory) && @directory
  }#{
    @full_path
  }\0#{
    @first_line if defined?(@first_line)
  }"
end

#parentObject



19
20
21
# File 'lib/fast_ignore/candidate.rb', line 19

def parent
  @parent ||= ::FastIgnore::Candidate.new(::File.dirname(@full_path), nil, true, true, nil)
end

#relative_to(dir) ⇒ Object



34
35
36
37
38
# File 'lib/fast_ignore/candidate.rb', line 34

def relative_to(dir)
  return unless @full_path.start_with?(dir)

  ::FastIgnore::RelativeCandidate.new(@full_path.delete_prefix(dir), self)
end