Class: XBEL

Inherits:
Nokogiri::XML::Document
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/xbel.rb

Constant Summary collapse

TEMPLATE =
%Q'<!DOCTYPE xbel PUBLIC "+//IDN python.org//DTD XML Bookmark Exchange Language 1.0//EN//XML" "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd"><xbel version="1.0" id="0"></xbel>'
DIV_ID_ER =
'_'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ XBEL

Calls Nokogiri#initialize and decorates XBEL nodes.


35
36
37
38
39
40
41
# File 'lib/xbel.rb', line 35

def initialize(*args)
  super
  decorators(Nokogiri::XML::Node) << Nokogiri::Decorators::XBEL
  decorate!

  @div_id_er = DIV_ID_ER
end

Instance Attribute Details

#div_id_erObject

Returns the value of attribute div_id_er


14
15
16
# File 'lib/xbel.rb', line 14

def div_id_er
  @div_id_er
end

Class Method Details

.new(attributes = {}) ⇒ Object

Returns an instance of XBEL.


20
21
22
23
24
25
# File 'lib/xbel.rb', line 20

def self.new(attributes = {})
  xbel = parse TEMPLATE
  xbel.attributes = attributes

  xbel
end

.open(path) {|XBEL| ... } ⇒ Object

Reads file at path into parse.

Yields:

See Also:

  • Nokogiri.parse

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

def self.open(path, &block)
  parse File.read(path.to_s), &block
end

Instance Method Details

#[](id) ⇒ Object

Returns node with given id.


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

def [](id)
  root.at("//*[@id='#{ id }']")
end

#attributes=(attributes) ⇒ Object


48
49
50
51
52
53
# File 'lib/xbel.rb', line 48

def attributes=(attributes)
  version = attributes.delete(:version) and self.version = version
  div_id_er = attributes.delete(:div_id_er) and self.div_id_er = div_id_er

  root.attributes = attributes
end

#versionObject

Returns an array of version numbers.


56
57
58
# File 'lib/xbel.rb', line 56

def version
  root.attribute('version').value.split('.').map { |n| n.to_i }
end

#version=(*numbers) ⇒ Object

Sets version numbers.


60
61
62
# File 'lib/xbel.rb', line 60

def version=(*numbers)
  root.attribute('version').value = numbers * '.'
end