implement memoization

Signed-off-by: Gwenhael Le Moine <gwenhael.le.moine@gmail.com>
This commit is contained in:
Gwenhael Le Moine 2021-05-27 10:28:22 +02:00
parent 657d4dd05e
commit 1f3c0d9e14
No known key found for this signature in database
GPG key ID: FDFE3669426707A7

View file

@ -8,10 +8,21 @@ module Ledger
@binary = 'ledger'
@file = ENV[ 'LEDGER_FILE' ]
@last_mtime = Pathname.new(@file).mtime
@cache = Hash.new
def run( options, command = '', command_parameters = '' )
STDERR.puts "#{@binary} -f #{@file} #{options} #{command} #{command_parameters}"
`#{@binary} -f #{@file} #{options} #{command} #{command_parameters}`
command = "#{@binary} -f #{@file} #{options} #{command} #{command_parameters}"
mtime = Pathname.new(@file).mtime
if @last_mtime < mtime || !@cache.has_key?( command )
@last_mtime = mtime
@cache[ command ] = `#{command}`
end
@cache[ command ]
end
def version