Reputation: 4078
I am using the rails config gem to feed external urls into my application. I would like to do parameter interpolation in the following manner:
settings.yml
url: 'http://www.example.com/#{self.param}'
then in model.rb:
def get_path
magic(Settings.url) #some magic procedure or function here
end
When I call get path I would like to have the fully interpolated url. Is there some "magic" function or method that would allow me to do that with railsconfig? I would prefer something safer than passing the string into an eval call.
Upvotes: 0
Views: 733
Reputation: 4078
I ended up using the following method:
settings.yml:
url: 'http://www.example.com/%{param}'
model.rb:
def get_path
Settings.url % {param: self.param}
end
which properly returns the current value of param interpolated into the string when model#get_path is called.
Upvotes: 1
Reputation: 19899
Assuming you want self.param
interpreted at runtime when get_path
is invoked, you could use ERB to parse the result. For example:
require 'erb'
def get_path(url, param)
bar = "1"
ERB.new(url).result(binding)
end
url = "http://www.example.com/<%= param %>?bar=<%= bar %>"
param = "foo"
puts get_path(url, param)
Would yield:
http://www.example.com/foo?bar=1
Upvotes: 0