0

I'm not sure this is the best topic name I could use but is there a way in sinatra+rack to use classes/modules like in php+apache way? I mean is there a way you can use Singleton or any other static typed variables in just request scope? What I need(would want to) is:

class Config # might be just module
    # some methods that are request scoped
    # cant be shared between users/reuqest

    def server_settings
        # possible server dependent settings 
        # can be shared between requests on same server
    end
end

class Something
    def do_thing
        # I would like to access Config class here without passing it as parameter
        # setted up for the current request
    end
end

class MyController < Sinatra::Base
    # request handling, creates Config class, setting up request parameters once

    def handle_it
        Something.new.do_thing
    end
end

No rails here and I'm very limited on things I can do / gems I can use. MyController comes from internal framework. I can ofc extend it though. Php+apache goes from nothing every request so all your code is request only unless you use sessions/cookies.

The reason why I would like to use this is to avoid passing config instance(or any other kind of data repository) into every other instance as parameter and simply access it "globaly".

insider
  • 352
  • 2
  • 5
  • Something like [this](http://speakmy.name/2011/05/29/simple-configuration-for-ruby-apps/) so I can call `Appdata.my_data` inside of `Something#do_thing` and it would be request scoped (so another request cant see data from first one). I dont know if that is even possible with sinatra, but would be great to have. – insider May 11 '15 at 09:37

1 Answers1

0

I think you looking for something like this answer Page Views counter sinatra? tl;dr version is using set

set :my_config_property, 'hello world'
Community
  • 1
  • 1
Pavel
  • 47
  • 2
  • 10
  • But how to access settings from other classes like in `Something#do_thing`? If it is just across the controller (class extending Sinatra::Base), it doesnt realy help me. – insider May 10 '15 at 21:41