Sha256: bd8ee897342c8f3c1a45a131f3b6bbf35e3bebf2b0c466ba483a310dcd2bd358

Contents?: true

Size: 1.63 KB

Versions: 4

Compression:

Stored size: 1.63 KB

Contents

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

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

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

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

    result = current_hash[target_key]
    return result.is_a?(Hash) ? result.merge("path" => path.push(target_key).join(".")) : result if result

    children = current_hash["children"]
    if children.is_a?(Hash)
      children.each do |key, value|
        new_path = path + [key]
        return value.merge("path" => new_path.join(".")) if key == target_key

        result = find_tag_in_hierarchy(value, target_key, new_path)
        return result if result
      end
    end

    current_hash.each do |key, value|
      next if %w[description children].include?(key) || !value.is_a?(Hash)

      new_path = path + [key]
      result = find_tag_in_hierarchy(value, target_key, new_path)
      return result if result
    end

    nil
  end

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

  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

  def self.import_full_ontology
    create_tags
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
needle_in_a_haystack-1.0.8 lib/needle_in_a_haystack/models/haystack_ontology.rb
needle_in_a_haystack-1.0.7 lib/needle_in_a_haystack/models/haystack_ontology.rb
needle_in_a_haystack-1.0.6 lib/needle_in_a_haystack/models/haystack_ontology.rb
needle_in_a_haystack-1.0.5 lib/needle_in_a_haystack/models/haystack_ontology.rb