Class: HypertextApplicationLanguage::NamespaceManager

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hypertext_application_language/namespace_manager.rb

Overview

Handles compact URIs, a.k.a. CURIEs. Representations and representation factories have CURIEs handled by a name-space manager instance.

Constant Summary collapse

REL =

Defines the relative reference token, the placeholder used in CURIEs.

'{rel}'.freeze

Instance Method Summary collapse

Constructor Details

#initializeNamespaceManager

Returns a new instance of NamespaceManager.



17
18
19
20
# File 'lib/hypertext_application_language/namespace_manager.rb', line 17

def initialize
  # Retains one relative hypertext reference for one name.
  @ref_for_name = {}
end

Instance Method Details

#curie(href) ⇒ String

Converts an expanded hypertext reference to a CURIE’d reference based on the current set of CURIE specifications, the name-spaces. hypertext reference, or nil if there is no matching CURIE.

Returns:

  • (String)

    Answers the CURIE’d reference corresponding to the given



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hypertext_application_language/namespace_manager.rb', line 39

def curie(href)
  @ref_for_name.each do |name, ref|
    # start_index = ref.index(REL)
    # end_index = start_index + REL.length
    # left = ref[0...start_index]
    # right = ref[end_index..-1]
    # if href.start_with?(left) && href.end_with?(right)
    #   middle = href[start_index..(end_index - 2)]
    #   return name + ':' + middle
    # end
    left, right = ref.split(REL)
    if href.start_with?(left) && href.end_with?(right)
      return name + ':' + href[left.length...-right.length]
    end
  end
  nil
end

#href(curie) ⇒ Object

Converts a CURIE’d reference to a hypertext reference. followed by a colon delimiter, followed by a CURIE argument.

Splits the name at the first colon. The prefix portion before the colon identifies the name of the CURIE. The portion after the colon replaces the {rel} placeholder. This is a very basic way to parse a CURIE, but it works.

Parameters:

  • curie (String)

    The argument is a string comprising a name prefix



65
66
67
68
69
70
# File 'lib/hypertext_application_language/namespace_manager.rb', line 65

def href(curie)
  name, arg = curie.split(':', 2)
  ref = @ref_for_name[name]
  return nil unless ref
  ref.sub(REL, arg)
end

#namespacesHash<String, String>

their name.

Returns:

  • (Hash<String, String>)

    Answers a hash of relative references by



15
# File 'lib/hypertext_application_language/namespace_manager.rb', line 15

def_delegator :@ref_for_name, :dup, :namespaces

#with_namespace(name, ref) ⇒ Object

Adds a name-space to this manager.

Parameters:

  • name (String)

    Names the CURIE. This appears in CURIE references as the prefix before the colon. The relative reference comes after the colon.

  • ref (String)

    Gives the CURIE’s relative reference. It must include the {rel} placeholder identifying where to substitute the CURIE argument, the value that replaces the placeholder.



30
31
32
33
# File 'lib/hypertext_application_language/namespace_manager.rb', line 30

def with_namespace(name, ref)
  @ref_for_name[name] = ref
  self
end