如何定义要从模块化SINATRA应用程序的配置挡路调用的方法? [英] How to define a method to be called from the configure block of a modular sinatra application?
本文介绍了如何定义要从模块化SINATRA应用程序的配置挡路调用的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Sinatra应用程序,简而言之,基本上是这样的:
class MyApp < Sinatra::Base
configure :production do
myConfigVar = read_config_file()
end
configure :development do
myConfigVar = read_config_file()
end
def read_config_file()
# interpret a config file
end
end
不幸的是,这不起作用。我得到undefined method read_config_file for MyApp:Class (NoMethodError)
read_config_file
中的逻辑非常重要,所以我不想两个都重复。如何定义可以从两个配置块调用的方法?还是我处理这个问题的方式完全错了?
推荐答案
似乎在读取文件时执行configure
挡路。您只需要在配置挡路之前移动您的方法的定义,并将其转换为一个类方法:
class MyApp < Sinatra::Base
def self.read_config_file()
# interpret a config file
end
configure :production do
myConfigVar = self.read_config_file()
end
configure :development do
myConfigVar = self.read_config_file()
end
end
这篇关于如何定义要从模块化SINATRA应用程序的配置挡路调用的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文