2017-07-26 7 views
1

ルビーの宝石を作っています。Radinです。これはRailsプロジェクトで使用されます。 rails generate radin:installを実行してconfig/initializers/radin.rbを作成するインストールプロセスがあります。Gailsの設定がRailsの初期化子に設定されていません

設定/イニシャライザ/ radin.rb(レールプロジェクト)(上記見られるように)

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

ジェネレータが意図したとおりに動作します。私の宝石では、私はMyGem.configure BlockConfig and Generators in Gemsの2つのリンクに従っています。

構成が設定されているかどうかを確認する実行可能ファイルがあります。

/EXE /ラディン

#!/usr/bin/env ruby 

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

puts "Output: #{output}" 

私のDocumentorクラスでは唯一

/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:nilのためのtest_configuration': undefined methodオプション」で:NilClass(NoMethodError)

&は、私が試した 失敗しようとしました

setモジュールを常にデフォルトの設定に変更しますが、イニシャライザの設定を変更しても、オプションは 'default_option'から変更されません。私の実行可能ファイルradin

module Radin 

    class << self 
    attr_accessor :configuration 
    end 

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

    def self.configure 
    yield(configuration) 
    end 

    ... 

答えて

1

私が追加:

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

を、すべてが正しく動作するようです。

関連する問題