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

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.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mathcha/mathcha_helper.rb', line 35

def mathcha_tag(options={})
  options.reverse_merge!({:seed_one => 80, :seed_two => 10})
  
  solv_key  = Time.now.to_i 
  solv_prob = generate_solv(SOLV_OPS[rand(SOLV_OPS.size)], options[:seed_one].to_i, options[:seed_two].to_i)

  session[:solv] = [solv_key, eval(solv_prob)]
  
  html = ''
  html << %{#{solv_prob} = <input type="text" name="solv" />\n}
  html << %{<input type="hidden" name="solv_key" value="#{solv_key}" />\n}
  html
end