Skip to content

Commit

Permalink
Merge pull request #1919 from kmuto/fix-image-finder
Browse files Browse the repository at this point in the history
Add `ReVIEW::Book::Cache` and use it in `ReVIEW::Book::ImageFinder`
  • Loading branch information
takahashim authored Aug 14, 2024
2 parents b070710 + 00dfb94 commit 378d1dc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/review/book/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'review/configure'
require 'review/catalog'
require 'review/book/bib'
require 'review/book/cache'

module ReVIEW
module Book
Expand All @@ -19,6 +20,7 @@ class Base
attr_accessor :catalog
attr_reader :basedir
attr_accessor :bibpaper_index
attr_reader :cache

def self.load(basedir = '.', config: nil)
new(basedir, config: config)
Expand All @@ -39,6 +41,8 @@ def initialize(basedir = '.', config: nil)

@warn_old_files = {} # XXX for checking CHAPS, PREDEF, POSTDEF
@basedir_seen = {}

@cache = ReVIEW::Book::Cache.new
update_rubyenv
end

Expand Down
54 changes: 54 additions & 0 deletions lib/review/book/cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#
# Copyright (c) 2014-2024 Minero Aoki, Kenshi Muto, Masayoshi Takahashi
#
# This program is free software.
# You can distribute or modify this program under the terms of
# the GNU LGPL, Lesser General Public License version 2.1.
# For details of LGPL, see the file "COPYING".
#
module ReVIEW
module Book
class Cache
def initialize
@store = {}
end

def reset
@store.clear
end

# key should be Symbol, not String
def fetch(key, &block)
raise ArgumentError, 'Key should be Symbol' unless key.is_a?(Symbol)

if cached?(key)
read(key)
else
exec_block_and_save(key, &block)
end
end

def cached?(key)
@store.key?(key)
end

private

def read(key)
@store[key]
end

def write(key, value)
@store[key] = value
end

def exec_block_and_save(key)
result = yield(key)

write(key, result)

result
end
end
end
end
4 changes: 3 additions & 1 deletion lib/review/book/image_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def entry_object(path)
end

def dir_entries
Dir.glob(File.join(@basedir, '**{,/*/**}/*.*')).uniq.sort.map { |entry| entry.sub(%r{^\./}, '') }
@book.cache.fetch(:image_finder_dir_entries) do
Dir.glob(File.join(@basedir, '**{,/*/**}/*.*')).uniq.sort.map { |entry| entry.sub(%r{^\./}, '') }
end
end

def add_entry(path)
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def compile_inline(text)
def compile_block(text)
method_name = "compile_block_#{@builder.target_name}"
method_name = 'compile_block_default' unless self.respond_to?(method_name, true)
@chapter.book.cache.reset
self.__send__(method_name, text)
end

Expand Down

0 comments on commit 378d1dc

Please sign in to comment.