2016-07-03 6 views
2

私はこれで困惑しています。私は最近、Railsプロジェクトにジェネレータを追加する宝石を書きました。私はジェネレータのデフォルトを設定するためにgemに設定を追加しようとしています。しかし、モジュールの設定メソッドをジェネレータから参照しようとすると、no method 'configuration'エラーが発生します。ジェネレータのコンフィグレーションのためのメソッド 'コンフィグレーション'がありませんRuby Gem

componentize.rb

require 'rails' 
require "componentize/engine" 
require "componentize/version" 
require "componentize/configuration" 

module Componentize 
    class << self 
    attr_accessor :configuration 
    end 

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

    def self.configure 
    yield(configuration) 
    end 

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

コンポーネント化/ configuration.rb

module Componentize 
    class Configuration 
    attr_accessor :extension, :view_partial_dir, :inline_style_partial_dir, :block_style_partial_dir, :import_file 

    def initialize 
     @extension = 'erb' 
     @view_partial_dir = 'application' 
     @inline_style_partial_dir = 'blocks' 
     @block_style_partial_dir = 'sections' 
     @import_file = 'base.scss' 
    end 
    end 
end 

コンポーネント化/ component_generator.rb

:ここ

は、関連するコードであります

require 'rails/generators/named_base' 

module Componentize 
    class ComponentGenerator < Rails::Generators::NamedBase 
    # Componentize.configuration will error here. 
    end 
end 

そして私私のテストの内部puts Componentize.configuration.extension場合、それは、価値がある何のため、それputs 'erb'ちょうどそれが必要なので、私は、設定方法がどこかで利用可能であることを知っているような。

スペック/ libに/発電機/コンポーネント化/ component_spec.rb

require 'fileutils' 
require 'spec_helper' 
require 'generator_spec' 
require 'generators/componentize/component_generator' 

describe Componentize::ComponentGenerator, type: :generator do 
    destination File.expand_path('../tmp', __FILE__) 
    arguments %w(test-component) 

    before :all do 
    prepare_destination 
    create_tmp_dirs_and_files 
    show_me_componentize 
    run_generator 
    end 

    it "creates a view file" do 
    assert_file "app/views/application/_test_component.html.erb" 
    end 

    it "creates an scss file" do 
    assert_file "app/assets/stylesheets/blocks/_test_component.scss" 
    end 

    it "finds a base.scss file" do 
    assert_file "app/assets/stylesheets/base.scss" 
    end 

    private 

    def create_tmp_dirs_and_files 
    FileUtils.mkdir_p('spec/lib/generators/tmp/app/assets/stylesheets') 
    FileUtils.touch('spec/lib/generators/tmp/app/assets/stylesheets/base.scss') 
    end 

    def show_me_componentize 
    puts Componentize.configuration.extension # works just fine 
    end 
end 

私はno method 'configuration'エラーを取得する理由を誰もが理解できますか?

EDIT 1

のは、この中にレンチをスローしてみましょう。テストを実行すると、構成を使用するすべてのコードの書き出しが完了し、すべてがパスします。したがって、Componentize.configuration.extensionConfigurationクラスのattr_accessorsは、テストの実行時に正しく動作します。私が得ている失敗は、宝石のジェネレータをRailsプロジェクトで実行しようとするときです。設定を削除すると、ジェネレータが実行されます。

答えて

0

まず、私はこれが私の知識をはるかに超えていると感じますが、私はちょうど野生の推測を投げます。

class << self 
    attr_accessor :configuration 
    end 

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

多分、問題は、これらの2つのブロックの両方が同じ名前空間で構成メソッドを作成するという事実と関係がありますか?

私はいくつかのマイナーな変更で、純粋なルビーを実行し、ここでテストしようとしている、と私はエラーを取得していない。また、:

module Componentize 
    class << self 
    attr_accessor :configuration 
    end 

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

    def self.configure 
    yield(configuration) 
    end 

    def self.reset 
    @configuration = Configuration.new 
    end 

    class Configuration 
    attr_accessor :extension 

    def initialize 
     @extension = 'erb' 
    end 
    end 

    class ComponentGenerator # < Rails::Generators::NamedBase 
    @default_extension = Componentize.configuration.extension # failure here 
    # More code 
    end 

    ComponentGenerator.new 
end 
+0

記録のために、あなたの最初の直感は、私が持っている、問題になることはほとんどありません以前にこのパターンを見てきました。次に例を示します:https://robots.thoughtbot.com/mygem-configure-blockこれらのクラスは別々のファイルにありますので、インクルードがない場合に備えて私の例を更新します。 –

関連する問題