From: okimoto Date: Fri, 26 Feb 2010 06:47:08 +0000 (+0900) Subject: fix reports_helper to show graph in reports/show X-Git-Url: http://git.sourceforge.jp/view?a=commitdiff_plain;h=1502b3686413b233e9779478cf61464f840e1fd9;p=mint%2Fmint-server.git fix reports_helper to show graph in reports/show --- diff --git a/app/helpers/reports_helper.rb b/app/helpers/reports_helper.rb index eeeb231..25ec218 100644 --- a/app/helpers/reports_helper.rb +++ b/app/helpers/reports_helper.rb @@ -1,7 +1,9 @@ module ReportsHelper def gradient_intercept(expression) - expression.split(/x/).map(&:strip).map{|v| v.gsub(/\A[^0-9]+?|[^0-9]+?\z/, '') } + m = expression.scan(%r!\A(?:\(?(\-? *[0-9/ ]+?)\)? *\* *)?x(?: *(?:\+ *)?(-? *[0-9/ ]+?))?\z!).flatten + return %w[1 0] if m.all?(&:nil?) + m.map{|v| v ? v.gsub(/ */, "") : "1" } end end diff --git a/spec/helpers/reports_helper_spec.rb b/spec/helpers/reports_helper_spec.rb index 010d8a3..a4cef0a 100644 --- a/spec/helpers/reports_helper_spec.rb +++ b/spec/helpers/reports_helper_spec.rb @@ -1,11 +1,27 @@ require 'spec_helper' describe ReportsHelper do + include ReportsHelper - #Delete this example and add some real ones or delete this file - it "should be included in the object returned by #helper" do - included_modules = (class << helper; self; end).send :included_modules - included_modules.should include(ReportsHelper) + describe "gradient_intercept" do + [ + ["2 * x + 1" , ["2", "1"]], + ["-2 * x + 1" , ["-2", "1"]], + ["2 * x - 1" , ["2", "-1"]], + ["2*x+1" , ["2", "1"]], + ["-2*x+1" , ["-2", "1"]], + ["2*x-1" , ["2", "-1"]], + ["x" , ["1", "0"]], + ["x + 1" , ["1", "1"]], + ["x - 1" , ["1", "-1"]], + ["(1/2) * x + 1/2", ["1/2", "1/2"]], + ["(1 / 2) * x + 1 / 2", ["1/2", "1/2"]], + ["(-1 / 2) * x - 1 / 2", ["-1/2", "-1/2"]], + ].each do |expression, expected| + describe expression do + it{ gradient_intercept(expression).should == expected } + end + end end end