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
- #href ⇒ Object
- #href? ⇒ Boolean
-
#initialize(kind, path, info = nil) ⇒ Link
constructor
A new instance of Link.
- #relative_href(base = nil) ⇒ Object
- #title ⇒ Object
- #to_anchor(base: nil, content: self.title, builder: nil, **attributes) ⇒ Object (also: #to_href)
- #to_s ⇒ Object
Constructor Details
#initialize(kind, path, info = nil) ⇒ Link
Returns a new instance of Link.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/utopia/content/link.rb', line 37 def initialize(kind, path, info = nil) path = Path.create(path) @info = info || {} @kind = kind case @kind when :file @name, @locale = path.last.split('.', 2) @path = path when :directory @name = path.basename if @name == INDEX @name = path.dirname.basename end @locale = path.extension @path = path when :virtual @name, @locale = path.to_s.split('.', 2) @path = @info[:path] ? Path.create(@info[:path]) : nil else raise ArgumentError.new("Unknown link kind #{@kind} with path #{path}") end @title = Trenni::Strings.to_title(@name) end |
Instance Attribute Details
#info ⇒ Object (readonly)
Returns the value of attribute info.
80 81 82 |
# File 'lib/utopia/content/link.rb', line 80 def info @info end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
77 78 79 |
# File 'lib/utopia/content/link.rb', line 77 def kind @kind end |
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
81 82 83 |
# File 'lib/utopia/content/link.rb', line 81 def locale @locale end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
78 79 80 |
# File 'lib/utopia/content/link.rb', line 78 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
79 80 81 |
# File 'lib/utopia/content/link.rb', line 79 def path @path end |
Instance Method Details
#==(other) ⇒ Object
128 129 130 |
# File 'lib/utopia/content/link.rb', line 128 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.
73 74 75 |
# File 'lib/utopia/content/link.rb', line 73 def [] key @info[key] end |
#default_locale? ⇒ Boolean
132 133 134 |
# File 'lib/utopia/content/link.rb', line 132 def default_locale? @locale == nil end |
#eql?(other) ⇒ Boolean
124 125 126 |
# File 'lib/utopia/content/link.rb', line 124 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 |
#href ⇒ Object
66 67 68 69 70 |
# File 'lib/utopia/content/link.rb', line 66 def href @href ||= @info.fetch(:uri) do (@path.dirname + @path.basename).to_s if @path end end |
#href? ⇒ Boolean
83 84 85 |
# File 'lib/utopia/content/link.rb', line 83 def href? !!href end |
#relative_href(base = nil) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/utopia/content/link.rb', line 87 def relative_href(base = nil) if base and href.start_with? '/' Path.shortest_path(href, base) else href end end |
#title ⇒ Object
95 96 97 |
# File 'lib/utopia/content/link.rb', line 95 def title @info.fetch(:title, @title) end |
#to_anchor(base: nil, content: self.title, builder: nil, **attributes) ⇒ Object Also known as: to_href
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/utopia/content/link.rb', line 99 def to_anchor(base: nil, content: self.title, builder: nil, **attributes) attributes[:class] ||= 'link' Trenni::Builder.fragment(builder) do |builder| if href? attributes[:href] ||= relative_href(base) attributes[:target] ||= @info[:target] builder.inline('a', attributes) do builder.text(content) end else builder.inline('span', attributes) do builder.text(content) end end end end |
#to_s ⇒ Object
120 121 122 |
# File 'lib/utopia/content/link.rb', line 120 def to_s "\#<#{self.class}(#{self.kind}) title=#{title.inspect} href=#{href.inspect}>" end |