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
-
#[](key) ⇒ Object
Look up from the `links.yaml` metadata with a given symbolic key.
- #default_locale? ⇒ Boolean
- #eql?(other) ⇒ Boolean
- #full_path(root, extension = XNODE_EXTENSION) ⇒ Object
- #href ⇒ Object
- #href? ⇒ Boolean
- #index? ⇒ Boolean
-
#initialize(kind, name, locale, path, info, title = nil) ⇒ Link
constructor
A new instance of Link.
- #key ⇒ Object
- #relative_href(base = nil) ⇒ Object
- #title ⇒ Object
- #to_anchor(base: nil, content: self.title, builder: nil, **attributes) ⇒ Object (also: #to_href)
- #to_s ⇒ Object
- #virtual? ⇒ Boolean
Constructor Details
#initialize(kind, name, locale, path, info, title = nil) ⇒ Link
Returns a new instance of Link.
36 37 38 39 40 41 42 43 |
# File 'lib/utopia/content/link.rb', line 36 def initialize(kind, name, locale, path, info, title = nil) @kind = kind @name = name @locale = locale @path = Path.create(path) @info = info || {} @title = Trenni::Strings.to_title(title || name) end |
Instance Attribute Details
#info ⇒ Object (readonly)
Returns the value of attribute info.
77 78 79 |
# File 'lib/utopia/content/link.rb', line 77 def info @info end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
74 75 76 |
# File 'lib/utopia/content/link.rb', line 74 def kind @kind end |
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
78 79 80 |
# File 'lib/utopia/content/link.rb', line 78 def locale @locale end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
75 76 77 |
# File 'lib/utopia/content/link.rb', line 75 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
76 77 78 |
# File 'lib/utopia/content/link.rb', line 76 def path @path end |
Instance Method Details
#==(other) ⇒ Object
133 134 135 |
# File 'lib/utopia/content/link.rb', line 133 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.
70 71 72 |
# File 'lib/utopia/content/link.rb', line 70 def [] key @info[key] end |
#default_locale? ⇒ Boolean
137 138 139 |
# File 'lib/utopia/content/link.rb', line 137 def default_locale? @locale == nil end |
#eql?(other) ⇒ Boolean
129 130 131 |
# File 'lib/utopia/content/link.rb', line 129 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
55 56 57 58 59 |
# File 'lib/utopia/content/link.rb', line 55 def full_path(root, extension = XNODE_EXTENSION) if @path&.file? File.join(root, @path.dirname, self.key + XNODE_EXTENSION) end end |
#href ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/utopia/content/link.rb', line 61 def href @href ||= @info.fetch(:uri) do @info.fetch(:href) do (@path.dirname + @path.basename).to_s if @path end end end |
#href? ⇒ Boolean
80 81 82 |
# File 'lib/utopia/content/link.rb', line 80 def href? !!href end |
#index? ⇒ Boolean
84 85 86 |
# File 'lib/utopia/content/link.rb', line 84 def index? @kind == :index end |
#key ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/utopia/content/link.rb', line 45 def key if @path if locale "#{@path.last}.#{@locale}" else @path.last end end end |
#relative_href(base = nil) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/utopia/content/link.rb', line 92 def relative_href(base = nil) if base and href.start_with? '/' Path.shortest_path(href, base) else href end end |
#title ⇒ Object
100 101 102 |
# File 'lib/utopia/content/link.rb', line 100 def title @info.fetch(:title, @title) end |
#to_anchor(base: nil, content: self.title, builder: nil, **attributes) ⇒ Object Also known as: to_href
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/utopia/content/link.rb', line 104 def to_anchor(base: nil, content: self.title, builder: nil, **attributes) attributes[:class] ||= 'link' Trenni::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
125 126 127 |
# File 'lib/utopia/content/link.rb', line 125 def to_s "\#<#{self.class}(#{self.kind}) title=#{title.inspect} href=#{href.inspect}>" end |
#virtual? ⇒ Boolean
88 89 90 |
# File 'lib/utopia/content/link.rb', line 88 def virtual? @kind == :virtual end |