Class: Duxml::Doc

Inherits:
Ox::Document
  • Object
show all
Includes:
ElementGuts
Defined in:
lib/duxml/doc.rb

Instance Attribute Summary collapse

Attributes included from Reportable

#observer_peers

Attributes included from Duxml

#doc

Attributes included from Saxer

#io

Instance Method Summary collapse

Methods included from ElementGuts

#[], #[]=, #abstract?, #add, #dclone, #delete, #each, #inspect, #name_space, #sclone, #set_doc!, #stub, #text?, #traverse

Methods included from LazyOx

#method_missing

Methods included from Reportable

#add_observer

Methods included from Duxml

#load, #log, #save, #validate

Methods included from Saxer

#sax

Constructor Details

#initialize(prolog = {}) ⇒ Doc

Returns a new instance of Doc.



21
22
23
24
25
26
27
28
# File 'lib/duxml/doc.rb', line 21

def initialize(prolog={})
  super(prolog)
  self[:version] ||= '1.0'
  @id_hash = {}
  @meta = MetaClass.new
  @nodes = NodeSet.new(self)
  add_observer meta.history
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Duxml::LazyOx

Instance Attribute Details

#id_hashObject (readonly)

Returns the value of attribute id_hash.



19
20
21
# File 'lib/duxml/doc.rb', line 19

def id_hash
  @id_hash
end

#metaObject (readonly)

Returns the value of attribute meta.



19
20
21
# File 'lib/duxml/doc.rb', line 19

def meta
  @meta
end

#pathObject

Returns the value of attribute path.



19
20
21
# File 'lib/duxml/doc.rb', line 19

def path
  @path
end

Instance Method Details

#<<(obj) ⇒ Object



88
89
90
91
92
# File 'lib/duxml/doc.rb', line 88

def <<(obj)
  super(obj)
  obj.set_doc! self
  self
end

#descriptionString

Returns one word description of what this object is: ‘document’.

Returns:

  • (String)

    one word description of what this object is: ‘document’



74
75
76
# File 'lib/duxml/doc.rb', line 74

def description
  'document'
end

#find_by_id(id) ⇒ Element, NilClass

Returns found element or nil if not found.

Parameters:

  • id (String, Symbol)

    document-unique id attribute value

Returns:

  • (Element, NilClass)

    found element or nil if not found



96
97
98
99
100
101
102
103
104
105
# File 'lib/duxml/doc.rb', line 96

def find_by_id(id)
  id_str = id.to_s
  return @id_hash[id_str] if @id_hash[id_str]
  root.traverse do |node|
    if node.respond_to?(:nodes) and node[:id] == id_str
      return @id_hash[id_str] = node
    end
  end
  nil
end

#grammarObject

shortcut method @see Meta#grammar



59
60
61
# File 'lib/duxml/doc.rb', line 59

def grammar
  meta.grammar
end

#grammar=(grammar_or_file) ⇒ Object

shortcut method @see Meta#grammar=



64
65
66
# File 'lib/duxml/doc.rb', line 64

def grammar=(grammar_or_file)
  meta.grammar = grammar_or_file
end

#historyObject

shortcut method @see Meta#history



69
70
71
# File 'lib/duxml/doc.rb', line 69

def history
  meta.history
end

#set_meta(path_or_obj = nil) ⇒ Doc

Returns self.

Parameters:

  • path_or_obj (String, MetaClass) (defaults to: nil)

    metadata object itself or path of metadata for this file; if none given, saves existing metadata to file using @path

Returns:

  • (Doc)

    self



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/duxml/doc.rb', line 46

def set_meta(path_or_obj=nil)
  @meta = case path_or_obj
            when MetaClass, Element then path_or_obj
            when String && File.exists?(path_or_obj)
              Ox.parse_obj(path_or_obj)
            else
              File.write(Meta.meta_path(path), Ox.dump(meta)) if path
              meta
          end
  self
end

#to_sString

Returns summary of XML document as Ruby object and description of root element.

Returns:

  • (String)

    summary of XML document as Ruby object and description of root element



40
41
42
# File 'lib/duxml/doc.rb', line 40

def to_s
  "#<#{self.class.to_s} @object_id='#{object_id}' @root='#{root.nil? ? '' : root.description}'>"
end

#write_to(path) ⇒ Doc

Returns self after writing contents to file

Parameters:

  • path (String)

    document’s file path

Returns:

  • (Doc)

    returns self after writing contents to file



80
81
82
83
84
85
86
# File 'lib/duxml/doc.rb', line 80

def write_to(path)
  s = attributes.collect do |k, v| %( #{k}="#{v}") end.join
  File.write(path, %(<?xml #{s}?>\n) + root.to_s)
  x = meta.xml
  File.write(Meta.meta_path(path), meta.xml.to_s)
  self
end