Skip to content

Commit

Permalink
Add MethodSource.clear_cache
Browse files Browse the repository at this point in the history
We often need to clear cache.

For example, in web application development, when we changed source code, we would like the change applied to the app server without reboot.

In the case of a Rails application, this requires the following configuration:

```ruby
Rails.application.configure do
  config.to_prepare do
    MethodSource.instance_variable_set(:@lines_for_file, {})
  end
end
```

It seems so ugly, isn't it? So I feel we need to add `MethodSource.clear_cache` interface.

```diff
  Rails.application.configure do
    config.to_prepare do
-     MethodSource.instance_variable_set(:@lines_for_file, {})
+     MethodSource.clear_cache
    end
  end
```
  • Loading branch information
yhirano55 committed Nov 17, 2021
1 parent e7ec137 commit 2d7ea44
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/method_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ 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 = {}
end

# @deprecated — use MethodSource::CodeHelpers#complete_expression?
def self.valid_expression?(str)
complete_expression?(str)
Expand Down

0 comments on commit 2d7ea44

Please sign in to comment.