def generate
o = ActiveSupport::JSON.decode(options).symbolize_keys
o.each{|k, v| v.symbolize_keys! if Hash === v }
- generator.generate(o).map{|v|
+ unit.generator(amount).generate(o).map{|v|
case v
when String then Mint::Builder.build(v)
else v
end
def solver
- Mint::Solver::Maxima::Factory.create(unit)
- end
-
- private
-
- def generator
- Mint::Generator::Factory.create(unit, amount)
+ unit.solver
end
end
class ProblemGroup < ActiveRecord::Base
has_many :problem_groups_problems, :class_name => 'ProblemGroupsProblems', :dependent => :destroy
- has_many :problems, :through => :problem_groups_problems, :order => 'unit, level ASC'
+ has_many :problems, :through => :problem_groups_problems
has_many :learning_results
has_many :reports
validates_presence_of :name
validates_length_of :name, :maximum => 40
- validates_presence_of :symbol
- validates_length_of :symbol, :maximum => 40, :allow_nil => true
- validates_format_of :symbol, :with => /\A[a-z_]+\z/
+ validates_presence_of :symbol
+ validates_length_of :symbol, :maximum => 40, :allow_nil => true
+ validates_format_of :symbol, :with => /\A[a-z_]+\z/
+ validates_uniqueness_of :symbol
+
+ def generator(amount)
+ Mint::Generator::Factory.create(symbol, amount)
+ end
+
+ def solver
+ Mint::Solver::Maxima::Factory.create(symbol)
+ end
end
["ordinary_arithmetic" , "整数の四則演算"],
["decimal_arithmetic" , "小数の四則演算"],
["fractional_arithmetic" , "分数の四則演算"],
+ ["square_root_arithmetic" , "根号を含む数の計算"],
+ ["complex_number_arithmetic" , "複素数の計算"],
["expansion" , "式の展開"],
["factorization" , "因数分解"],
- ["complex_number_arithmetic" , "複素数の計算"],
["fractional_expression_arithmetic" , "分数式の計算"],
- ["linear_function_graph" , "一次関数のグラフ"],
["partial_fraction_expansion" , "部分分数の展開"],
- ["square_root_arithmetic" , "根号を含む数の計算"],
+ ["linear_function_graph" , "一次関数のグラフ"],
].each do |symbol, name|
Unit.create!(:symbol => symbol, :name => name)
end
Factory.define :problem do |m|
m.label "label"
- m.unit "unit"
m.level 1
m.amount 2
m.options({ :term_number => 2, :operators => %w[+ - * div] }.to_json)
end
-Factory.define :ordinary_arithmetic, :parent => :problem do |m|
- m.unit "ordinary_arithmetic"
+Factory.define :ordinary_arithmetic_level_1, :parent => :problem do |m|
+ m.unit{|u| u.association(:ordinary_arithmetic) }
m.level 1
m.options({
:minus => true, :min => 0, :max => 100,
}.to_json)
end
-Factory.define :decimal_arithmetic, :parent => :problem do |m|
- m.unit "decimal_arithmetic"
+Factory.define :decimal_arithmetic_level_1, :parent => :problem do |m|
+ m.unit{|u| u.association "decimal_arithmetic" }
m.level 1
m.options({
:minus => true, :digits => 2, :min => 0, :max => 10.0,
}.to_json)
end
-Factory.define :fractional_arithmetic, :parent => :problem do |m|
- m.unit "fractional_arithmetic"
+Factory.define :fractional_arithmetic_level_1, :parent => :problem do |m|
+ m.unit{ u.association "fractional_arithmetic" }
m.level 1
m.options({
:minus => true,
}.to_json)
end
-Factory.define :factorization, :parent => :problem do |m|
- m.unit "factorization"
+Factory.define :factorization_level_1, :parent => :problem do |m|
+ m.unit{|u| u.association "factorization" }
m.level 1
m.options({
:order_min => 2, :order_max => 3, :x => %w[x y z],
}.to_json)
end
-Factory.define :expansion, :parent => :problem do |m|
- m.unit "expansion"
+Factory.define :expansion_level_1, :parent => :problem do |m|
+ m.unit{|u| u.association "expansion" }
m.level 1
m.options({
:order_min => 2, :order_max => 3, :x => %w[x y z],
Factory.define :problem_group do |m|
m.label 'label'
- m.problems{|n| [n.association(:ordinary_arithmetic)] }
+ m.problems{|n| [n.association(:ordinary_arithmetic_level_1)] }
end
--- /dev/null
+# -*- coding: utf-8 -*-
+Factory.define :unit do |u|
+ u.name 'unit'
+ u.symbol 'unit'
+end
+
+[
+ ["ordinary_arithmetic" , "整数の四則演算"],
+ ["decimal_arithmetic" , "小数の四則演算"],
+ ["fractional_arithmetic" , "分数の四則演算"],
+ ["expansion" , "式の展開"],
+ ["factorization" , "因数分解"],
+ ["complex_number_arithmetic" , "複素数の計算"],
+ ["fractional_expression_arithmetic" , "分数式の計算"],
+ ["partial_fraction_expansion" , "部分分数の展開"],
+ ["square_root_arithmetic" , "根号を含む数の計算"],
+ ["linear_function_graph" , "一次関数のグラフ"],
+].each do |symbol, name|
+ Factory.define symbol.to_sym, :parent => :unit do |u|
+ u.name name
+ u.symbol symbol
+ end
+end
+
describe "before_save" do
before do
user = Factory(:user)
- problem = Factory(:ordinary_arithmetic)
+ problem = Factory(:ordinary_arithmetic_level_1)
solver = mock('solver')
answer = mock('answer')
answer.should_receive(:normalize).at_least(1).and_return("2")
}
@klass = ProblemGroup
@basic = @klass.new(@valid_attributes)
- @basic.problems << Factory.build(:ordinary_arithmetic)
+ @basic.problems << Factory.build(:ordinary_arithmetic_level_1)
end
describe "basic" do
describe "association" do
describe "has_many problms" do
before do
- @basic.problems << Factory(:expansion)
+ @basic.problems << Factory(:expansion_level_1)
@basic.save!
@basic.reload
end
before(:each) do
@valid_attributes = {
:label => "label",
- :unit => "unit",
+ :unit_id => 1,
:level => 1,
:amount => 2,
:options => "options",
end
end
- describe "unit" do
+ describe "unit_id" do
describe "is nil" do
- subject{ @basic.unit = nil; @basic }
- it{ should_not be_valid }
- end
- describe "is empty" do
- subject{ @basic.unit = ''; @basic }
- it{ should_not be_valid }
- end
- describe "length 40" do
- subject{ @basic.unit = "a"*40; @basic }
- it{ should be_valid }
- end
- describe "length 41" do
- subject{ @basic.unit = "a"*41; @basic }
- it{ should_not be_valid }
- end
- describe "valid format" do
- subject{ @basic.unit = "a"; @basic }
- it{ should be_valid }
- end
- describe "invalid format" do
- subject{ @basic.unit = "AAAAA"; @basic }
+ subject{ @basic.unit_id = nil; @basic }
it{ should_not be_valid }
end
end
Mint::Builder.should_receive(:build).at_least(1).and_return(@expression)
@basic.options = { }.to_json
@problem = @basic.dup
+ @problem.unit = Factory(:ordinary_arithmetic)
end
it{ @problem.generate.first.should == @expression }
end
Mint::Generator::Factory.should_receive(:create).and_return(generator)
@basic.options = { }.to_json
@problem = @basic.dup
+ @problem.unit = Factory(:linear_function_graph)
end
it{ @problem.generate.first.should == { } }
end
user = Factory(:user)
admin = Factory(:admin)
@problem_group = Factory(:problem_group)
- @problem_group2 = Factory(:problem_group, :label => "2", :problems => [Factory(:decimal_arithmetic)])
+ @problem_group2 = Factory(:problem_group, :label => "2",
+ :problems => [Factory(:decimal_arithmetic_level_1)])
Factory(:report, :user => user, :problem_group => @problem_group)
Factory(:report, :user => user, :problem_group => @problem_group)
Factory(:report, :user => user, :problem_group => @problem_group)