Skip to content

Instantly share code, notes, and snippets.

@ocean
Created March 21, 2011 03:52
Show Gist options
  • Save ocean/878985 to your computer and use it in GitHub Desktop.
Save ocean/878985 to your computer and use it in GitHub Desktop.
HALP. I DRYed up the XML-writing "each" loop by abstracting out each bit into it's own mini-function, and now I get an "undefined local variable or method 'xml' for main:Object" error. What's the magickal thing that has to be done to get it working? Or t
def truncate_words(text, length = 6)
words = text.split()
words[0..(length-1)].join(' ')
end
def writeQuestion(questText)
xml.name {
xml.text_ truncate_words(questText.to_s)
}
xml.questiontext(:format => "html") {
xml.text_ questText.to_s
}
end
def writeAnswer(score, answerText, feedback)
xml.answer(:fraction => score) {
xml.text_ answerText.to_s
xml.feedback {
xml.text_ feedback
}
}
end
def writeCategory(categoryText)
xml.question(:type => "category") {
xml.category {
xml.text_ categoryText.to_s
}
}
end
categoryidStored = ''
builder = Nokogiri::XML::Builder.new do |xml|
xml.quiz {
db.exec('select "SLI_QUESTIONS"."QUESTIONID" as "QUESTIONID",
"SLI_QUESTIONS"."QUESTIONTEXT" as "QUESTIONTEXT",
"SLI_QUESTIONS"."QUESTIONCATEGORYID" as "QUESTIONCATEGORYID",
"SLI_QUESTIONCATEGORIES"."QUESTIONCATEGORYNAME" as "QUESTIONCATEGORYNAME",
"SLI_QUESTIONOPTIONS"."OPTIONTEXT" as "OPTIONTEXT",
"SLI_QUESTIONOPTIONS"."OPTIONCORRECT" as "OPTIONCORRECT",
"SLI_QUESTIONS"."QUESTIONTYPEID" as "QUESTIONTYPEID",
"SLI_QUESTIONTYPES"."QUESTIONTYPENAME" as "QUESTIONTYPENAME"
from "SLI_QUESTIONTYPES" "SLI_QUESTIONTYPES",
"SLI_QUESTIONOPTIONS" "SLI_QUESTIONOPTIONS",
"SLI_QUESTIONCATEGORIES" "SLI_QUESTIONCATEGORIES",
"SLI_QUESTIONS" "SLI_QUESTIONS"
where "SLI_QUESTIONS"."QUESTIONCATEGORYID"="SLI_QUESTIONCATEGORIES"."QUESTIONCATEGORYID"
and "SLI_QUESTIONS"."QUESTIONID"="SLI_QUESTIONOPTIONS"."QUESTIONID"
and "SLI_QUESTIONS"."QUESTIONTYPEID"="SLI_QUESTIONTYPES"."QUESTIONTYPEID"
and "SLI_QUESTIONS"."QUESTIONTYPEID" = 1
order by SLI_QUESTIONS.QUESTIONCATEGORYID ASC') do |r|
qName = r[1]
qCatId = r[2]
qCategory = r[3]
qAnswerText = r[4]
qAnswerCorrect = r[5]
if qCatId == categoryidStored
xml.question(:type => "truefalse") {
writeQuestion(qName)
# xml.name {
# xml.text_ truncate_words(r[1].to_s)
# }
# xml.questiontext(:format => "html") {
# xml.text_ r[1].to_s
# }
if qAnswerCorrect == 'Y'
writeAnswer("100",qAnswerText,"Correct!")
# xml.answer(:fraction => "100") {
# xml.text_ r[4].to_s
# xml.feedback {
# xml.text_ "Correct!"
# }
# }
else
writeAnswer("0",qAnswerText,"Incorrect.")
# xml.answer(:fraction => "0") {
# xml.text_ r[4].to_s
# xml.feedback {
# xml.text_ "Incorrect."
# }
# }
end
}
else
writeCategory(qCategory)
# xml.question(:type => "category") {
# xml.category {
# xml.text_ qCategory.to_s
# }
# }
xml.question(:type => "truefalse") {
writeQuestion(qName)
# xml.name {
# xml.text_ truncate_words(r[1].to_s)
# }
# xml.questiontext(:format => "html") {
# xml.text_ r[1].to_s
# }
if qAnswerCorrect == 'Y'
writeAnswer("100",qAnswerText,"Correct!")
# xml.answer(:fraction => "100") {
# xml.text_ r[4].to_s
# xml.feedback {
# xml.text_ "Correct!"
# }
# }
else
writeAnswer("0",qAnswerText,"Incorrect.")
# xml.answer(:fraction => "0") {
# xml.text_ r[4].to_s
# xml.feedback {
# xml.text_ "Incorrect."
# }
# }
end
}
end
categoryidStored = r[2]
end
}
end
#puts builder.to_xml
@lindsayevans
Copy link

I'd pass the xml object into the functions as an argument:

writeQuestion(xml, qName)

...

def writeQuestion(xml, questText)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment