Module: Mathcha::MathchaHelper
- Defined in:
- lib/mathcha/mathcha_helper.rb
Overview
Helper included in ActionView::Base. Use mathcha_tag in your views to generate a quick and - at this point - incredibly simple - arithmetic problem for your end user to validate.
Coming soon:
-
Support for floats and precision.
-
Multiple active mathchas per session.
-
Override default session key.
-
Suppport for negative values if users so wish.
Constant Summary collapse
- ADD =
For some reason I thought this may make the code look a little cleaner in the switch. Work with me.
'+'
- SUB =
'-'
- DIV =
'/'
- SOLV_OPS =
[ADD, SUB, DIV]
Instance Method Summary collapse
-
#mathcha_tag(options = {}) ⇒ Object
Use mathcha_tag to generate a simple mathcha - dead simple math problems.
Instance Method Details
#mathcha_tag(options = {}) ⇒ Object
Use mathcha_tag to generate a simple mathcha - dead simple math problems. How crazy you get with your seeds is solely at your discretion - cater to you audience.
-
All solutions are positive integers.
-
All problems generated by mathcha present positive integers to the end-user.
Other bits:
-
Pass html options as such => {:class => ‘foo’}
-
The default session key for this thing is :solv, I’d suggest you leave it be.
-
Only one mathcha can exist at a time, for now.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mathcha/mathcha_helper.rb', line 37 def mathcha_tag(={}) solv_key = Time.now.to_i seed_one = [:one] || 300 seed_two = [:two] || 100 result = nil while result.nil? or !result.is_a?(Integer) or result < 0 result, eq, eq_slick = generate_solv(SOLV_OPS[rand(SOLV_OPS.size)], seed_one, seed_two) end # Here we bury away the equation. session[:solv] = [solv_key, eval(eq)] if [:html] = [:html].inject([]){|dump, pair| dump << "#{pair[0]}=\"#{pair[1]}\""} .join(" ") end html = '' html << %{#{eq_slick.titlecase} = <input type="text" name="solv" #{html_options} />\n} html << %{<input type="hidden" name="solv_key" value="#{solv_key}" />\n} html end |