Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/method_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,15 @@ def self.lines_for(file_name, name=nil)
raise SourceNotFoundError, "Could not load source for #{name}: #{e.message}"
end

# Clear cache.
def self.clear_cache
@lines_for_file = {}
# Clear cached source lines.
#
# @param [String, nil] file_name The file to evict, or nil to clear all files.
def self.clear_cache(file_name=nil)
if file_name
@lines_for_file.delete(file_name) if @lines_for_file
else
@lines_for_file = {}
end
end

# @deprecated — use MethodSource::CodeHelpers#complete_expression?
Expand Down Expand Up @@ -174,4 +180,3 @@ class Proc
include MethodSource::SourceLocation::ProcExtensions
include MethodSource::MethodExtensions
end

30 changes: 30 additions & 0 deletions spec/method_source_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
require 'spec_helper'
require 'tempfile'

describe MethodSource do

describe ".clear_cache" do
it "can evict one file without clearing other cached files" do
first = Tempfile.new("method-source-first")
second = Tempfile.new("method-source-second")

first.write("first old\n")
second.write("second old\n")
first.flush
second.flush

expect(MethodSource.lines_for(first.path)).to eq(["first old\n"])
expect(MethodSource.lines_for(second.path)).to eq(["second old\n"])

File.write(first.path, "first new\n")
File.write(second.path, "second new\n")
MethodSource.clear_cache(first.path)

expect(MethodSource.lines_for(first.path)).to eq(["first new\n"])
expect(MethodSource.lines_for(second.path)).to eq(["second old\n"])

MethodSource.clear_cache
expect(MethodSource.lines_for(second.path)).to eq(["second new\n"])
ensure
MethodSource.clear_cache
first.close!
second.close!
end
end

describe "source_location (testing 1.8 implementation)" do
it 'should return correct source_location for a method' do
expect(method(:hello).source_location.first).to match(/spec_helper/)
Expand Down