Class: SectionX

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSectionX

Returns a new instance of SectionX.



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

def initialize()
end

Instance Attribute Details

#summaryObject (readonly)

Returns the value of attribute summary.



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

def summary
  @summary
end

Instance Method Details

#import(raw_s) ⇒ Object



17
18
19
20
21
22
23
24
25
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sectionx.rb', line 17

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

  summary = a.shift.flatten(1)
  summary.shift

  section1 = a.shift.flatten(1)
  section1.shift

  xml = RexleBuilder.new

  a2 = xml.send(id) do 
    xml.summary do
      summary.each do |raw_x| 
        label, value = raw_x.split(/\s*:\s*/,2)
        xml.send(label.downcase.gsub(/\s+/,'_'), value)
      end
      xml.recordx_type 'sectionx'
    end
    xml.sections do
      xml.section do
        xml.summary do 
          section1.each do |raw_x|
            label, value = raw_x.split(/\s*:\s*/,2)
            xml.send(label.downcase.gsub(/\s+/,'_'), value)
          end
        end
        xml.sections
      end
      a.each do |section_name, raw_rows|
        xml.section({title: section_name}) do
          xml.summary do
            raw_rows.each do |raw_x|
              label, value = raw_x.split(/\s*:\s*/,2)
              xml.send(label.downcase.gsub(/\s+/,'_'), value)
            end
          end
          xml.sections
        end
      end 
    end
  end

  @doc = doc = Rexle.new a2
  @summary = doc.root.xpath('summary/*')\
                         .inject({}) {|r,x| r.merge(x.name.to_sym => x.text)}
  
  summary_methods = (@summary.keys - self.public_methods)
  
  summary_methods.each do |x|
    
    instance_eval "
    
      def #{x.to_sym}()
        @summary[:#{x}]
      end
    
      def #{x.to_s}=(v)
        @summary[:#{x}] = v
        @doc.root.element('summary/#{x.to_s}').text = v
      end
      "      
  end    
  self
end

#recordx_typeObject



93
94
95
# File 'lib/sectionx.rb', line 93

def recordx_type()
  @summary[:recordx_type]
end

#to_xml(options = {}) ⇒ Object



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

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

#xpath(x) ⇒ Object



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

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