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.



18
19
20
21
22
23
24
# File 'lib/duxml/doc.rb', line 18

def initialize(prolog={})
  super(prolog)
  self[:version] ||= '1.0'
  @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

#metaObject (readonly)

Returns the value of attribute meta.



16
17
18
# File 'lib/duxml/doc.rb', line 16

def meta
  @meta
end

#pathObject

Returns the value of attribute path.



16
17
18
# File 'lib/duxml/doc.rb', line 16

def path
  @path
end

Instance Method Details

#<<(obj) ⇒ Object



84
85
86
87
88
# File 'lib/duxml/doc.rb', line 84

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’



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

def description
  'document'
end

#grammarObject

shortcut method @see Meta#grammar



55
56
57
# File 'lib/duxml/doc.rb', line 55

def grammar
  meta.grammar
end

#grammar=(grammar_or_file) ⇒ Object

shortcut method @see Meta#grammar=



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

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

#historyObject

shortcut method @see Meta#history



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

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



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/duxml/doc.rb', line 42

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



36
37
38
# File 'lib/duxml/doc.rb', line 36

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



76
77
78
79
80
81
82
# File 'lib/duxml/doc.rb', line 76

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