Sha256: 8861d23590dc7a714310dbb9860cefc04e731452b7911b93f32131ad5d7edfab

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

# Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
# License:   Apache License, Version 2.0

module RTM::Sugar::Topic::BestName

  # Tries to find the best matching name for a given topic.
  #
  def best_name(theme = nil)
    all_names = self.names

    # return last part of identifier if no name exist
    return find_user_friendly_identifier if all_names.empty

    resulting_names = []

    if theme
      resulting_names = self["- @#{theme.reference}"]
      if resulting_names.empty?
        resulting_names = all_names.select{|name| name.scope.include?(theme)}
      end
    end

    # collect name with default name type
    if resulting_names.empty?
      resulting_names = self["-"]
    end

    # use all names
    if resulting_names.empty?
      resulting_names = all_names
    end

    # get the name with the smalles scope and smallest value
    min_scoped_name = resulting_names.sort_by{|name| [name.scope.size, name.value.length, name.value]}.first
    return min_scoped_name.value
  end

  # Returns the shortest most significant identifier in yaml-style.
  def shortest_topic_identifier
    return self.references(:outputstyle => :yaml, :resolve_qnames => :true, :resolve_base_iri => :true, :sort_by_length => :true).
      first unless references.empty?

    raise("Runtime Exception: Topic has no identifiers.")
  end

  private
  # Returns a subject identifier, subject locator or item identifier in this
  # preference. Qnames will be resolved backwards.
  def find_user_friendly_identifier
    return self.references(:outputstyle => :blank, :resolve_qnames => :true, :resolve_base_iri => :true, :sort_by_length => :true).
      first unless references.empty?

    raise("Runtime Exception: Topic has no identifiers.")
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rtm-0.3.1 lib/rtm/sugar/topic/best_name.rb
rtm-0.3.0 lib/rtm/sugar/topic/best_name.rb