您可以创建一个包含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文件中。 –