Class: Utopia::Content::Link
- Inherits:
-
Object
- Object
- Utopia::Content::Link
- Defined in:
- lib/utopia/content/link.rb
Overview
Represents a link to some content with associated metadata.
Instance Attribute Summary collapse
-
#info ⇒ Object
readonly
Returns the value of attribute info.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#locale ⇒ Object
readonly
Returns the value of attribute locale.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compare this object with another object.
-
#[](key) ⇒ Object
Look up from the
links.yamlmetadata with a given symbolic key. -
#default_locale? ⇒ Boolean
Check whether this link uses the default locale.
-
#eql?(other) ⇒ Boolean
Check whether this object is equivalent to another object.
-
#full_path(root, extension = XNODE_EXTENSION) ⇒ Object
Resolve the backing content file beneath a root directory.
-
#href ⇒ Object
Resolve this link's target URI.
-
#href? ⇒ Boolean
Check whether this link has a target URI.
-
#index? ⇒ Boolean
Check whether this link refers to an index document.
-
#initialize(kind, name, locale, path, info, title = nil) ⇒ Link
constructor
A new instance of Link.
-
#key ⇒ Object
Build the filesystem key, including the locale suffix when present.
-
#relative_href(base = nil) ⇒ Object
Resolve this link's target relative to a base path.
-
#title ⇒ Object
Return the display title from metadata or the inferred title.
-
#to_anchor(base: nil, content: self.title, builder: nil, **attributes) ⇒ Object
(also: #to_href)
Render this link as an anchor element.
-
#to_s ⇒ Object
Convert this object to a string.
-
#virtual? ⇒ Boolean
Check whether this link is virtual.
Constructor Details
#initialize(kind, name, locale, path, info, title = nil) ⇒ Link
Returns a new instance of Link.
21 22 23 24 25 26 27 28 |
# File 'lib/utopia/content/link.rb', line 21 def initialize(kind, name, locale, path, info, title = nil) @kind = kind @name = name @locale = locale @path = Path.create(path) @info = info || {} @title = XRB::Strings.to_title(title || name) end |
Instance Attribute Details
#info ⇒ Object (readonly)
Returns the value of attribute info.
71 72 73 |
# File 'lib/utopia/content/link.rb', line 71 def info @info end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
68 69 70 |
# File 'lib/utopia/content/link.rb', line 68 def kind @kind end |
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
72 73 74 |
# File 'lib/utopia/content/link.rb', line 72 def locale @locale end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
69 70 71 |
# File 'lib/utopia/content/link.rb', line 69 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
70 71 72 |
# File 'lib/utopia/content/link.rb', line 70 def path @path end |
Instance Method Details
#==(other) ⇒ Object
Compare this object with another object.
160 161 162 |
# File 'lib/utopia/content/link.rb', line 160 def == other other and kind == other.kind and name == other.name and path == other.path end |
#[](key) ⇒ Object
Look up from the links.yaml metadata with a given symbolic key.
64 65 66 |
# File 'lib/utopia/content/link.rb', line 64 def [] key @info[key] end |
#default_locale? ⇒ Boolean
Check whether this link uses the default locale.
166 167 168 |
# File 'lib/utopia/content/link.rb', line 166 def default_locale? @locale == nil end |
#eql?(other) ⇒ Boolean
Check whether this object is equivalent to another object.
153 154 155 |
# File 'lib/utopia/content/link.rb', line 153 def eql? other self.class.eql?(other.class) and kind.eql?(other.kind) and name.eql?(other.name) and path.eql?(other.path) and info.eql?(other.info) end |
#full_path(root, extension = XNODE_EXTENSION) ⇒ Object
Resolve the backing content file beneath a root directory.
46 47 48 49 50 |
# File 'lib/utopia/content/link.rb', line 46 def full_path(root, extension = XNODE_EXTENSION) if @path&.file? File.join(root, @path.dirname, self.key + XNODE_EXTENSION) end end |
#href ⇒ Object
Resolve this link's target URI.
54 55 56 57 58 59 60 61 |
# File 'lib/utopia/content/link.rb', line 54 def href @href ||= @info.fetch(:uri) do @info.fetch(:href) do # Omit the variant suffix from inferred content links: (@path.dirname + @path.basename).to_url_path.encoded if @path end end end |
#href? ⇒ Boolean
Check whether this link has a target URI.
76 77 78 |
# File 'lib/utopia/content/link.rb', line 76 def href? !!href end |
#index? ⇒ Boolean
Check whether this link refers to an index document.
82 83 84 |
# File 'lib/utopia/content/link.rb', line 82 def index? @kind == :index end |
#key ⇒ Object
Build the filesystem key, including the locale suffix when present.
32 33 34 35 36 37 38 39 40 |
# File 'lib/utopia/content/link.rb', line 32 def key if @path if locale "#{@path.last}.#{@locale}" else @path.last end end end |
#relative_href(base = nil) ⇒ Object
Resolve this link's target relative to a base path.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/utopia/content/link.rb', line 95 def relative_href(base = nil) target = href return unless target return target unless base url = Protocol::URL[target] relative_url = url.relative_to(Path[base].to_url_path) # Preserve unchanged targets exactly and avoid allocating a serialized replacement: if relative_url.equal?(url) return target end return relative_url.to_s end |
#title ⇒ Object
Return the display title from metadata or the inferred title.
113 114 115 |
# File 'lib/utopia/content/link.rb', line 113 def title @info.fetch(:title, @title) end |
#to_anchor(base: nil, content: self.title, builder: nil, **attributes) ⇒ Object Also known as: to_href
Render this link as an anchor element.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/utopia/content/link.rb', line 123 def to_anchor(base: nil, content: self.title, builder: nil, **attributes) attributes[:class] ||= "link" XRB::Builder.fragment(builder) do |inner_builder| if href? attributes[:href] ||= relative_href(base) attributes[:target] ||= @info[:target] inner_builder.inline("a", attributes) do inner_builder.text(content) end else inner_builder.inline("span", attributes) do inner_builder.text(content) end end end end |
#to_s ⇒ Object
Convert this object to a string.
146 147 148 |
# File 'lib/utopia/content/link.rb', line 146 def to_s "\#<#{self.class}(#{self.kind}) title=#{title.inspect} href=#{href.inspect}>" end |
#virtual? ⇒ Boolean
Check whether this link is virtual.
88 89 90 |
# File 'lib/utopia/content/link.rb', line 88 def virtual? @kind == :virtual end |