Class: Utopia::Content::Link
- Inherits:
-
Object
- Object
- Utopia::Content::Link
- Defined in:
- lib/utopia/content/link.rb
Instance Attribute Summary collapse
-
#info ⇒ Object
readonly
Returns the value of attribute info.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#variant ⇒ Object
readonly
Returns the value of attribute variant.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](key) ⇒ Object
- #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_href(options = {}) ⇒ Object
Constructor Details
#initialize(kind, path, info = nil) ⇒ Link
Returns a new instance of Link.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/utopia/content/link.rb', line 30 def initialize(kind, path, info = nil) path = Path.create(path) @info = info || {} @kind = kind case @kind when :file @name, @variant = path.last.split('.', 2) @path = path when :directory # raise ArgumentError unless path.last.start_with? 'index' @name = path.dirname.last @variant = path.last.split('.', 2)[1] @path = path when :virtual @name, @variant = 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.
69 70 71 |
# File 'lib/utopia/content/link.rb', line 69 def info @info end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
66 67 68 |
# File 'lib/utopia/content/link.rb', line 66 def kind @kind end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
67 68 69 |
# File 'lib/utopia/content/link.rb', line 67 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
68 69 70 |
# File 'lib/utopia/content/link.rb', line 68 def path @path end |
#variant ⇒ Object (readonly)
Returns the value of attribute variant.
70 71 72 |
# File 'lib/utopia/content/link.rb', line 70 def variant @variant end |
Instance Method Details
#==(other) ⇒ Object
115 116 117 |
# File 'lib/utopia/content/link.rb', line 115 def == other return other && kind == other.kind && name == other.name && path == other.path end |
#[](key) ⇒ Object
62 63 64 |
# File 'lib/utopia/content/link.rb', line 62 def [] key @info[key] end |
#default_locale? ⇒ Boolean
119 120 121 |
# File 'lib/utopia/content/link.rb', line 119 def default_locale? @locale == nil end |
#eql?(other) ⇒ Boolean
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/utopia/content/link.rb', line 104 def eql? other if other && self.class == other.class return kind.eql?(other.kind) && name.eql?(other.name) && path.eql?(other.path) && info.eql?(other.info) else return false end end |
#href ⇒ Object
56 57 58 59 60 |
# File 'lib/utopia/content/link.rb', line 56 def href @href ||= @info.fetch(:uri) do (@path.dirname + @path.basename.parts[0]).to_s if @path end end |
#href? ⇒ Boolean
72 73 74 |
# File 'lib/utopia/content/link.rb', line 72 def href? !!href end |
#relative_href(base = nil) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/utopia/content/link.rb', line 76 def relative_href(base = nil) if base and href.start_with? '/' Path.shortest_path(href, base) else href end end |
#title ⇒ Object
84 85 86 |
# File 'lib/utopia/content/link.rb', line 84 def title @info.fetch(:title, @title) end |
#to_href(options = {}) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/utopia/content/link.rb', line 88 def to_href( = {}) Trenni::Builder.fragment([:builder]) do |builder| if href? relative_href([:base]) builder.inline('a', class: .fetch(:class, 'link'), href: relative_href([:base])) do builder.text([:content] || title) end else builder.inline('span', class: .fetch(:class, 'link')) do builder.text([:content] || title) end end end end |