Class: ConceptQL::Knitter::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/conceptql/knitter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, file, options = {}) ⇒ Cache

Returns a new instance of Cache.



171
172
173
174
175
176
# File 'lib/conceptql/knitter.rb', line 171

def initialize(db, file, options = {})
  @db = db
  @file = file
  @options = options.nil? ? {} : options.dup
  remove_cache if @options[:ignore]
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



169
170
171
# File 'lib/conceptql/knitter.rb', line 169

def db
  @db
end

#fileObject (readonly)

Returns the value of attribute file.



169
170
171
# File 'lib/conceptql/knitter.rb', line 169

def file
  @file
end

#optionsObject (readonly)

Returns the value of attribute options.



169
170
171
# File 'lib/conceptql/knitter.rb', line 169

def options
  @options
end

Instance Method Details

#cache_dirObject



196
197
198
# File 'lib/conceptql/knitter.rb', line 196

def cache_dir
  @cache_dir ||= (file.dirname + ".#{hash_it(hash_fodder)}").tap { |d| d.mkpath }
end

#cache_file_path(str) ⇒ Object



183
184
185
# File 'lib/conceptql/knitter.rb', line 183

def cache_file_path(str)
  cache_dir + hash_it(str)
end

#db_optsObject



204
205
206
# File 'lib/conceptql/knitter.rb', line 204

def db_opts
  db.opts.values_at(*i(adapter user password host database search_path))
end

#fetch_or_create(str, &block) ⇒ Object



187
188
189
190
191
192
193
194
# File 'lib/conceptql/knitter.rb', line 187

def fetch_or_create(str, &block)
  cache_file = cache_file_path(str)
  return cache_file.read if cache_file.exist?
  #p ["cache miss for", str, cache_file]
  output = block.call(cache_file)
  cache_file.write(output) unless cache_file.exist?
  cache_file.read
end

#hash_fodderObject



200
201
202
# File 'lib/conceptql/knitter.rb', line 200

def hash_fodder
  (db_opts.inspect + file.basename.to_s)
end

#hash_it(str) ⇒ Object



208
209
210
# File 'lib/conceptql/knitter.rb', line 208

def hash_it(str)
  Digest::SHA256.hexdigest("#{str}")
end

#remove_cacheObject



178
179
180
181
# File 'lib/conceptql/knitter.rb', line 178

def remove_cache
  cache_dir.rmtree
  @cache_dir = nil
end