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.



20
21
22
23
24
25
26
27
# File 'lib/utopia/content/link.rb', line 20

def initialize(kind, name, locale, path, info, title = nil)
  @kind = kind
  @name = name
  @locale = locale
  @path = Path.create(path)
  @info = info || {}
  @title = XRB::Strings.to_title(title || name)
end

Instance Attribute Details

#infoObject (readonly)

Returns the value of attribute info.



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

def info
  @info
end

#kindObject (readonly)

Returns the value of attribute kind.



58
59
60
# File 'lib/utopia/content/link.rb', line 58

def kind
  @kind
end

#localeObject (readonly)

Returns the value of attribute locale.



62
63
64
# File 'lib/utopia/content/link.rb', line 62

def locale
  @locale
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#==(other) ⇒ Object



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

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.



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

def [] key
  @info[key]
end

#default_locale?Boolean

Returns:

  • (Boolean)


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

def default_locale?
  @locale == nil
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#full_path(root, extension = XNODE_EXTENSION) ⇒ Object



39
40
41
42
43
# File 'lib/utopia/content/link.rb', line 39

def full_path(root, extension = XNODE_EXTENSION)
  if @path&.file?
    File.join(root, @path.dirname, self.key + XNODE_EXTENSION)
  end
end

#hrefObject



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

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

#href?Boolean

Returns:

  • (Boolean)


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

def href?
  !!href
end

#index?Boolean

Returns:

  • (Boolean)


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

def index?
  @kind == :index
end

#keyObject



29
30
31
32
33
34
35
36
37
# File 'lib/utopia/content/link.rb', line 29

def key
  if @path
    if locale
      "#{@path.last}.#{@locale}"
    else
      @path.last
    end
  end
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

#titleObject



84
85
86
# File 'lib/utopia/content/link.rb', line 84

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

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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/utopia/content/link.rb', line 88

def to_anchor(base: nil, content: self.title, builder: nil, **attributes)
  attributes[:class] ||= "link"
  
  XRB::Builder.fragment(builder) do |inner_builder|
    if href?
      attributes[:href] ||= relative_href(base)
      attributes[:target] ||= @info[:target]
      
      inner_builder.inline("a", attributes) do
        inner_builder.text(content)
      end
    else
      inner_builder.inline("span", attributes) do
        inner_builder.text(content)
      end
    end
  end
end

#to_sObject



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

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

#virtual?Boolean

Returns:

  • (Boolean)


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

def virtual?
  @kind == :virtual
end