Class: RBS::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs_loading_optimizer/loading/optimizer/environment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#modified_timeObject

Returns the value of attribute modified_time.



47
48
49
# File 'lib/rbs_loading_optimizer/loading/optimizer/environment.rb', line 47

def modified_time
  @modified_time
end

Class Method Details

.cached_loader(loader) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/rbs_loading_optimizer/loading/optimizer/environment.rb', line 18

def cached_loader(loader)
  if system_cached?(loader.latest_modified_time)
    fully_cached_loader(loader.latest_modified_time)
  else
    loader
  end
end

.cached_path(filename) ⇒ Object



42
43
44
# File 'lib/rbs_loading_optimizer/loading/optimizer/environment.rb', line 42

def cached_path(filename)
  Pathname(ENV["XDG_CACHE_HOME"] || File.expand_path("~/.cache")).join("rbs", filename)
end

.from_loader(loader) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/rbs_loading_optimizer/loading/optimizer/environment.rb', line 9

def from_loader(loader)
  new_loader = cached_loader(loader)
  new.tap do |env|
    # NOTE: memoize the latest modified time to create cache file
    env.modified_time = new_loader.latest_modified_time
    new_loader.load(env:)
  end
end

.fully_cached_loader(latest_modified_time) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/rbs_loading_optimizer/loading/optimizer/environment.rb', line 26

def fully_cached_loader(latest_modified_time)
  @fully_cached_loader ||= RBS::EnvironmentLoader.new(core_root: nil).tap do |loader|
    loader.latest_modified_time = latest_modified_time
    loader.cached_sources = [system_cache_path]
    loader.add(path: system_cache_path)
  end
end

.system_cache_pathObject



38
39
40
# File 'lib/rbs_loading_optimizer/loading/optimizer/environment.rb', line 38

def system_cache_path
  @system_cache_path ||= cached_path("system.rbs")
end

.system_cached?(modified_time) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/rbs_loading_optimizer/loading/optimizer/environment.rb', line 34

def system_cached?(modified_time)
  system_cache_path.exist? && system_cache_path.mtime == modified_time
end

Instance Method Details

#original_resolve_declarationObject



59
# File 'lib/rbs_loading_optimizer/loading/optimizer/environment.rb', line 59

alias original_resolve_declaration resolve_declaration

#original_resolve_type_namesObject



49
# File 'lib/rbs_loading_optimizer/loading/optimizer/environment.rb', line 49

alias original_resolve_type_names resolve_type_names

#resolve_declaration(resolver, map, decl, outer:, prefix:) ⇒ Object



61
62
63
64
65
66
# File 'lib/rbs_loading_optimizer/loading/optimizer/environment.rb', line 61

def resolve_declaration(resolver, map, decl, outer:, prefix:)
  # NOTE: skip resolution if the declaration is already resolved (came from cache)
  return decl if decl.resolved

  original_resolve_declaration(resolver, map, decl, outer:, prefix:)
end

#resolve_type_names(only: nil) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/rbs_loading_optimizer/loading/optimizer/environment.rb', line 51

def resolve_type_names(only: nil)
  original_resolve_type_names(only:).tap do |resolved_env|
    # NOTE: Write the environment to the cache file
    resolved_env.modified_time = modified_time
    write_cache("system.rbs", resolved_env)
  end
end