Ruby/Sinatra - Using Helper *classes that can also consume Sinatra settings

319 views Asked by At

I have a classic Sinatra Application:

module Tds
  class Application < Sinatra:: Application
    set :persons, []
  end
end 

All of my routes are broken up into controllers in the style above.

I'm building a websockets application using sinatra-websockets, so I'd like to use some home-grown objects to help me manage my connection state. I might want a helper class like this:

class Person
  attr_accessor: :favorite_color, :name, :etc
end

I'd like to store my persons in a Sinatra settings array:

get '/' do
  person = Person.new(init_data)
  settings.persons << person
  erb :whatev
end

But I'd like the Person class to be able to access the sinatra settings object, so it could do some self maintenance. Maybe like this:

get '/quit' do
  person.delete!
end

which would do this:

class Person
  #....
  def delete!
   settings.persons.delete_if{|p| p == self}
  end
end

I'm pretty sure my psuedocode is pretty close to actual code, give or take a syntax issue or mis-remembered keyword.

The problem I keep having is name space issues - either I don't understand where to put my helper classes (Do I have to place them in module Tds and include them somewhere?) or maybe Sinatra doesn't support classes as helpers? Or do I not use helpers at all?)

After the namespace issue, I still have the scope issue. No matter what I have tried, even when I can figure out the namespace Issue (and I don't remember how I did), my helper classes can't access the Settings object.

I'm fairly sure I'm just not used to the Sinatra convention. Any guidance for me?

2

There are 2 answers

0
Yorkshireman On

I'm not aware of the settings array being used like this before. Does this settings array exist as part of Sinatra? Can you store stuff in it like this?

I guess if it's possible then fair enough, but there might be some limitations to it. General practice to get persistence is to use the session hash. This is limited to a few kilobytes, so if it's not big enough, setup a database.

0
peter On

It's a bit confusing to see like this where you keep these fragments. We would need to see the whole of the code to pinpoint exactly what is wrong. You are aware that a helpers.rb file needs to be required in your main file/class ? And also included, since it is a Module ? Placing the class inside or outside the Module makes a difference in scope.

If you could publish the whole of the code, we could offer more help, if not, here some general advise..

The settings object in Sinatra is what you configure in your main class/startup file. by doing

configure do
  enable :run
  set :port, 8003
  ...
  set :public_folder, File.join(File.dirname(__FILE__), 'public')
  set :files, File.join(settings.public_folder, 'files')
end

or just

set :port, 8002
set :bind, '0.0.0.0'
...

In your helper file you can access these settings in the same Sinatra application namespace like this

settings.files