不能在前置模块中使用实例变量

Cannot use instance variables in prepended module

我希望能够将我的模块包含在 ActiveRecord::Base 中,以便 has_folder_attachments 方法可用于我的 Rails AR 类.

我这样做是为了扩展原始模块的功能以支持 AR 挂钩;但是变量 @physical_path@dice 都是零,我不明白为什么。

module FolderAttachments
  module ClassMethods
    def has_folder_attachments(physical_path, excludes: [])
      @physical_path = physical_path
      super
    end 
  end  

  def self.prepended(base)
    class << base
      prepend ClassMethods
    end  
  end  

  attr_reader :physical_path
end

module ActiveRecord
  class Base
    prepend FolderAttachments

    attr_reader :dice

    # This should run after the module method
    def self.has_folder_attachments(*args)
      @dice = true
    end
  end
end

class Damned < ActiveRecord::Base
  has_folder_attachments :for_real
end

damn = Damned.new
puts damn.physical_path # => nil
puts damn.dice          # => nil

您在使用这两个变量时混合了 实例和 (meta)class 上下文 。这两个变量都在 class 上下文(更准确地说是 metaclass 上下文)中的 运行 方法中设置它们的值。因此,您无法在实例上下文中访问这些变量(及其 attr_readers)。

要使 attr_reader 正常工作,您必须 将它们移动到 class 上下文 并从那里访问它们:

module FolderAttachments
  module ClassMethods
    ...
    attr_reader :physical_path
  end
end

module ActiveRecord
  class Base
    ...
    class << self
      attr_reader :dice
    end
  end
end

damn = Damned.new
damn.class.physical_path # => :for_real
damn.class.dice          # => true

或者您还可以添加委托给 class 级读取器的实例级读取器,以便您也可以在实例上下文中访问它们:

module FolderAttachments
  module ClassMethods
    ...
    attr_reader :physical_path
  end

  def physical_path
    self.class.physical_path
  end
end

module ActiveRecord
  class Base
    ...
    class << self
      attr_reader :dice
    end

    def dice
     self.class.dice
    end
  end
end

damn = Damned.new
damn.physical_path # => :for_real
damn.dice          # => true