Class: SectionX
- Inherits:
-
Object
- Object
- SectionX
- Defined in:
- lib/sectionx.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#sections ⇒ Object
readonly
Returns the value of attribute sections.
-
#summary ⇒ Object
readonly
Returns the value of attribute summary.
Instance Method Summary collapse
- #element(s) ⇒ Object
- #fetch(rxpath) ⇒ Object
- #import(raw_s) ⇒ Object
-
#initialize(x = nil, debug: false) ⇒ SectionX
constructor
A new instance of SectionX.
- #new_section(raw_title) ⇒ Object
- #recordx_type ⇒ Object
- #save(filepath) ⇒ Object
- #text(s) ⇒ Object
- #to_xml(options = {}) ⇒ Object
- #update(id = nil, h = {}) ⇒ Object
- #xpath(x) ⇒ Object
- #xslt=(value) ⇒ Object
Constructor Details
#initialize(x = nil, debug: false) ⇒ SectionX
Returns a new instance of SectionX.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/sectionx.rb', line 16 def initialize(x=nil, debug: false) @debug = debug puts ('initialize() x: ' + x.inspect).debug if @debug @doc = if x.is_a? String then buffer, _ = RXFHelper.read x Rexle.new buffer elsif x.is_a? Rexle::Element then x end if @doc then @attributes, @summary, @sections = parse_root_node @doc.root end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
14 15 16 |
# File 'lib/sectionx.rb', line 14 def attributes @attributes end |
#sections ⇒ Object (readonly)
Returns the value of attribute sections.
14 15 16 |
# File 'lib/sectionx.rb', line 14 def sections @sections end |
#summary ⇒ Object (readonly)
Returns the value of attribute summary.
14 15 16 |
# File 'lib/sectionx.rb', line 14 def summary @summary end |
Instance Method Details
#element(s) ⇒ Object
32 33 34 |
# File 'lib/sectionx.rb', line 32 def element(s) @doc.root.element(s) end |
#fetch(rxpath) ⇒ Object
36 37 38 39 |
# File 'lib/sectionx.rb', line 36 def fetch(rxpath) list = rxpath.split('/').map {|x| x.gsub(/\s+/,'_').downcase.to_sym} find_section(list) end |
#import(raw_s) ⇒ Object
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/sectionx.rb', line 42 def import(raw_s) raw_buffer, type = RXFHelper.read(raw_s) lines = raw_buffer.lines header = lines.shift id = header[/id=["']([^"']+)/,1] || 'sectionx' body, summary = lines.join.strip.split(/^----*$/).reverse nested = indent_heading("# summary\n%s\n# begin\n%s" % [summary,\ body.strip]) a = LineTree.new(nested).to_a raw_summary = a.shift raw_summary.shift # removes the redundant summary label section1 = a.shift # get the 1st section section1[0] = nil # nilify the section heading 'begin' xml = RexleBuilder.new a2 = xml.send(id) do xml.summary do build_rows xml, raw_summary xml.recordx_type 'sectionx' end xml.sections do build_section xml, section1 a.each {|raw_rows| build_section xml, raw_rows } end end @doc = Rexle.new a2 @attributes, @summary, @sections = parse_root_node(@doc.root) self end |
#new_section(raw_title) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/sectionx.rb', line 86 def new_section(raw_title) doc = Rexle.new("<section title='#{raw_title}'><summary/>" + "<sections/></section>") @doc.root.element('sections').add doc.root title = raw_title.gsub(/\s+/,'_').downcase.to_sym @sections[title] = SectionX.new \ @doc.root.element('sections').elements.last, debug: @debug define_singleton_method(title) { self.sections[title] } end |
#recordx_type ⇒ Object
99 100 101 |
# File 'lib/sectionx.rb', line 99 def recordx_type() @summary[:recordx_type] end |
#save(filepath) ⇒ Object
103 104 105 |
# File 'lib/sectionx.rb', line 103 def save(filepath) File.write filepath, @doc.xml(pretty: true) end |
#text(s) ⇒ Object
107 108 109 |
# File 'lib/sectionx.rb', line 107 def text(s) @doc.root.element("//%s/text()" % s.to_s).unescape end |
#to_xml(options = {}) ⇒ Object
111 112 113 |
# File 'lib/sectionx.rb', line 111 def to_xml(={}) @doc.xml() end |
#update(id = nil, h = {}) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/sectionx.rb', line 115 def update(id=nil, h={}) puts 'inside update h: ' + h.inspect if @debug summary = @doc.root.element('summary') name = h.keys.first.to_s puts 'xpath: ' + name.inspect if @debug e = summary.element(name) puts 'e: ' + e.inspect if @debug if e then e.text = h.values.first else summary.add Rexle::Element.new(name).add_text(h.values.first) end puts '@doc: ' + @doc.xml if @debug end |
#xpath(x) ⇒ Object
133 134 135 |
# File 'lib/sectionx.rb', line 133 def xpath(x) @doc.root.xpath(x) end |
#xslt=(value) ⇒ Object
137 138 139 140 141 |
# File 'lib/sectionx.rb', line 137 def xslt=(value) self.summary.merge!({xslt: value}) @xslt = value end |