module LiveAST::Linker

Constants

REVISION_TOKEN

Public Class Methods

fetch_from_cache(file, line) click to toggle source
# File lib/live_ast/linker.rb, line 83
def fetch_from_cache(file, line)
  cache = @caches[file]
  if !cache && !file.index(REVISION_TOKEN)
    _, cache =
      if defined?(IRB) && file == "(irb)"
        new_cache(IRBSpy.code_at(line), file, line, false)
      else
        #
        # File was loaded by 'require'.
        # Play catch-up: assume it has not changed in the meantime.
        #
        new_cache(Reader.read(file), file, 1, true)
      end
  end
  cache.fetch_ast(line) if cache
end
find_ast(*location) click to toggle source
# File lib/live_ast/linker.rb, line 73
def find_ast(*location)
  raise ASTNotFoundError unless location.size == 2
  raise RawEvalError if location.first == "(eval)"

  ast = fetch_from_cache(*location)
  raise MultipleDefinitionsOnSameLineError if ast == :multiple

  ast
end
find_method_ast(klass, name, *location) click to toggle source
# File lib/live_ast/linker.rb, line 62
def find_method_ast(klass, name, *location)
  @mutex.synchronize do
    case ast = find_ast(*location)
    when nil
      fetch_method_attachment(klass, name) or raise ASTNotFoundError
    else
      attach_to_method(klass, name, ast)
    end
  end
end
find_proc_ast(obj) click to toggle source
# File lib/live_ast/linker.rb, line 53
def find_proc_ast(obj)
  @mutex.synchronize do
    fetch_proc_attachment(obj) or (
      ast = find_ast(*obj.source_location) or raise ASTNotFoundError
      attach_to_proc(obj, ast)
    )
  end
end
flush_cache() click to toggle source
# File lib/live_ast/linker.rb, line 117
def flush_cache
  @mutex.synchronize do
    @caches.delete_if { |key, _| key.index REVISION_TOKEN }
  end
end
new_cache(contents, file, user_line, file_is_key) click to toggle source

create a cache along with a unique key for it

# File lib/live_ast/linker.rb, line 103
def new_cache(contents, file, user_line, file_is_key)
  key = file_is_key ? file : file + REVISION_TOKEN + @counter
  cache = Cache.new(contents, user_line)
  @caches[key] = cache
  @counter.next!
  return key, cache
end
new_cache_synced(*args) click to toggle source
# File lib/live_ast/linker.rb, line 111
def new_cache_synced(*args)
  @mutex.synchronize do
    new_cache(*args)
  end
end