Class: Uniword::Hyperlink

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/hyperlink.rb

Overview

Hyperlink convenience wrapper for external and internal links.

This provides a simplified API over the underlying Wordprocessingml::Hyperlink model.

Examples:

External hyperlink

link = Hyperlink.new(url: 'https://example.com', text: 'Click here')
link.external?  # => true
link.url        # => 'https://example.com'

Internal hyperlink (bookmark)

link = Hyperlink.new(anchor: 'section1', text: 'Go to section')
link.internal?  # => true
link.anchor    # => 'section1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url: nil, anchor: nil, text: nil, tooltip: nil) ⇒ Hyperlink

Returns a new instance of Hyperlink.

Parameters:

  • url (String, nil) (defaults to: nil)

    URL for external links

  • anchor (String, nil) (defaults to: nil)

    Anchor for internal links

  • text (String) (defaults to: nil)

    Display text

  • tooltip (String, nil) (defaults to: nil)

    Optional tooltip



36
37
38
39
40
41
# File 'lib/uniword/hyperlink.rb', line 36

def initialize(url: nil, anchor: nil, text: nil, tooltip: nil)
  @url = url
  @anchor = anchor
  @text = text
  @tooltip = tooltip
end

Instance Attribute Details

#anchorString? (readonly)

Returns Anchor/bookmark name for internal links.

Returns:

  • (String, nil)

    Anchor/bookmark name for internal links



24
25
26
# File 'lib/uniword/hyperlink.rb', line 24

def anchor
  @anchor
end

#textString (readonly)

Returns Display text.

Returns:

  • (String)

    Display text



27
28
29
# File 'lib/uniword/hyperlink.rb', line 27

def text
  @text
end

#tooltipString? (readonly)

Returns Tooltip text.

Returns:

  • (String, nil)

    Tooltip text



30
31
32
# File 'lib/uniword/hyperlink.rb', line 30

def tooltip
  @tooltip
end

#urlString? (readonly)

Returns URL for external links.

Returns:

  • (String, nil)

    URL for external links



21
22
23
# File 'lib/uniword/hyperlink.rb', line 21

def url
  @url
end

Instance Method Details

#external?Boolean

Returns True if this is an external URL link.

Returns:

  • (Boolean)

    True if this is an external URL link



44
45
46
# File 'lib/uniword/hyperlink.rb', line 44

def external?
  !url.nil?
end

#internal?Boolean

Returns True if this is an internal anchor link.

Returns:

  • (Boolean)

    True if this is an internal anchor link



49
50
51
# File 'lib/uniword/hyperlink.rb', line 49

def internal?
  !anchor.nil?
end

#to_hHash

Returns Hash representation.

Returns:

  • (Hash)

    Hash representation



72
73
74
# File 'lib/uniword/hyperlink.rb', line 72

def to_h
  { url: url, anchor: anchor, text: text, tooltip: tooltip }
end

#to_modelWordprocessingml::Hyperlink

Convert to the underlying Wordprocessingml::Hyperlink model

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/uniword/hyperlink.rb', line 56

def to_model
  model = Wordprocessingml::Hyperlink.new
  model.id = url if url
  model.anchor = anchor if anchor
  model.tooltip = tooltip if tooltip

  if text
    run = Wordprocessingml::Run.new
    run.text = Wordprocessingml::Text.new(content: text)
    model.runs << run
  end

  model
end