0

I am a complete noob to rack. I have 2 sinatra classes engine_a.rb and engine_b.rb

Want to pass the rackoptions using ru file like

$ rackup config.ru -O engine=a

I want to use a selective loading using rack similar to the following

require 'engine_a'
require 'engine_b'

if rackoption == a
  run eng_a.new
else if rackoption == b
  run eng_b.new
end

Please tell me if my analogy of doing the same is wrong. If not can somebody help me with a code that can help me realize the same behavior. I didnt get enough tutorials on these.

Also let me know if rack is the right tool to do it.

Saurajeet
  • 1,088
  • 2
  • 8
  • 10
  • What is your ultimate goal? Why do you need to switch between two sinatra apps? More background information would be good in order to provide a helpful answer or even a better solution. – Patrick Oscity Jun 19 '13 at 13:20
  • The two sinatra apps are 2 different engines (/ways) of doing the same thing. So i need the selective loading. – Saurajeet Jun 24 '13 at 10:38

1 Answers1

0

@padde makes a good point, you need to give us more information. However, one easy way to selectively run things is to use environment variables. The obvious classic use is to run some things in production and some things in development etc, e.g.

if ENV["RACK_ENV"] == "production"
  # do this
elsif ENV["RACK_ENV"] == "staging"
 # do something almost the same
else
 # do something quite different
end

Rack will generally set those vars for you, but you could use a different one and if you wanted to run it from the commandline you could use env MYVAR=1 bin/rackup config.ru.

Consider @padde's request and tell us your goal, not the implementation you believe is best (considering you don't really know what's best or else you wouldn't be asking;) and perhaps you'll get a better answer

Kashyap
  • 4,696
  • 24
  • 26
ian
  • 12,003
  • 9
  • 51
  • 107
  • Well, I have production and staging environments for both the engines. So cant really use this method. But Thanx Anyway. Can you propose some other alternative – Saurajeet Jun 24 '13 at 10:40
  • My GOAL is to select a sinatra app (more than one in the cwd) in response to a variable passed through commandline. Does it clear the question. – Saurajeet Jun 24 '13 at 10:43
  • @Saurajeet that clears it up. What you've put in your question is fine, you can run rack apps conditionally (just try hardcoding the variables to see it). You might be better using env vars though, so `env ENGING=a rackup config.ru -O` and if `ENV["ENGINE"] == "a", but I'd probably add the setting of the env var into the Rakefile and have a couple of tasks `run_engine_a` and `run_engine_b` or something like that. – ian Jun 25 '13 at 11:29