Class: Bookmarks::Document
- Inherits:
-
Object
- Object
- Bookmarks::Document
- Defined in:
- lib/bookmarks/document.rb
Overview
Public: Document is your main interface to the file of bookmarks (called «document»).
Instance Attribute Summary collapse
-
#bookmarks ⇒ Object
readonly
Public: Returns an Array of NetscapeBookmark bookmarks.
-
#bookmarks_format ⇒ Object
readonly
Public: Returns the Symbol format of the document.
-
#document ⇒ Object
readonly
Public: Returns the String document.
-
#total ⇒ Object
readonly
Public: Returns the Integer numbers of bookmarks in the document.
Instance Method Summary collapse
-
#build(&block) ⇒ Object
Public: Build a document, ie build a file of bookmarks.
-
#initialize(format: :netscape) ⇒ Document
constructor
Public: Init a new Document.
-
#parse(file_path) ⇒ Object
Public: Parse a file of bookmarks (netscape format).
Constructor Details
#initialize(format: :netscape) ⇒ Document
18 19 20 21 22 23 24 |
# File 'lib/bookmarks/document.rb', line 18 def initialize format: :netscape @bookmarks_format = format @document = "" @bookmarks = [] @total = 0 @h3_tags = [] end |
Instance Attribute Details
#bookmarks ⇒ Object (readonly)
Public: Returns an Array of NetscapeBookmark bookmarks.
34 35 36 |
# File 'lib/bookmarks/document.rb', line 34 def bookmarks @bookmarks end |
#bookmarks_format ⇒ Object (readonly)
Public: Returns the Symbol format of the document. Currently
there is only one format available: `:netscape`.
28 29 30 |
# File 'lib/bookmarks/document.rb', line 28 def bookmarks_format @bookmarks_format end |
#document ⇒ Object (readonly)
Public: Returns the String document.
31 32 33 |
# File 'lib/bookmarks/document.rb', line 31 def document @document end |
#total ⇒ Object (readonly)
Public: Returns the Integer numbers of bookmarks in the document.
37 38 39 |
# File 'lib/bookmarks/document.rb', line 37 def total @total end |
Instance Method Details
#build(&block) ⇒ Object
Public: Build a document, ie build a file of bookmarks.
block - A block that enumerate all NetscapeBookmark to put
into the document.
Examples
# ary is an array of NetscapeBookmark.
document.build do
ary.each {|e| e }
end
Returns the String document.
52 53 54 55 56 57 58 59 |
# File 'lib/bookmarks/document.rb', line 52 def build &block @document += FIRST_PART block.call.each do |n| @document += n.to_s + "\n" @total += 1 end @document += LAST_PART end |
#parse(file_path) ⇒ Object
Public: Parse a file of bookmarks (netscape format). Bookmarks could then be retrieved with #bookmarks.
file_path - Full String pathname of the file to parse.
Returns the String document (see also #document).
67 68 69 70 |
# File 'lib/bookmarks/document.rb', line 67 def parse file_path File.new(file_path).readlines.each {|line| parse_a_bookmark line } @total = @bookmarks.size end |