Class: OLS::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/ols/cache.rb

Overview

Utility class responsible for handling caching in the OLS gem. You should NOT really interact with instances of this class directly, use the methods on the OLS module and the resulting OLS::Term objects.

Author:

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Cache

Creates a new OLS::Cache object and scans the cache directory for cached terms.



12
13
14
15
16
17
18
# File 'lib/ols/cache.rb', line 12

def initialize(args={})
  options = { :directory => Dir.getwd }.merge(args)
  @cache_directory = options[:directory]
  @cache_directory.sub!(/\/$/,'')

  prepare_cache
end

Instance Method Details

#add_ontology_to_cache(ontology) ⇒ Object Also known as: refresh_ontology_in_cache

Add an ontology to the cache.

Parameters:

  • ontology (String)

    The ontology to add

Raises:

  • (ArgumentError)

    Raised if the ontology is not part of OLS



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ols/cache.rb', line 62

def add_ontology_to_cache(ontology)
  raise ArgumentError, "'#{ontology}' is not a valid OLS ontology" unless OLS.ontologies.include?(ontology)

  remove_ontology_from_cache(ontology) if @cached_ontologies.has_key?(ontology)

  OLS.root_terms(ontology).each do |term|
    term.focus_graph!
    term.send(:get_term_metadata)
    term.all_children.each { |child| child.send(:get_term_metadata) }

    term_filename = "#{term.term_id.gsub(':','')}.marshal"
    File.open("#{@cache_directory}/#{term_filename}",'w') { |f| f << Marshal.dump(term) }

    @cached_ontologies[ontology] ||= { :root_terms => [], :filenames => [], :date => Date.today }
    @cached_ontologies[ontology][:root_terms].push(term.term_id) unless @cached_ontologies[ontology][:root_terms].include? term.term_id
    @cached_ontologies[ontology][:filenames].push(term_filename) unless @cached_ontologies[ontology][:filenames].include? term_filename
  end

  write_cached_ontologies_to_disk
  prepare_cache
end

#cached_ontologiesArray

Returns a list of the cached ontologies.

Returns:

  • (Array)

    A list of the cached ontologies



54
55
56
# File 'lib/ols/cache.rb', line 54

def cached_ontologies
  @cached_ontologies.keys
end

#find_by_id(term_id) ⇒ OLS::Term

Pull an OLS::Term object out of the cache. Returns nil if the term is not found.

Parameters:

  • term_id (String)

    The ontology term_id to look up

Returns:

  • (OLS::Term)

    The found OLS::Term object or nil



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ols/cache.rb', line 24

def find_by_id(term_id)
  found_term = nil
  filename = @term_id_to_files[term_id]

  unless filename.nil? || filename.to_s.empty?
    root_term = Marshal.load( @the_cache[filename] )
    found_term = root_term.send(:find_in_graph,term_id)
  end

  found_term
end

#remove_ontology_from_cache(ontology) ⇒ Object

Remove an ontology from the cache.

Parameters:

  • ontology (String)

    The ontology to remove

Raises:

  • (ArgumentError)

    Raised if the ontology is not part of OLS



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ols/cache.rb', line 90

def remove_ontology_from_cache(ontology)
  raise ArgumentError, "'#{ontology}' is not part of the cache" unless OLS.ontologies.include?(ontology)

  @cached_ontologies[ontology][:filenames].each do |file|
    File.delete("#{@cache_directory}/#{file}")
  end

  @cached_ontologies.delete(ontology)

  write_cached_ontologies_to_disk
  prepare_cache
end

#root_terms(ontology) ⇒ Array

Pull root_terms (as OLS::Term objects) out of the cache. Returns an empty array if the specified ontology is not in the cache.

Parameters:

  • ontology (String)

    The ontology name

Returns:

  • (Array)

    An array of root terms



41
42
43
44
45
46
47
48
49
# File 'lib/ols/cache.rb', line 41

def root_terms(ontology)
  root_terms = []

  if @cached_ontologies.keys.include? ontology
    root_terms = @cached_ontologies[ontology][:root_terms].map { |term_id| self.find_by_id(term_id) }
  end

  root_terms
end