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?
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.