簡體   English   中英

生成器Ruby Gem中沒有用於配置的方法“配置”

[英]No method 'configuration' for Configuration in Generator Ruby Gem

我對此感到困惑。 我最近寫了一個gem,它為Rails項目添加了一個生成器。 我試圖將配置添加到gem,以設置生成器的默認值。 但是,當我嘗試從生成器引用模塊的配置方法時,出現no method 'configuration'錯誤。

以下是相關代碼:

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'錯誤?

編輯1

讓我們為此投入一把扳手。 繼續前進,完成了所有將使用該配置的代碼的編寫,當我運行測試時,所有代碼都通過了。 因此,運行測試時, Componentize.configuration.extensionConfiguration類中的其他任何attr_accessors都可以正常運行。 我遇到的失敗是嘗試在Rails項目中運行gem的生成器時,當我刪除配置時,生成器運行,當我有當前配置時,它給出了no method錯誤。

好的,首先,我覺得這有點超出我的知識范圍,但是我只是拋出一個瘋狂的猜測。

  class << self
    attr_accessor :configuration
  end

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

問題可能與這兩個塊都將在相同名稱空間下創建配置方法有關嗎?

另外,我正在嘗試在此處測試運行的純紅寶石,並進行一些細微更改,並且沒有出現任何錯誤:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM