Class: Glyph::Bookmark
- Inherits:
-
Object
- Object
- Glyph::Bookmark
- Defined in:
- lib/glyph/bookmark.rb
Overview
This class is used to model bookmarks within a Glyph document. It contains methods to store bookmark data and resolve link paths automatically.
Direct Known Subclasses
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#==(b) ⇒ Object
Returns true if the two bookmarks have the same ID and file.
-
#code ⇒ Object
Returns the bookmark ID.
-
#initialize(hash) ⇒ Bookmark
constructor
Initializes a bookmark object from a hash containing bookmark data.
-
#link(file = nil) ⇒ String
Returns the appropriate link path to the bookmark, depending on the specified file.
-
#to_s ⇒ String
(also: #to_str)
Returns the bookmark id.
Constructor Details
#initialize(hash) ⇒ Bookmark
Initializes a bookmark object from a hash containing bookmark data.
20 21 22 23 24 25 26 27 |
# File 'lib/glyph/bookmark.rb', line 20 def initialize(hash) @id = hash[:id].to_sym rescue nil @file = hash[:file] @title = hash[:title] @definition = hash[:definition] raise RuntimeError, "Bookmark ID not specified" unless @id raise RuntimeError, "Invalid bookmark ID: #{@id}" unless check_id end |
Instance Attribute Details
#definition ⇒ Object
10 11 12 |
# File 'lib/glyph/bookmark.rb', line 10 def definition @definition end |
#file ⇒ Object
10 11 12 |
# File 'lib/glyph/bookmark.rb', line 10 def file @file end |
#title ⇒ Object
10 11 12 |
# File 'lib/glyph/bookmark.rb', line 10 def title @title end |
Instance Method Details
#==(b) ⇒ Object
Returns true if the two bookmarks have the same ID and file
37 38 39 40 |
# File 'lib/glyph/bookmark.rb', line 37 def ==(b) raise RuntimeError, "#{b.inspect} is not a bookmark" unless b.is_a? Glyph::Bookmark self.code == b.code && self.file == b.file end |
#code ⇒ Object
Returns the bookmark ID.
30 31 32 |
# File 'lib/glyph/bookmark.rb', line 30 def code @id end |
#link(file = nil) ⇒ String
Returns the appropriate link path to the bookmark, depending on the specified file
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/glyph/bookmark.rb', line 45 def link(file=nil) if multiple_output_files? then dest_file = @file.to_s dest_file += '.glyph' unless dest_file.match /\..+$/ dest_file.gsub!(/^text\//, '') unless Glyph.lite? external_file = dest_file.to_s.gsub(/\..+$/, Glyph["output.#{Glyph['document.output']}.extension"]) f = (file.blank? || file != @file) ? "#{Glyph["output.#{Glyph['document.output']}.base"]}#{external_file}" : "" "#{f}##{@id}" else "##{@id}" end end |
#to_s ⇒ String Also known as: to_str
Returns the bookmark id
60 61 62 |
# File 'lib/glyph/bookmark.rb', line 60 def to_s @id.to_s end |