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) ⇒ Path

Returns a new instance of Path.



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

def initialize(path)
  @path = path.to_s.freeze
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



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

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.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bootsnap/load_path_cache/path.rb', line 40

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



62
63
64
# File 'lib/bootsnap/load_path_cache/path.rb', line 62

def expanded_path
  File.expand_path(path).freeze
end

#non_directory?Boolean

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

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/bootsnap/load_path_cache/path.rb', line 28

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

#relative?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/bootsnap/load_path_cache/path.rb', line 34

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)


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

def stable?
  stability == STABLE
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)


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

def volatile?
  stability == VOLATILE
end