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.



173
174
175
176
177
178
179
180
181
# File 'lib/dhall/resolve.rb', line 173

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



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/dhall/resolve.rb', line 183

def fetch(key, &block)
  if key.is_a?(String) && key.start_with?("sha256:")
    file = @dir + key.sub(/^sha256:/, "")
    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_binary) }
      result
    end
  else
    @ram.fetch(key, &block)
  end
end