[英]Retrieve list of instance methods that are defined in rails class excluding inherited and included methods
I have a class Product in my rails project, I am trying to retrieve a list of instance methods of my class that are defined in my file (not inherited method or included via mixin). 我的rails项目中有一个Product类,我试图检索在文件中定义的我的类的实例方法的列表(不是继承的方法或通过mixin包含的)。 Here's a small sample of my class : 这是我班的一小部分样品:
class Product
include Mongoid::Document
include Mongoid::Paperclip
include Mongoid::Search
include Mongoid::Slug
include Mongoid::Timestamps
extend Enumerize
def product_image
image.url(:small) unless image.nil?
end
def product_school_level
self.school_levels.join ' | '
end
def product_grades
self.grades.where(degree_ids: nil).pluck(:name).uniq.join ' | '
end
end
I tried to use Product.instance_methods(false)
. 我尝试使用Product.instance_methods(false)
。 However this still returns a lot of methods I dont need, here's a little sample : :_run_post_process_callbacks, :aliased_fields, :_post_process_callbacks, :_run_image_post_process_callbacks, :_image_post_process_callbacks, :_validation_callbacks, :nested_attributes?, :_run_touch_callbacks, :readonly_attributes?, :_run_save_callbacks, :aliased_fields?, :_save_callbacks, :localized_fields?, :readonly_attributes, :fields?, :pre_processed_defaults?, :_update_callbacks, :post_processed_defaults?, :fields, : _id_default 但这仍然返回很多我不需要的方法,这是一个小示例::_run_post_process_callbacks,:aliased_fields,:_post_process_callbacks,:_run_image_post_process_callbacks,:_image_post_process_callbacks,:_validation_callbacks,:nested_attributess::_run_attributes? aliased_fields ?、:_save_callbacks,:localized_fields ?、:readonly_attributes,:fields ?、:pre_processed_defaults ?、:_update_callbacks,:post_processed_defaults ?、:fields,: _id_default
I ran Product.new.method(:_run_post_process_callbacks).source_location
on a few of these methods to try to check where they come from. 我在其中一些方法上运行了Product.new.method(:_run_post_process_callbacks).source_location
,以尝试检查它们的来源。 It seems they all come from active_support. 似乎它们都来自active_support。 I never included active_support in my class, so I guess classes in a rails project automatically include active_supports methods ? 我从未在类中包含active_support,因此我猜想Rails项目中的类会自动包含active_supports方法吗? How is that possible without any inheritance syntax (<<) or include syntax ? 没有任何继承语法(<<)或包含语法怎么办? How can I then achieve what I want to do and get rid of these methods I dont need in my list ? 然后,我如何才能实现自己想做的事情,并摆脱列表中不需要的这些方法?
Many (most? all?) of those extra methods you are seeing are being created using Module#define_method
. 您正在使用Module#define_method
创建许多(大多数?全部?)这些额外的方法。 If you dig deep in the source for active_support
, you'll see that. 如果您深入研究active_support
的源代码,则会看到它。 (You aren't directly including active_support
, but it's being pulled in by one or more of the Mongoid
modules.) (您没有直接包含active_support
,但是它被一个或多个Mongoid
模块Mongoid
。)
So these are in fact valid instance methods of your model class, which is why they are included in instance_methods(false)
. 因此,这些实际上是模型类的有效实例方法,这就是为什么将它们包含在instance_methods(false)
。 Other methods that are defined "conventionally" in the mixins, such as #freeze
, are reported by instance_methods(true)
, but not by instance_methods(false)
. 在mixins中“常规”定义的其他方法,例如#freeze
,由instance_methods(true)
报告,但由instance_methods(false)
。
I think you may have to do something to filter the list based on the source location. 我认为您可能必须执行某些操作以根据源位置过滤列表。 Something along these lines: 遵循以下原则:
my_methods = Product.instance_methods(false).select do |m|
Product.instance_method(m).source_location.first.ends_with? '/product.rb'
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.