4

I'm tring to do something basic in Padrino, but lots of searching hasn't come up with any answers. I'm trying to access a global setting from a model. I have added a line to the "development" section of the environments.yaml file called "endpoint", how do I access this setting from a model class? Is environments.yaml the best file to use for this kind of setting?

environments:
  development:
    endpoint: 'http://192.168.56.1:3001'

When I try using "settings.endpoint" in a model function, I get the error:

undefined local variable or method `settings' for Country:Class
kristianp
  • 5,496
  • 37
  • 56

3 Answers3

2

If I were you I would use environment variables:

environments:
  development:
    endpoint: <%= ENV["ENDPOINT"] %>

Then in your ruby code you can access it with ENV['ENDPOINT'].

See how to set environments variables in a .env file with a gem such as Dotenv.

Pak
  • 2,123
  • 22
  • 28
  • Thanks, using environment variables means I don't need a yaml configuration file though, so the `endpoint: <%= ENV["ENDPOINT"] %>` is redundant. – kristianp Jul 26 '14 at 21:23
  • Well I'm not an expert about Padrino but in rails some .yml files are mandatory whether you use env vars or not. – Pak Jul 26 '14 at 21:53
2

This may not be exactly what you asked for, but here's how I solved this for settings that are not environment-specific:

Specify the setting in config/apps.rb:

Padrino.configure_apps do
  set :foobar, 42
end

You should then be able to get the value with:

MyApp::App.settings.foobar # => 42

This is, of course, assuming your Padrino app is called MyApp (you can check in app/app.rb if you're not sure).

Alessandro
  • 788
  • 10
  • 13
0

Tested on Padrino 0.13.2

Padrino.mounted_apps[0].app_obj.settings.endpoint

There may be more than 1 apps mounted. I've not tested yet with multiple apps. Please comment for fix if you have discovered more.

halfer
  • 19,824
  • 17
  • 99
  • 186
James Tan
  • 1,336
  • 1
  • 14
  • 32