Class: Uniword::Hyperlink
- Inherits:
-
Object
- Object
- Uniword::Hyperlink
- Defined in:
- lib/uniword/hyperlink.rb
Overview
Hyperlink convenience wrapper for external and internal links.
Converts to Wordprocessingml::Hyperlink model objects. For external links, properly assigns an rId that references a relationship in document.xml.rels — not the raw URL string.
Constant Summary collapse
- REL_TYPE =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
Instance Attribute Summary collapse
-
#anchor ⇒ Object
readonly
Returns the value of attribute anchor.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#tooltip ⇒ Object
readonly
Returns the value of attribute tooltip.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #external? ⇒ Boolean
-
#initialize(url: nil, anchor: nil, text: nil, tooltip: nil) ⇒ Hyperlink
constructor
A new instance of Hyperlink.
- #internal? ⇒ Boolean
- #to_h ⇒ Object
-
#to_model(allocator: nil) ⇒ Wordprocessingml::Hyperlink
Convert to the underlying Wordprocessingml::Hyperlink model.
Constructor Details
#initialize(url: nil, anchor: nil, text: nil, tooltip: nil) ⇒ Hyperlink
Returns a new instance of Hyperlink.
22 23 24 25 26 27 |
# File 'lib/uniword/hyperlink.rb', line 22 def initialize(url: nil, anchor: nil, text: nil, tooltip: nil) @url = url @anchor = anchor @text = text @tooltip = tooltip end |
Instance Attribute Details
#anchor ⇒ Object (readonly)
Returns the value of attribute anchor.
20 21 22 |
# File 'lib/uniword/hyperlink.rb', line 20 def anchor @anchor end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
20 21 22 |
# File 'lib/uniword/hyperlink.rb', line 20 def text @text end |
#tooltip ⇒ Object (readonly)
Returns the value of attribute tooltip.
20 21 22 |
# File 'lib/uniword/hyperlink.rb', line 20 def tooltip @tooltip end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
20 21 22 |
# File 'lib/uniword/hyperlink.rb', line 20 def url @url end |
Instance Method Details
#external? ⇒ Boolean
29 30 31 |
# File 'lib/uniword/hyperlink.rb', line 29 def external? !url.nil? end |
#internal? ⇒ Boolean
33 34 35 |
# File 'lib/uniword/hyperlink.rb', line 33 def internal? !anchor.nil? end |
#to_h ⇒ Object
73 74 75 |
# File 'lib/uniword/hyperlink.rb', line 73 def to_h { url: url, anchor: anchor, text: text, tooltip: tooltip } end |
#to_model(allocator: nil) ⇒ Wordprocessingml::Hyperlink
Convert to the underlying Wordprocessingml::Hyperlink model.
When an allocator is provided, external links get a proper rId registered as a hyperlink relationship. Without an allocator, falls back to setting id to the raw URL (legacy behavior).
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/uniword/hyperlink.rb', line 45 def to_model(allocator: nil) model = Wordprocessingml::Hyperlink.new if url if allocator r_id = allocator.alloc_rid( target: url, type: REL_TYPE, target_mode: "External", ) model.id = r_id else model.id = url end end model.anchor = anchor if anchor model.tooltip = tooltip if tooltip if text run = Wordprocessingml::Run.new run.text = Wordprocessingml::Text.cast(text) model.runs << run end model end |