Class: ConceptQL::Knitter::Cache
- Inherits:
-
Object
- Object
- ConceptQL::Knitter::Cache
- Defined in:
- lib/conceptql/knitter.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #cache_dir ⇒ Object
- #cache_file_path(str) ⇒ Object
- #db_opts ⇒ Object
- #fetch_or_create(str, &block) ⇒ Object
- #hash_fodder ⇒ Object
- #hash_it(str) ⇒ Object
-
#initialize(db, file, options = {}) ⇒ Cache
constructor
A new instance of Cache.
- #remove_cache ⇒ Object
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, = {}) @db = db @file = file = .nil? ? {} : .dup remove_cache if [:ignore] end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
169 170 171 |
# File 'lib/conceptql/knitter.rb', line 169 def db @db end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
169 170 171 |
# File 'lib/conceptql/knitter.rb', line 169 def file @file end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
169 170 171 |
# File 'lib/conceptql/knitter.rb', line 169 def end |
Instance Method Details
#cache_dir ⇒ Object
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_opts ⇒ Object
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_fodder ⇒ Object
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_cache ⇒ Object
178 179 180 181 |
# File 'lib/conceptql/knitter.rb', line 178 def remove_cache cache_dir.rmtree @cache_dir = nil end |