Class: HaystackOntology

Inherits:
ApplicationRecord show all
Defined in:
lib/needle_in_a_haystack/models/haystack_ontology.rb

Class Method Summary collapse

Class Method Details

.create_tagsObject



29
30
31
32
# File 'lib/needle_in_a_haystack/models/haystack_ontology.rb', line 29

def self.create_tags
  factory = HaystackFactory.new(OntologyTagStrategy.new)
  factory.create_tags(tags)
end

.find_or_create_tag(name) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/needle_in_a_haystack/models/haystack_ontology.rb', line 34

def self.find_or_create_tag(name)
  factory = HaystackFactory.new(OntologyTagStrategy.new)
  tag_data = find_tag(name)
  return nil if tag_data.nil?

  attributes = { description: tag_data["description"], haystack_marker: tag_data["marker"] }
  factory.find_or_create_tag(name, attributes)
end

.find_tag(path) ⇒ Object



6
7
8
9
10
# File 'lib/needle_in_a_haystack/models/haystack_ontology.rb', line 6

def self.find_tag(path)
  return nil if path.nil?

  path.include?(".") ? find_tag_in_hierarchy(tags, path.split(".")) : find_tag_in_hierarchy(tags, [path])
end

.find_tag_in_hierarchy(current_hash, keys, path = []) ⇒ Object

This method recursively searches for a tag in a nested hash structure.

  • ‘current_hash`: The current level of the hash being searched.

  • ‘keys`: An array of keys representing the path to the desired tag.

  • ‘path`: An array to keep track of the current path (used for constructing the full path of the tag).



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/needle_in_a_haystack/models/haystack_ontology.rb', line 16

def self.find_tag_in_hierarchy(current_hash, keys, path = [])
  return nil unless current_hash.is_a?(Hash) && keys.any?

  if current_hash[keys.first].is_a?(Hash)
    path.push(keys.shift)
    return current_hash[path.last].merge("path" => path.join(".")) if keys.empty?

    find_tag_in_hierarchy(current_hash[path.last], keys, path)
  else
    current_hash[keys.shift]
  end
end

.import_full_ontologyObject



43
44
45
# File 'lib/needle_in_a_haystack/models/haystack_ontology.rb', line 43

def self.import_full_ontology
  create_tags
end

.tagsObject



2
3
4
# File 'lib/needle_in_a_haystack/models/haystack_ontology.rb', line 2

def self.tags
  @tags ||= YAML.load_file(Rails.root.join("config/haystack_ontology.yml"))
end