您可以創建一個包含configure
類的gem。這個類有一個load
方法,它將一個目錄作爲參數。然後,您可以傳遞您當前正在工作的目錄。
您準備創業板的好方法是在你的寶石打造Configuration
單件類:
require 'singleton'
class Configuration
include Singleton
attr_accessor :config, :app_path
def load(app_path)
@app_path = app_path
#load the config file and store the data
@config = YAML.load_file(File.join(@app_path,'config','config.yml'))
end
end
在主類:
module MyFancyGem
class << self
#define a class method that configure the gem
def configure(app_path)
# Call load method with given path
config.load(app_path)
end
# MyFancyGem.config will refer to the singleton Configuration class
def config
MyFancyGem::Configuration.instance
end
end
end
用法:
-Working directory
- my_new_script
- Gemfile
- config/
- config.yml
在my_new_script中:
require 'bundler'
Bundler.setup(:default)
require 'my_fancy_gem'
MyFancyGem.configure(File.join(File.dirname(__FILE__),"./")) #here, you define the path
MyFancyGem.hello_world
我希望這已經夠清楚了。我實際上是要寫一篇博客文章來解釋這個特定的問題(我希望能有一個更完整的版本)。讓我知道你是否感興趣!
想到的地方是放在用戶主目錄下的dotfile文件中。 –