Class: Utopia::Content::Link

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

Overview

Represents a link to some content with associated metadata.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, name, locale, path, info, title = nil) ⇒ Link

Returns a new instance of Link.

Parameters:

  • kind (Symbol)

    the kind 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

#infoObject (readonly)

Returns the value of attribute info.



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

def info
  @info
end

#kindObject (readonly)

Returns the value of attribute kind.



64
65
66
# File 'lib/utopia/content/link.rb', line 64

def kind
  @kind
end

#localeObject (readonly)

Returns the value of attribute locale.



68
69
70
# File 'lib/utopia/content/link.rb', line 68

def locale
  @locale
end

#nameObject (readonly)

Returns the value of attribute name.



65
66
67
# File 'lib/utopia/content/link.rb', line 65

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



66
67
68
# File 'lib/utopia/content/link.rb', line 66

def path
  @path
end

Instance Method Details

#==(other) ⇒ Object



123
124
125
# File 'lib/utopia/content/link.rb', line 123

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.



60
61
62
# File 'lib/utopia/content/link.rb', line 60

def [] key
	@info[key]
end

#default_locale?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/utopia/content/link.rb', line 127

def default_locale?
	@locale == nil
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/utopia/content/link.rb', line 119

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

#hrefObject



53
54
55
56
57
# File 'lib/utopia/content/link.rb', line 53

def href
	@href ||= @info.fetch(:uri) do
		(@path.dirname + @path.basename).to_s if @path
	end
end

#href?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/utopia/content/link.rb', line 70

def href?
	!!href
end

#index?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/utopia/content/link.rb', line 74

def index?
	@kind == :index
end

#keyObject



45
46
47
48
49
50
51
# File 'lib/utopia/content/link.rb', line 45

def key
	if locale
		"#{@path.last}.#{@locale}"
	else
		@path.last
	end
end

#relative_href(base = nil) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/utopia/content/link.rb', line 82

def relative_href(base = nil)
	if base and href.start_with? '/'
		Path.shortest_path(href, base)
	else
		href
	end
end

#titleObject



90
91
92
# File 'lib/utopia/content/link.rb', line 90

def title
	@info.fetch(:title, @title)
end

#to_anchor(base: nil, content: self.title, builder: nil, **attributes) ⇒ Object Also known as: to_href



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/utopia/content/link.rb', line 94

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_sObject



115
116
117
# File 'lib/utopia/content/link.rb', line 115

def to_s
	"\#<#{self.class}(#{self.kind}) title=#{title.inspect} href=#{href.inspect}>"
end

#virtual?Boolean

Returns:

  • (Boolean)


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

def virtual?
	@kind == :virtual
end