Skip to content

Commit 1b6a6cd

Browse files
committed
Fix the PDFExtractor.version_string's memoization
Looked like there was an attempt to memoize the version_string in the @@help class var but the cached var was only consulted after the system call had already been executed. This caches the results in the @@version_string var.
1 parent 8e484ef commit 1b6a6cd

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

lib/docsplit/pdf_extractor.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
module Docsplit
44
class PdfExtractor
5-
@@executable = nil
5+
@@executable = nil
6+
@@version_string = nil
67

78
# Provide a set of helper functions to determine the OS.
89
HOST_OS = (defined?("RbConfig") ? RbConfig : Config)::CONFIG['host_os']
@@ -19,13 +20,14 @@ def linux?
1920
# The first line of the help output holds the name and version number
2021
# of the office software to be used for extraction.
2122
def version_string
22-
null = windows? ? "NUL" : "/dev/null"
23-
versionstr = `#{office_executable} -h 2>#{null}`.split("\n").first
24-
if !!versionstr.match(/[0-9]*/)
25-
versionstr = `#{office_executable} --version`.split("\n").first
23+
unless @@version_string
24+
null = windows? ? "NUL" : "/dev/null"
25+
@@version_string = `#{office_executable} -h 2>#{null}`.split("\n").first
26+
if !!@@version_string.match(/[0-9]*/)
27+
@@version_string = `#{office_executable} --version`.split("\n").first
28+
end
2629
end
27-
@@help ||= versionstr
28-
30+
@@version_string
2931
end
3032
def libre_office?
3133
!!version_string.match(/^LibreOffice/)

0 commit comments

Comments
 (0)