Rails/Ruby 中是否有可能获取模型方法的列表。费。模型名称.方法
我想获取属于 Mailer 模型的所有方法的名称。
在 ruby 中,将方法分类始终是一个问题,因为您不能简单地说:“给我特定于该类的方法”。
您必须使用@Monk_Code 提到的数组减法,即使那样,您也无法将方法与基本实现及其猴子补丁分开。
彻底删除所有包含的模块和所有父方法:
> MyClass.instance_methods - ( MyClass.ancestors - [ MyClass ] ).map( &:instance_methods ).flatten
如果需要类方法,请替换#instance_methods
为。#methods
请注意,#define_method
在父类中动态创建的方法(如模型回调)仍然会出现,因为它们是直接在子类上定义的。
通常,将方法与类隔离是不够的。
我写了一个帮助我从文件中分离出方法的助手。它允许这样做:
> MyModel.new.located_methods
+------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+
| Name | Location |
+------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+
| ! | |
| <=> | /home/user/.gem/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/core.rb line 324 |
| unloadable | /home/user/.gem/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb line 245 |
| == | /home/user/.gem/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/core.rb line 296 |
| validates_format_of | /home/user/.gem/ruby/2.0.0/gems/activemodel-4.0.0/lib/active_model/validations/format.rb line 110 |
| and so on ... |
第一个参数允许 grep 方法名称:
> MyModel.new.located_methods /validate/
+----------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------+
| Name | Location |
+----------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------+
| _validate_callbacks | /home/user/.gem/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/core_ext/class/attribute.rb line 107 |
| _validate_callbacks= | /home/user/.gem/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/core_ext/class/attribute.rb line 117 |
| _validate_callbacks? | /home/user/.gem/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/core_ext/class/attribute.rb line 114 |
| validate_associated_records_for_amenities | /home/user/.gem/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/autosave_association.rb line 147 |
第二个允许 grep 每个源文件:
> MyModel.new.located_methods /validate/, /autosave/
+----------------------------------------------------------+------------------------------------------------------------------------------------------------------+
| Name | Location |
+----------------------------------------------------------+------------------------------------------------------------------------------------------------------+
| validate_associated_records_for_amenities | /home/user/.gem/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/autosave_association.rb line 147 |
class Object
# Display an object methods list with their source location.
#
# List can optionally be filtered by method pattern and source file
# pattern.
#
# Mainly useful for debugging.
#
# @param [Regexp] method_pattern grep method name
# @param [Regexp] file_pattern grep file name
def located_methods( method_pattern = nil, file_pattern = nil )
list = ( method_pattern ? methods.grep( method_pattern ) : methods ).sort.map do |name|
location = method( name ).source_location
location = "#{location.first} line #{location.second}" if location
[ name.to_s.colorize( :yellow ), location ]
end
list = list.select { |meth| meth[1] =~ file_pattern } if file_pattern
puts ( [[ 'Name'.colorize( :yellow ), 'Location' ]] + list ).to_table( first_row_is_head: true )
true
end
end
此版本取决于colorize和text-table,但您可以轻松修改它以使用您喜欢的格式设置方式。
class Object
def show_methods
(methods - self.class.superclass.instance_methods).sort
end
end
Mailer.show_methods
例如