Class: Extreml
- Inherits:
-
Object
- Object
- Extreml
- Defined in:
- lib/extreml.rb
Defined Under Namespace
Classes: TypeElement, XmlHeader
Instance Method Summary collapse
-
#document ⇒ Object
Expose the entire document.
-
#initialize(xml_file = nil, warnings: true, xml_header: nil) ⇒ Extreml
constructor
A new instance of Extreml.
-
#to_hash ⇒ Object
Returns the document in an Hash form.
- #to_json ⇒ Object
-
#to_xml ⇒ Object
Retrurns the document in XML format.
-
#tree(attributes: false) ⇒ Object
Print the entire document tree.
-
#update_content(key, content) ⇒ Object
update content from a subsequent element.
Constructor Details
#initialize(xml_file = nil, warnings: true, xml_header: nil) ⇒ Extreml
Returns a new instance of Extreml.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/extreml.rb', line 26 def initialize xml_file = nil, warnings: true, xml_header: nil # Warnings flag @warnings = warnings @header = XmlHeader.new xml_header if xml_file.nil? @document = Array.new else if !File.file? xml_file raise "Error: file #{xml_file} not found." else # Read file xml = File.read xml_file @body = Hash.new # Get xml header informations header = xml[/^\<\?xml (.*)\?\>/] if header.nil? puts "Warning: #{xml_file}: xml header missing." if @warnings define_singleton_method :header do return nil end else h = header.scan /([\w\?\<]*)=["|']([^'"]*)["|']/ @xml_header = XmlHeader.new header define_singleton_method :header do return @xml_header end end end # Read document doc = xml.match /(?:\<\?xml .*?(?: ?\?\>))?[\t\n\r\f ]*(.*)/m @document = unpack doc[1] end end |
Instance Method Details
#document ⇒ Object
Expose the entire document
95 96 97 |
# File 'lib/extreml.rb', line 95 def document return TypeElement.new({name: 'document', content: @document}, main_element: self) end |
#to_hash ⇒ Object
Returns the document in an Hash form
73 74 75 |
# File 'lib/extreml.rb', line 73 def to_hash return self.document.to_hash end |
#to_json ⇒ Object
77 78 79 |
# File 'lib/extreml.rb', line 77 def to_json return self.document.to_json end |
#to_xml ⇒ Object
Retrurns the document in XML format
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/extreml.rb', line 82 def to_xml if @xml_header.nil? xml = '' else xml = @xml_header.to_xml + "\n" end self.document.__types.each do |t| xml += self.document.send(t).to_xml end return xml end |
#tree(attributes: false) ⇒ Object
Print the entire document tree. For debug purposes
130 131 132 |
# File 'lib/extreml.rb', line 130 def tree attributes: false self.document.__tree attributes: attributes end |
#update_content(key, content) ⇒ Object
update content from a subsequent element
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/extreml.rb', line 100 def update_content key, content # byebug # @document = content.to_hash # upd = false # @document.each do |e| # byebug # if e[:name] == key # upd = true # content.each do |k,v| # e[k] = v # end # break # end # end # # unless upd # @document << content # end # if @document[key].nil? # @document[key] = content # else # unless @document[key].class = Array # @document[key] = [@document[key]] # end # @document[key] << content.to_hash # end end |