Class: Utopia::Link

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

Constant Summary collapse

XNODE_EXT =
".xnode"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Link.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/utopia/link.rb', line 23

def initialize(kind, path, info = nil)
	@kind = kind

	case @kind
	when :file
		@name = path.basename(XNODE_EXT)
		@path = path
	when :directory
		@name = path.dirname.basename(XNODE_EXT)
		@path = path
	when :virtual
		@name = path.to_s
		@path = nil
	end

	@components = @name.split(".")
	@locale = components[1..-1].join(".")
	@title = components[0]

	if info
		@info = info.symbolize_keys
	else
		@info = {}
	end
end

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



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

def components
  @components
end

#infoObject (readonly)

Returns the value of attribute info.



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

def info
  @info
end

#kindObject (readonly)

Returns the value of attribute kind.



49
50
51
# File 'lib/utopia/link.rb', line 49

def kind
  @kind
end

#localeObject (readonly)

Returns the value of attribute locale.



52
53
54
# File 'lib/utopia/link.rb', line 52

def locale
  @locale
end

#nameObject (readonly)

Returns the value of attribute name.



50
51
52
# File 'lib/utopia/link.rb', line 50

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



51
52
53
# File 'lib/utopia/link.rb', line 51

def path
  @path
end

Instance Method Details

#==(other) ⇒ Object



108
109
110
# File 'lib/utopia/link.rb', line 108

def == other
	return other && kind == other.kind && name == other.name && path == other.path
end

#[](key) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/utopia/link.rb', line 56

def [] (key)
	if key == :title
		return @title
	end
	
	return @info[key]
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
106
# File 'lib/utopia/link.rb', line 97

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

#external?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/utopia/link.rb', line 82

def external?
	@info.key? :uri
end

#hrefObject



64
65
66
67
68
69
70
71
72
# File 'lib/utopia/link.rb', line 64

def href
	if @info[:uri]
		return @info[:uri]
	elsif @path
		return @path.to_s
	else
		"\#"
	end
end

#href?Boolean

Returns:

  • (Boolean)


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

def href?
	return href != "\#"
end

#titleObject



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

def title
	@info[:title] || @title.to_title
end

#to_href(options = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/utopia/link.rb', line 86

def to_href(options = {})
	options[:content] ||= title
	options[:class] ||= "link"
	
	if href == "\#"
		"<span class=#{options[:class].dump}>#{options[:content].to_html}</span>"
	else
		"<a class=#{options[:class].dump} href=\"#{href.to_html}\">#{options[:content].to_html}</a>"
	end
end