Class: Bootsnap::LoadPathCache::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/bootsnap/load_path_cache/path.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, real: false) ⇒ Path

Returns a new instance of Path.



24
25
26
27
# File 'lib/bootsnap/load_path_cache/path.rb', line 24

def initialize(path, real: false)
  @path = path.to_s.freeze
  @real = real
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



22
23
24
# File 'lib/bootsnap/load_path_cache/path.rb', line 22

def path
  @path
end

Instance Method Details

#entries_and_dirs(store) ⇒ Object

Return a list of all the requirable files and all of the subdirectories of this Path.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bootsnap/load_path_cache/path.rb', line 59

def entries_and_dirs(store)
  if stable?
    # the cached_mtime field is unused for 'stable' paths, but is
    # set to zero anyway, just in case we change the stability heuristics.
    _, entries, dirs = store.get(expanded_path)
    return [entries, dirs] if entries # cache hit

    entries, dirs = scan!
    store.set(expanded_path, [0, entries, dirs])
    return [entries, dirs]
  end

  cached_mtime, entries, dirs = store.get(expanded_path)

  current_mtime = latest_mtime(expanded_path, dirs || [])
  return [[], []]        if current_mtime == -1 # path does not exist
  return [entries, dirs] if cached_mtime == current_mtime

  entries, dirs = scan!
  store.set(expanded_path, [current_mtime, entries, dirs])
  [entries, dirs]
end

#expanded_pathObject



82
83
84
85
86
87
88
# File 'lib/bootsnap/load_path_cache/path.rb', line 82

def expanded_path
  if @real
    path
  else
    @expanded_path ||= File.expand_path(path).freeze
  end
end

#non_directory?Boolean

True if the path exists, but represents a non-directory object

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/bootsnap/load_path_cache/path.rb', line 47

def non_directory?
  !File.stat(path).directory?
rescue Errno::ENOENT, Errno::ENOTDIR, Errno::EINVAL
  false
end

#relative?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/bootsnap/load_path_cache/path.rb', line 53

def relative?
  !path.start_with?(SLASH)
end

#stable?Boolean

A path is considered ‘stable’ if it is part of a Gem.path or the ruby distribution. When adding or removing files in these paths, the cache must be cleared before the change will be noticed.

Returns:

  • (Boolean)


11
12
13
# File 'lib/bootsnap/load_path_cache/path.rb', line 11

def stable?
  stability == STABLE
end

#to_realpathObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bootsnap/load_path_cache/path.rb', line 29

def to_realpath
  return self if @real

  realpath = begin
    File.realpath(path)
  rescue Errno::ENOENT
    return self
  end

  if realpath == path
    @real = true
    self
  else
    Path.new(realpath, real: true)
  end
end

#volatile?Boolean

A path is considered volatile if it doesn’t live under a Gem.path or the ruby distribution root. These paths are scanned for new additions more frequently.

Returns:

  • (Boolean)


18
19
20
# File 'lib/bootsnap/load_path_cache/path.rb', line 18

def volatile?
  stability == VOLATILE
end