Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 在初始化之前查找子类的目录以配置干视图路径_Ruby_Dry Rb - Fatal编程技术网

Ruby 在初始化之前查找子类的目录以配置干视图路径

Ruby 在初始化之前查找子类的目录以配置干视图路径,ruby,dry-rb,Ruby,Dry Rb,我在多个Rails引擎中使用,我必须在每个子类中复制配置 class BaseView < Dry::View::Controller configure do |c| c.paths = [File.join(__dir__, 'templates')] end end class SubView < BaseView configure do |c| c.paths = [File.join(__dir__, 'templates')] # todo

我在多个Rails引擎中使用,我必须在每个子类中复制配置

class BaseView < Dry::View::Controller
  configure do |c|
    c.paths = [File.join(__dir__, 'templates')]
  end
end

class SubView < BaseView
  configure do |c|
    c.paths = [File.join(__dir__, 'templates')] # todo: remove me
  end
end
此外,在大多数情况下,BaseView类并不存在于同一个gem中

如果我从子视图类中删除configure块,则不再找到模板。__dir__变量包含BaseView类的目录路径

我尝试在基类中实现一个after初始化方法,该方法可以访问子类的目录。但在这一点上,由于干rb配置的限制,配置不再可能。配置必须在初始化之前进行

我能想到的唯一解决方案是在每个类中复制configure块,或者有一个gem/引擎特定的父类来配置所有可能的模板路径

在这种情况下,查找每个子类中实现的特定方法的目录的常用方法也不起作用,因为大多数视图甚至不定义方法

在这个类的加载阶段,是否有更好的方法在父类的方法中访问给定类的目录

class BaseView < Dry::View::Controller
  def self.inherited(child)
    child.class_eval do
      configure do |c|
        c.paths = [File.join(__dir__, 'templates')]
      end
    end
  end
end

回拨。

感谢您抽出时间回答!我必须在self.inherited方法的第一行添加一个超级调用,因为干式配置使用相同的方法。但问题仍然是_dir _总是指向基类的目录。你有什么想法吗?我在这里尝试了一些方法,但它们不起作用,因为视图本身不声明方法。此外,您可能希望在嵌套继承的子级上动态声明self.inherited。
class BaseView < Dry::View::Controller
  def self.inherited(child)
    child.class_eval do
      configure do |c|
        c.paths = [File.join(__dir__, 'templates')]
      end
    end
  end
end