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, path, info = nil) ⇒ Link

Returns a new instance of Link.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/utopia/content/link.rb', line 33

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
		# raise ArgumentError unless path.last.start_with? INDEX
		
		@name = path.dirname.last
		@locale = path.last.split('.', 2)[1]
		@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

#infoObject (readonly)

Returns the value of attribute info.



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

def info
  @info
end

#kindObject (readonly)

Returns the value of attribute kind.



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

def kind
  @kind
end

#localeObject (readonly)

Returns the value of attribute locale.



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

def locale
  @locale
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



72
73
74
# File 'lib/utopia/content/link.rb', line 72

def path
  @path
end

Instance Method Details

#==(other) ⇒ Object



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

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.



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

def [] key
	@info[key]
end

#default_locale?Boolean

Returns:

  • (Boolean)


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

def default_locale?
	@locale == nil
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

#href?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/utopia/content/link.rb', line 76

def href?
	!!href
end

#relative_href(base = nil) ⇒ Object



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

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

#titleObject



88
89
90
# File 'lib/utopia/content/link.rb', line 88

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

#to_anchor(**options) ⇒ Object Also known as: to_href



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

def to_anchor(**options)
	Trenni::Builder.fragment(options[:builder]) do |builder|
		if href?
			a_attributes = {
				:class => options.fetch(:class, 'link'),
				:href => relative_href(options[:base]),
				:target => options.fetch(:target, @info[:target])
			}
			
			builder.inline('a', a_attributes) do
				builder.text(options[:content] || title)
			end
		else
			builder.inline('span', class: options.fetch(:class, 'link')) do
				builder.text(options[:content] || title)
			end
		end
	end
end

#to_sObject



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

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