Class: Ruby_Ab101

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

Instance Method Summary collapse

Constructor Details

#initialize(qb_txt, ab_xml = nil, filepath: '.', debug: false) ⇒ Ruby_Ab101

Returns a new instance of Ruby_Ab101.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ruby_qb101.rb', line 72

def initialize(qb_txt, ab_xml=nil, filepath: '.', debug: false)
  
  @debug = debug
  
  @qb = Ruby_Qb101.new(qb_txt)
  
  @dx = Dynarex.new('book[title, tags]/item(question, answer)')
  @dx.title = @qb.title
  @dx.tags = @qb.tags
  
  @qb.questions.each {|q| @dx.create({question: q}) }
  
  if ab_xml then
    
    dx = Dynarex.new(ab_xml)
    
    # using each question, find the associated answer and 
    # add it the main record
    
    @qb.questions.each do |q|
      
      found = dx.find_by_question q
      
      if found then
        rx = @dx.find_by_question q
        rx.answer = found.answer 
      end        
      
    end
    
    @dx.save ab_xml
    
  end
  
end

Instance Method Details

#save(filename) ⇒ Object



108
109
110
# File 'lib/ruby_qb101.rb', line 108

def save(filename)
  @dx.save filename
end

#to_htmlObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ruby_qb101.rb', line 112

def to_html()

  doc = Rexle.new('<body>' + @qb.to_html.gsub(/<p>[^>]+>/,'<div>\0</div>') \
                  + '</body>')
  answers = @dx.all.map(&:answer)
  
  puts 'answers: ' + answers.inspect if @debug
  
  doc.root.xpath('//p').each.with_index do |para, i|
    e = Rexle::Element.new('p', attributes: {class: 'answer'}).add_text answers[i]
    para.insert_after e
  end
  
  doc.root.xml pretty: true
end

#to_xmlObject



128
129
130
# File 'lib/ruby_qb101.rb', line 128

def to_xml()
  @dx.to_xml(pretty: true)
end