我有没有办法super
从扩展模块中添加的方法?我正在使用elasticsearch并需要包含方法
class Someclass
include Elasticsearch::Model
class << self
alias_method :importing, :import
end
def self.import(options = {})
transform = lambda do |a|
{index: {_id: "#{a.resource_id},#{a.ayah_key}", _parent: a.ayah_key, data: a.__elasticsearch__.as_indexed_json}}
end
options = {transform: transform}.merge(options)
self.importing options
end
end
elasticsearch的导入方法:
def import(options={}, &block)
errors = []
refresh = options.delete(:refresh) || false
target_index = options.delete(:index) || index_name
target_type = options.delete(:type) || document_type
transform = options.delete(:transform) || __transform
return_value = options.delete(:return) || 'count'
unless transform.respond_to?(:call)
raise ArgumentError,
"Pass an object responding to `call` as the :transform option, #{transform.class} given"
end
if options.delete(:force)
self.create_index! force: true, index: target_index
end
__find_in_batches(options) do |batch|
response = client.bulk \
index: target_index,
type: target_type,
body: __batch_to_bulk(batch, transform)
yield response if block_given?
errors += response['items'].select { |k, v| k.values.first['error'] }
end
self.refresh_index! if refresh
case return_value
when 'errors'
errors
else
errors.size
end
end
当我self.import
时,我得到堆栈级别太深的错误,当我使用super
时,我没有超级方法超级
更新
通过添加alias_method
使其工作如上所述。
答案 0 :(得分:1)
使用import options
时这样:
def import options
# code...
import options
end
您正在对import
中定义的Someclass
方法进行递归调用,从而获得无限循环。
如果您要包含该模块,请执行以下操作:
class Someclass
include Elasticsearch::Model::ClassMethods
def import (options = {})
transform = lambda do |a|
{index: {_id: "#{a.resource_id},#{a.ayah_key}", _parent: a.ayah_key, data: a.__elasticsearch__.as_indexed_json}}
end
options = {transform: transform}.merge(options)
#calling import method from the module
super options
end
end
答案 1 :(得分:0)
我相信extend会向Someclass添加类方法,所以你可能需要这样做:
class Someclass
extend SomeModuleWithImportMethod
def self.import
...
super
end
end