Module: RTM::Sugar::Topic::TopicRef

Defined in:
rtm/lib/rtm/sugar/topic/topic_ref.rb,
rtm/spec/rtm/sugar/topic/topic_ref_spec.rb

Instance Method Summary

Instance Method Details

- (Object) reference(params = {})

Returns one identifier of this topic. If several exist, the shortest one is returned.

This method takes a Hash as argument.

The key :ouputstyle defines if the ctm style (value :ctm, this is the default), the YAML style (value :yaml) or no style (value :blank) is supported.

In ctm style an item identifier starts with a “^”, a subject locator starts with a “=” and a subject identifiers contains no prefix.

In yaml style an item identifiers start with a “ii:”, a subject locator starts with a “sl:” and a subject identifier start with a “si:”.

:call-seq:

    reference -> String
    reference(params = {}) -> String


97
98
99
# File 'rtm/lib/rtm/sugar/topic/topic_ref.rb', line 97

def reference(params = {})
  return references(params).sort_by{|identifier| identifier.size}.reverse.first
end

- (Object) references(params = {})

Returns an Array including all identifiers of this Topic.

This method takes a Hash as argument.

The key :ouputstyle defines if the ctm style (value :ctm, this is the default), the YAML style (value :yaml) or no style (value :blank) is supported.

In ctm style an item identifier starts with a “^”, a subject locator starts with a “=” and a subject identifiers contains no prefix.

In yaml style an item identifiers start with a “ii:”, a subject locator starts with a “sl:” and a subject identifier start with a “si:”.

:call-seq:

    references -> Array of Strings
    references(params = {}) -> Array of Strings


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'rtm/lib/rtm/sugar/topic/topic_ref.rb', line 27

def references(params = {})
  default_hash = {:outputstyle => :ctm, :resolve_qnames => :false, :resolve_base_iri => :false, :sort_by_length => :false}
  params = default_hash.merge(params) if params.is_a? Hash
  prefixes = topic_map.prefixes

  set_of_si = subject_identifiers.map{|si| si.reference}
  set_of_sl = subject_locators.map{|si| si.reference}
  set_of_ii = item_identifiers.map{|si| si.reference}

  if params[:resolve_qnames] == :true
    unless prefixes.empty?
      prefixes.each do |identifier, reference|
        set_of_si = set_of_si.map{|si| si.index(reference) == 0 ? si.sub(reference, identifier + ":") : si}
        set_of_sl = set_of_sl.map{|sl| sl.index(reference) == 0 ? sl.sub(reference, identifier + ":") : sl}
        set_of_ii = set_of_ii.map{|ii| ii.index(reference) == 0 ? ii.sub(reference, identifier + ":") : ii}
      end
    end
  end

  if params[:resolve_base_iri] == :true
    set_of_si = set_of_si.map{|si| unresolve(si)}
    set_of_sl = set_of_sl.map{|sl| unresolve(sl)}
    set_of_ii = set_of_ii.map{|ii| unresolve(ii)}
  end

  if params[:sort_by_length] == :true
    set_of_si = set_of_si.sort_by{|si| si.length}
    set_of_sl = set_of_sl.sort_by{|sl| sl.length}
    set_of_ii = set_of_ii.sort_by{|ii| ii.length}
  end

  case params[:outputstyle]
  when :yaml
    identifiers = set_of_si.map{  |si| "si:#{si}"} +
      set_of_sl.map{  |sl| "sl:#{sl}"} +
      set_of_ii.map{  |ii| "ii:#{ii}"}
  when :blank
    identifiers = set_of_si +
      set_of_sl +
      set_of_ii
  else #:ctm default
    identifiers = set_of_si +
      set_of_sl.map{  |sl| "=#{sl}"} +
      set_of_ii.map{  |ii| "^#{ii}"}
  end
  return identifiers
end

- (Object) references_as_locators

Returns all identifiers of this Topic as Array of Locators.

:call-seq:

  references_as_locators -> Array of Locators


106
107
108
# File 'rtm/lib/rtm/sugar/topic/topic_ref.rb', line 106

def references_as_locators
  subject_identifiers.to_a + subject_locators.to_a + item_identifiers.to_a
end

- (Object) unresolve(identifier)

Unresolves the identifier against the base_iri.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'rtm/lib/rtm/sugar/topic/topic_ref.rb', line 111

def unresolve(identifier)
  raise("identifier must be a String") unless identifier.is_a?(String)
  base_iri = topic_map.base_iri
  if identifier.index(base_iri) == 0
    short_form = identifier.sub(base_iri, "")
    if topic_map.create_locator(short_form).reference == identifier
      return short_form
    end
#        if short_form[0] == "/"
#          short_form = short_form.sub("/", "")
#
#        end
  else
    return identifier #nothing happened
  end
end