Skip to content

Instantly share code, notes, and snippets.

@santiago
Created June 28, 2010 19:27
Show Gist options
  • Save santiago/456244 to your computer and use it in GitHub Desktop.
Save santiago/456244 to your computer and use it in GitHub Desktop.
require 'mongo'
module MathTestr
include Mongo
MONGO_DATABASE= "mathtestr"
SUPPORTED_OPERATIONS= %w{addition substraction division multiplication fractions}
module Test
def create_test(name,operations,digits,groups,amount)
# validate "operations" is array and not empty
# and includes supported operations
return {:error => "Operations is empty"} if operations.empty?
# validate "digits" is an integer
if digits.to_i == 0
if digits != "user" || digits!= "random"
return {:error => digits }
end
else
unless digits.to_i >= 1 && digits.to_i <= 4
return {:error => "1-4" }
end
end
# validate "groups" is not empty and included
# groups exist
# validate "amount" is a number
return {:error => "Number of Questions is not a number"} if amount.to_i == 0
# we want every operation to have
# the same number of questions ...
questions_per_operation= amount/operations.length
# ... since it is not always possible
# we need to know how many question are left
remaining_questions= amount.modulo operations.length
# this is the object going to mongo
test= {
:name => name,
:questions => []
}
# for every operation we create as many as
# "questions_per_operation" questions
operations.each do |op|
questions_per_operation.times do |i|
test[:questions] << send("#{op}_question", digits)
end
end
# we give an operation to the
# remaining questions
remaining_questions.times do |i|
op= operations[i]
test[:questions] << send("#{op}_question", digits)
end
db= Mongo::Connection.new.db(MONGO_DATABASE)
coll= db.collection("tests")
coll.insert(test)
test
end
def remove_test
end
def test_item(id)
db= Mongo::Connection.new.db(MONGO_DATABASE)
coll= db.collection("tests")
coll.find_one({:_id=>BSON::ObjectID.from_string(id)})
end
def tests_list
db= Mongo::Connection.new.db(MONGO_DATABASE)
coll= db.collection("tests")
rows=[]
coll.find({}, {:fields=>["name"]}).each { |row| rows << row }
rows
end
private
def perform_basic_operation(question)
operation= question[:operation]
return question[:first_operand]+question[:second_operand] if operation == "addition"
return question[:first_operand]-question[:second_operand] if operation == "substraction"
return question[:first_operand]*question[:second_operand] if operation == "multiplication"
return question[:first_operand].to_f/question[:second_operand].to_f if operation == "division"
end
def basic_operation_question(operation, digits)
question= {
:first_operand => rand(10**digits),
:second_operand => rand(10**digits),
:operation => operation,
:digits => digits
}
question[:result]= perform_basic_operation question
question
end
def addition_question(digits)
basic_operation_question "addition", digits
end
def substraction_question(digits)
basic_operation_question "substraction", digits
end
def multiplication_question(digits)
basic_operation_question "multiplication", digits
end
def division_question(digits)
basic_operation_question "division", digits
end
def fractions_question(digits)
r= {
:first_operand => [rand(10**digits),rand(10**digits)],
:second_operand => [rand(10**digits),rand(10**digits)],
:operation => "fractions",
:digits => digits
}
r[:answer]= r[:first_operand]+r[:second_operand]
r
end
end
module Group
## Groups
def groups_list
db= Mongo::Connection.new.db(MONGO_DATABASE)
coll= db.collection("groups")
rows=[]
coll.find({}, {:fields=>["name"]}).each { |row| rows << row }
rows
end
def create_group(group_data)
group={}
if group_data["id"]
else
group["name"]= group_data["name"]
group["members"]= group_data["new_members"]
end
db= Mongo::Connection.new.db(MONGO_DATABASE)
coll= db.collection("groups")
coll.insert(group)
end
def members_list(group_id)
db= Mongo::Connection.new.db(MONGO_DATABASE)
coll= db.collection("groups")
rows=[]
id= BSON::ObjectID.from_string(group_id)
coll.find({:_id=>id}, {:fields=>["members"]}).each { |row| rows = row["members"] }
rows
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment