Class: Twig::Cache::Filesystem

Inherits:
Base
  • Object
show all
Defined in:
lib/twig/cache/filesystem.rb

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ Filesystem

Returns a new instance of Filesystem.



6
7
8
9
10
# File 'lib/twig/cache/filesystem.rb', line 6

def initialize(directory)
  super()

  @directory = directory
end

Instance Method Details

#generate_key(_name, class_name) ⇒ Object



12
13
14
15
16
# File 'lib/twig/cache/filesystem.rb', line 12

def generate_key(_name, class_name)
  hash = ::Digest::SHA256.hexdigest(class_name)

  File.join(directory, hash[0] + hash[1], "#{hash}.rb")
end

#load(key) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/twig/cache/filesystem.rb', line 18

def load(key)
  if File.file?(key)
    Kernel.load(key)

    true
  else
    false
  end
end

#timestamp(key) ⇒ Object



35
36
37
38
39
# File 'lib/twig/cache/filesystem.rb', line 35

def timestamp(key)
  return 0 unless File.file?(key)

  File.mtime(key).to_i
end

#write(key, content) ⇒ Object



28
29
30
31
32
33
# File 'lib/twig/cache/filesystem.rb', line 28

def write(key, content)
  dirname = File.dirname(key)

  FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
  File.write(key, content)
end