Class: SectionX

Inherits:
Object
  • Object
show all
Defined in:
lib/sectionx.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = nil, debug: false) ⇒ SectionX

Returns a new instance of SectionX.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sectionx.rb', line 15

def initialize(x=nil, debug: false)
  
  @debug = 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

#attributesObject (readonly)

Returns the value of attribute attributes.



13
14
15
# File 'lib/sectionx.rb', line 13

def attributes
  @attributes
end

#sectionsObject (readonly)

Returns the value of attribute sections.



13
14
15
# File 'lib/sectionx.rb', line 13

def sections
  @sections
end

#summaryObject (readonly)

Returns the value of attribute summary.



13
14
15
# File 'lib/sectionx.rb', line 13

def summary
  @summary
end

Instance Method Details

#import(raw_s) ⇒ Object



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
71
72
# File 'lib/sectionx.rb', line 30

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

#recordx_typeObject



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

def recordx_type()
  @summary[:recordx_type]
end

#save(filepath) ⇒ Object



78
79
80
# File 'lib/sectionx.rb', line 78

def save(filepath)
  File.write filepath, @doc.xml(pretty: true)
end

#to_xml(options = {}) ⇒ Object



82
83
84
# File 'lib/sectionx.rb', line 82

def to_xml(options={})
  @doc.xml(options)
end

#update(id = nil, h = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/sectionx.rb', line 86

def update(id=nil, h={})
  
  puts 'inside update h: ' + h.inspect if @debug
  xpath = "summary/" + h.keys.first.to_s
  puts 'xpath: ' + xpath.inspect if @debug
  e = @doc.root.element(xpath)
  puts 'e: ' + e.inspect if @debug
  e.text =  h.values.first if e
  puts '@doc: ' + @doc.xml if @debug
end

#xpath(x) ⇒ Object



97
98
99
# File 'lib/sectionx.rb', line 97

def xpath(x)
  @doc.root.xpath(x)
end

#xslt=(value) ⇒ Object



101
102
103
104
105
# File 'lib/sectionx.rb', line 101

def xslt=(value)
  
  self.summary.merge!({xslt: value})
  @xslt = value
end