Class: SQLtorial::QueryCache

Inherits:
Object
  • Object
show all
Defined in:
lib/sqltorial/query_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_to_md) ⇒ QueryCache

Returns a new instance of QueryCache.



7
8
9
# File 'lib/sqltorial/query_cache.rb', line 7

def initialize(query_to_md)
  @query_to_md = query_to_md
end

Instance Attribute Details

#query_to_mdObject (readonly)

Returns the value of attribute query_to_md.



5
6
7
# File 'lib/sqltorial/query_cache.rb', line 5

def query_to_md
  @query_to_md
end

Instance Method Details

#cache_fileObject



23
24
25
# File 'lib/sqltorial/query_cache.rb', line 23

def cache_file
  @cache_file ||= Pathname.pwd + '.cache' + cache_file_name
end

#cache_file_nameObject



27
28
29
# File 'lib/sqltorial/query_cache.rb', line 27

def cache_file_name
  @cache_file_name ||= Digest::SHA256.hexdigest("#{input_str}") + ".md"
end

#input_strObject



31
32
33
34
35
# File 'lib/sqltorial/query_cache.rb', line 31

def input_str
  @input_str ||= %w(query row_limit validation_directives other_directives).inject("") do |s, meth|
    s + query_to_md.send(meth).inspect
  end
end

#make_cache_fileObject



18
19
20
21
# File 'lib/sqltorial/query_cache.rb', line 18

def make_cache_file
  cache_file.dirname.mkpath
  cache_file.write(query_to_md.get_md)
end

#to_mdObject



11
12
13
14
15
16
# File 'lib/sqltorial/query_cache.rb', line 11

def to_md
  unless cache_file.exist?
    make_cache_file
  end
  cache_file.read
end