Class: HypertextApplicationLanguage::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/hypertext_application_language/link.rb

Overview

Links belong to representations; a representation retains zero or more links. Each link gives a hypertext reference for a relation, at least. Some links provide more information.

Links have attributes. The relation and hypertext reference attributes are primary and are always required for every link. Representations may carry multiple links with the same relation, just like a HTML page. Links sharing the same relation refer to the same thing, or things.

This is the simplest possible implementation of a hypertext-application-language (HAL) link. It does not support immutability. All links and their attributes remain mutable, even after attaching to a representation. This condition continues until you freeze the instance, according to the Ruby immutability paradigm.

Required link attribute names collapse

REL =
'rel'.freeze
HREF =
'href'.freeze

Optional link attribute names collapse

NAME =
'name'.freeze
TITLE =
'title'.freeze
HREFLANG =
'hreflang'.freeze
PROFILE =
'profile'.freeze

Special link relations collapse

SELF_REL =

This special link relation describes the link to the representations own source, i.e. itself.

'self'.freeze
CURIES_REL =

Special link relation used for name-spaces. Representation name-spaces appear in rendered links under the “curies” relation; where the link name corresponds to the name-space name and the link href corresponds to the name-space reference with its embedded {rel} placeholder.

'curies'.freeze

Constant Summary collapse

ATTRIBUTE_NAMES =

Array of attribute names including those required and those optional.

[
  # required
  REL,
  HREF,

  # optional
  NAME,
  TITLE,
  HREFLANG,
  PROFILE,
].freeze

Required attributes collapse

Optional attributes collapse

Instance Method Summary collapse

Constructor Details

#initialize(rel, href, *args) ⇒ Link

Creates a new mutable link.

Parameters:

  • rel (String)

    Relation.

  • href (String)

    Hypertext reference.

  • args (Array<String, Hash>)

    Array of strings used to initialise the optional attributes in the following order: name, title, hreflang and profile. If the last element is a Hash, these become keyword arguments where you can set up the optional link attributes by name, either symbolic or string.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hypertext_application_language/link.rb', line 26

def initialize(rel, href, *args)
  @rel = rel
  @href = href

  # Take an array of arguments following the #rel and #href; these arguments
  # assign to the optional link attributes in order. Pick out keyword
  # arguments if the last argument is a #Hash. Be indifferent about the
  # keywords; accept both string and symbols. Do this by converting the
  # string keys to symbols if they do not otherwise match anything in the
  # keyword arguments hash. Take care not to re-invoke the hash fetch again
  # using the subscript operator, otherwise the default #Proc will recurse
  # indefinitely.
  keyword_args = args.last.is_a?(Hash) ? args.pop : {}
  keyword_args.default_proc = proc do |hash, key|
    hash.fetch(key.to_sym, nil)
  end
  @name, @title, @hreflang, @profile = args
  @name ||= keyword_args[NAME]
  @title ||= keyword_args[TITLE]
  @hreflang ||= keyword_args[HREFLANG]
  @profile ||= keyword_args[PROFILE]
end

Instance Attribute Details

#hrefObject

Link attribute describing the hypertext reference. The reference can be a full universal resource location, or some element thereof. It can be just the path.



67
68
69
# File 'lib/hypertext_application_language/link.rb', line 67

def href
  @href
end

#hreflangObject

Returns the ISO 639-1 code describing the link’s language. You can have multiple links for the same relation but for different languages.



76
77
78
# File 'lib/hypertext_application_language/link.rb', line 76

def hreflang
  @hreflang
end

#nameObject

Returns the value of attribute name.



71
72
73
# File 'lib/hypertext_application_language/link.rb', line 71

def name
  @name
end

#profileObject

Returns the value of attribute profile.



78
79
80
# File 'lib/hypertext_application_language/link.rb', line 78

def profile
  @profile
end

#relObject

Returns the value of attribute rel.



62
63
64
# File 'lib/hypertext_application_language/link.rb', line 62

def rel
  @rel
end

#titleObject

Returns the value of attribute title.



72
73
74
# File 'lib/hypertext_application_language/link.rb', line 72

def title
  @title
end

Instance Method Details

#freezeObject

When you freeze the object, also freeze all the instance variables. Otherwise, you can still modify the existing instance variables’ assigned objects even though you cannot reassign the variables themselves.



53
54
55
56
57
58
# File 'lib/hypertext_application_language/link.rb', line 53

def freeze
  instance_variables.each do |instance_variable|
    instance_variable_get(instance_variable).freeze
  end
  super
end