Class: Dhall::Resolvers::StandardFileCache

Inherits:
Object
  • Object
show all
Defined in:
lib/dhall/resolve.rb

Instance Method Summary collapse

Constructor Details

#initialize(dir = Pathname.new(ENV.fetch( "XDG_CACHE_HOME", ENV.fetch("HOME") + "/.cache/" )) + "dhall/") ⇒ StandardFileCache

Returns a new instance of StandardFileCache.



165
166
167
168
169
170
171
172
173
# File 'lib/dhall/resolve.rb', line 165

def initialize(
	dir=Pathname.new(ENV.fetch(
		"XDG_CACHE_HOME", ENV.fetch("HOME") + "/.cache/"
	)) + "dhall/"
)
	dir.mkpath
	@dir = dir
	@ram = RamCache.new
end

Instance Method Details

#fetch(key, &block) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/dhall/resolve.rb', line 175

def fetch(key, &block)
	if key.is_a?(String) && key.start_with?("sha256:")
		file = @dir + key.sub(/^sha256:/, "1220")
		return Dhall.from_binary(file.binread) if file.exist?

		Promise.resolve(nil).then(&block).then do |result|
			file.open("wb") { |fh| fh.write(result.to_cbor) }
			result
		end
	else
		@ram.fetch(key, &block)
	end
end