Class: HypertextApplicationLanguage::NamespaceManager
- Inherits:
-
Object
- Object
- HypertextApplicationLanguage::NamespaceManager
- 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
-
#curie(href) ⇒ String
Converts an expanded hypertext reference to a CURIE’d reference based on the current set of CURIE specifications, the name-spaces.
-
#href(curie) ⇒ Object
Converts a CURIE’d reference to a hypertext reference.
-
#initialize ⇒ NamespaceManager
constructor
A new instance of NamespaceManager.
-
#namespaces ⇒ Hash<String, String>
their name.
-
#with_namespace(name, ref) ⇒ Object
Adds a name-space to this manager.
Constructor Details
#initialize ⇒ NamespaceManager
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.
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.
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 |
#namespaces ⇒ Hash<String, String>
their name.
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.
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 |