Class: PathList::RootCandidate

Inherits:
Object
  • Object
show all
Defined in:
lib/path_list/root_candidate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(full_path, filename, directory, content) ⇒ RootCandidate

Returns a new instance of RootCandidate.



7
8
9
10
11
12
# File 'lib/path_list/root_candidate.rb', line 7

def initialize(full_path, filename, directory, content)
  @full_path = full_path
  @filename = filename
  (@directory = directory) unless directory.nil?
  @first_line = content
end

Instance Attribute Details

#full_pathObject (readonly)

Returns the value of attribute full_path.



5
6
7
# File 'lib/path_list/root_candidate.rb', line 5

def full_path
  @full_path
end

Instance Method Details

#directory?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/path_list/root_candidate.rb', line 29

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

  @directory ||= ::File.directory?(@full_path)
end

#filenameObject



35
36
37
# File 'lib/path_list/root_candidate.rb', line 35

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

#first_lineObject

how long can a shebang be? www.in-ulm.de/~mascheck/various/shebang/ 512 feels like a reasonable limit



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/path_list/root_candidate.rb', line 42

def first_line # rubocop:disable Metrics/MethodLength
  @first_line ||= begin
    file = ::File.new(@full_path)
    first_line = new_fragment = file.sysread(512)
    file.close
    first_line || ''
  rescue ::EOFError, ::SystemCallError
    # :nocov:
    file&.close
    # :nocov:
    first_line || ''
  end
end

#parentObject



14
15
16
17
18
19
20
21
# File 'lib/path_list/root_candidate.rb', line 14

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

#relative_to(dir) ⇒ Object



23
24
25
26
27
# File 'lib/path_list/root_candidate.rb', line 23

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

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