简体   繁体   English

获取Rails定制方法的列表

[英]Get list of Rails custom methods

I'm trying to get the list of methods defined in a Rails model without the attributes and reflections. 我正在尝试获取没有属性和反射的Rails模型中定义的方法的列表。

The code I have thus far is as follows but it only shows the database columns and has_many, etc definitions. 到目前为止,我的代码如下,但仅显示数据库列和has_many等定义。 I need the custom methods in the model as well. 我也需要模型中的自定义方法。

    class.reflections.keys.each do |key|
      define_method key do
        @object.send key
      end
    end
    class.attribute_names.each do |sym|
      define_method sym do
        @object.send sym
      end
    end

I've tried ".methods", etc but it includes or excludes the methods I've defined. 我尝试过“ .methods”等,但是它包含或排除了我定义的方法。

Use MyClass.instance_methods(false) , but make sure to pass false as an argument if you don't want it to return the methods defined in the superclasses. 使用MyClass.instance_methods(false) ,但是如果您不希望它返回超类中定义的方法,请确保将false作为参数传递。

Additionally, use MyClass.singleton_methods(false) for class methods. 此外,对类方法使用MyClass.singleton_methods(false)

More info: 更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM