11

我想根据包含这个 Mixin 的类名在 Mixin 中动态生成一个类方法。

这是我当前的代码:

module MyModule  
  extend ActiveSupport::Concern  

  # def some_methods  
  #   ...  
  # end  

  module ClassMethods  

    # Here is where I'm stuck...
    define_method "#{self.name.downcase}_status" do  
      # do something...  
    end  

  end  
end  

class MyClass < ActiveRecord::Base  
  include MyModule  
end  

# What I'm trying to achieve:
MyClass.myclass_status

但这给了我以下方法名称:

MyClass.mymodule::classmethods_status  

在方法定义中获取基类名称有效(self,self.name ...),但我无法使其适用于方法名称...

到目前为止,我已经尝试过

define_method "#{self}"
define_method "#{self.name"
define_method "#{self.class}"
define_method "#{self.class.name}"
define_method "#{self.model_name}"
define_method "#{self.parent.name}"

但这似乎都不起作用:/

有什么方法可以检索基类名称(不确定如何调用包含我的模块的类)。我已经为这个问题苦苦挣扎了好几个小时,但我似乎无法找到一个干净的解决方案:(

谢谢!

4

4 回答 4

7

你不能那样做——此时还不知道哪个类(或哪些类)包含该模块。

如果您定义了一个self.included方法,它将在每次包含模块时被调用,并且执行包含的事物将作为参数传递。或者,由于您使用的是 AS::Concern,您可以这样做

included do 
  #code here is executed in the context of the including class
end
于 2013-02-05T12:26:55.480 回答
7

我找到了一个干净的解决方案:使用define_singleton_method(在 ruby​​ v1.9.3 中可用)

module MyModule  
  extend ActiveSupport::Concern  

  included do
    define_singleton_method "#{self.name}_status" do
      # do stuff
    end
  end

  # def some_methods  
  #   ...  
  # end  

  module ClassMethods  
    # Not needed anymore!
  end  
end  
于 2013-02-05T14:29:19.470 回答
1

你可以这样做:

module MyModule
  def self.included(base)
    (class << base; self; end).send(:define_method, "#{base.name.downcase}_status") do
      puts "Hey!"
  end

  base.extend(ClassMethods)
end

  module ClassMethods
    def other_method
      puts "Hi!"
    end
  end
end

class MyClass
  include MyModule
end

MyClass.myclass_status
MyClass.other_method
于 2013-02-05T12:28:48.600 回答
1

适用于extend

module MyModule  
  def self.extended who
    define_method "#{who.name.downcase}_status" do
      p "Inside"
    end
  end
end  

class MyClass  
  extend MyModule  
end  

MyClass.myclass_status
于 2013-02-05T12:38:57.887 回答