Where to place/access config file in gem?

2019-01-30 11:34发布

I am writing my first gem and I'd like specific options to be retrieved and set by the user through a config.yml file.

Where should this file be placed within my gem file structure and how does someone modify the file when installing my gem? I'm guessing they can pass in specific options when installing the gem, and those options can be mapped to the config.yml file, but how is this possible?

Also, is the best way to retrieve the file through YAML.load_file?

I've watched Ryan's railcasts on creating a gem via Bundler, but he doesn't cover this topic.

4条回答
Deceive 欺骗
2楼-- · 2019-01-30 11:44

I'm jumping on this one a little late but I'll leave an example implementation of how I generally handle this, for future reference.

As it was mentioned, you'll normally want to allow configuration through both files and hashes. It's pretty easy and light to include both ways, so you should do it.

Something like this works for me in most scenarios:

require 'yaml'

module MyGem
  # Configuration defaults
  @config = {
              :log_level => "verbose",
              :min => 0,
              :max => 99 
            }

  @valid_config_keys = @config.keys

  # Configure through hash
  def self.configure(opts = {})
    opts.each {|k,v| @config[k.to_sym] = v if @valid_config_keys.include? k.to_sym}
  end

  # Configure through yaml file
  def self.configure_with(path_to_yaml_file)
    begin
      config = YAML::load(IO.read(path_to_yaml_file))
    rescue Errno::ENOENT
      log(:warning, "YAML configuration file couldn't be found. Using defaults."); return
    rescue Psych::SyntaxError
      log(:warning, "YAML configuration file contains invalid syntax. Using defaults."); return
    end

    configure(config)
  end

  def self.config
    @config
  end
end

An added best practice would be to have defaults for all your configuration keys(as in the example above). That way, you are giving the user ultimate freedom in how they can configure your library.

查看更多
叛逆
3楼-- · 2019-01-30 11:54

If your gem includes a command which can be run interactively by the user it would be best to prompt for any necessary details on the first run. A good place to save the configuration would be in the user's home directory as a dot-file.

If your gem is purely for use in other code as a library then configuration should be allowed to be passed in as a hash or suchlike.

查看更多
Root(大扎)
4楼-- · 2019-01-30 12:08

Another pattern without using config files:

YourGem.configure do |config|
  config.api_key = 'your_key_here'
end

https://robots.thoughtbot.com/mygem-configure-block

查看更多
女痞
5楼-- · 2019-01-30 12:09

As a gem you need to allow people to interface with it how they want. You cant assume any sort of app structure. Instead expose an API that allows the developer pass in a hash of options, or a path to their own YAML file which you can read and parse.

But trying to establish a file naming convention from a gem probably isn't what you want.

查看更多
登录 后发表回答