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.



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

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.



3
4
5
# File 'lib/sqltorial/query_cache.rb', line 3

def query_to_md
  @query_to_md
end

Instance Method Details

#cache_fileObject



21
22
23
# File 'lib/sqltorial/query_cache.rb', line 21

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

#cache_file_nameObject



25
26
27
# File 'lib/sqltorial/query_cache.rb', line 25

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

#input_strObject



29
30
31
32
33
# File 'lib/sqltorial/query_cache.rb', line 29

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



16
17
18
19
# File 'lib/sqltorial/query_cache.rb', line 16

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

#to_mdObject



9
10
11
12
13
14
# File 'lib/sqltorial/query_cache.rb', line 9

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