2017-07-26 67 views
1

我正在製作一顆紅寶石,我們稱它爲Radin。它用於Rails項目。有一個安裝過程通過運行rails generate radin:install創建config/initializers/radin.rb未在Rails初始化程序中設置Gem的配置

配置/初始化/ radin.rb(Rails項目)

Radin.configure do |config| 
    # Set this options to what makes sense for you 
    config.option = 'test' 
end 

發電機按預期工作(如上所見)。在我的寶石中,我遵循MyGem.configure BlockConfig and Generators in Gems兩個鏈接。

我有一個可執行文件來檢查配置是否已設置。

/EXE /雷丁

#!/usr/bin/env ruby 

require 'radin' 
output = {} 
output["option"] = Radin::Documentor.test_configuration 

puts "Output: #{output}" 

我的文件處理器類只輸出我一個配置選項

/lib/radin/documentor.rb

module Radin 
    class Documentor 
    def self.test_configuration 
     Radin.configuration.option 
    end 
    end 
end 

最後我有我的Radin模塊和Configuration

/lib/radin.rb

require "radin/version" 
require 'json' 

module Radin 

    autoload :Documentor, 'radin/documentor' 

    class << self 
    attr_accessor :configuration 
    end 

    def self.configure 
    self.configuration ||= Configuration.new 
    yield(configuration) 
    end 

    class Configuration 
    attr_accessor :option 

    def initialize 
     @option = 'default_option' 
    end 
    end 
end 

當我運行一個測試Rails應用程序目錄中我得到一個錯誤$ radin,儘管有在config/initializers/radin.rb設置的配置選項。

... /雷丁/ lib中/雷丁/ documentor.rb:8:test_configuration': undefined method選項」對零:NilClass(NoMethodError)

試圖&失敗 我試着set將模塊更改爲始終具有默認設置,但儘管在初始化程序中更改了配置,該選項也不會從「default_option」更改。

module Radin 

    class << self 
    attr_accessor :configuration 
    end 

    def self.configuration 
    @configuration ||= Configuration.new 
    end 

    def self.configure 
    yield(configuration) 
    end 

    ... 

回答

1

在我的可執行文件radin我說:

begin 
    load File.join(Dir.pwd, 'config', 'initializers', 'radin.rb') 
rescue LoadError 
    puts "Please run `rails generate radin:install` before continuing " 
end 

,一切似乎現在能夠正常工作。

相關問題