Class: Coradoc::Reference::Address

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/coradoc/reference/address.rb,
lib/coradoc/reference/address/doi.rb,
lib/coradoc/reference/address/url.rb,
lib/coradoc/reference/address/isbn.rb,
lib/coradoc/reference/address/path.rb,
lib/coradoc/reference/address/anchor.rb,
lib/coradoc/reference/address/scoped_path.rb

Overview

Scheme-aware locator targeting a Content node.

An Address is one of six built-in schemes — :anchor, :path, :scoped_path, :url, :doi, :isbn — selected by heuristic on parse or by an explicit hint:. External gems register additional schemes via Address.register_scheme (OCP).

Address.parse("foo")               # => anchor "foo"
Address.parse("ELF-5005-1#sec-3")  # => path "ELF-5005-1", fragment "sec-3"
Address.parse("ELF:5005:1#sec-3")  # => scoped_path scope "ELF"
Address.parse("https://x.y/z")     # => url
Address.parse("10.1234/abc")       # => doi
Address.parse("ISBN 978-1-2-3")    # => isbn

Addresses are value types: two addresses are equal iff every attribute matches. Round-trip via to_s.

Defined Under Namespace

Modules: Anchor, Doi, Isbn, Path, Scheme, ScopedPath, Url Classes: ParseError, UnknownSchemeError

Constant Summary collapse

BUILTIN_SCHEME_ORDER =
%i[url doi isbn scoped_path path anchor].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(raw, hint: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/coradoc/reference/address.rb', line 42

def parse(raw, hint: nil)
  Scheme.ensure_builtins_registered!
  mod = hint ? scheme_for_hint!(hint) : Scheme.match(raw)
  unless mod
    raise ParseError,
          "Cannot determine address scheme for #{raw.inspect}"
  end
  mod.parse(raw)
end

.register_scheme(mod) ⇒ Object



52
53
54
# File 'lib/coradoc/reference/address.rb', line 52

def register_scheme(mod)
  Scheme.register(mod)
end

.scheme_namesObject



56
57
58
59
# File 'lib/coradoc/reference/address.rb', line 56

def scheme_names
  Scheme.ensure_builtins_registered!
  Scheme.names
end

Instance Method Details

#to_sObject

Value equality (==/eql?/hash) comes from Lutaml::Model — all attributes compared, class-aware, safe as Hash keys.



74
75
76
77
78
79
80
81
82
83
# File 'lib/coradoc/reference/address.rb', line 74

def to_s
  Scheme.ensure_builtins_registered!
  mod = Scheme.for(scheme)
  unless mod
    raise UnknownSchemeError,
          "No registered scheme for #{scheme.inspect}. " \
          "Registered: #{Scheme.names.inspect}"
  end
  mod.serialize(self)
end