我对这个感到困惑。我最近写了一个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.extension
类中的attr_accessors
和任何其他Configuration
在测试运行时都能正常工作。我遇到的失败是在尝试在Rails项目中运行gem的生成器时,当我删除配置时,生成器运行,当我有配置存在时,它给出no方法错误。
答案 0 :(得分:0)
好的,首先,我觉得这有点超出了我的知识,但我只是疯狂猜测。
class << self
attr_accessor :configuration
end
def self.configuration
@configuration ||= Configuration.new
end
这个问题可能与这两个块都会在同一名称空间下创建配置方法有关吗?
另外,我正在尝试在这里测试运行纯ruby,进行一些小的更改,我没有错误:
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