简体   繁体   English

超级扩展/包含的方法ruby

[英]super on extended/included methods ruby

Is there a way I can super a method which is added from extending a module? 有没有我独有的方式来super是从延伸的模块添加的方法? I am using elasticsearch and need to include the methods 我正在使用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 

The import method from elasticsearch: 来自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

When I do self.import , I get stack level too deep error, and when i use super I get no superclass method to super 当我做self.import ,我得到了堆栈级别太深的错误,而当我使用super我没有得到超类的方法

Update Made it work by added alias_method See above. 更新通过添加alias_method使它起作用。参见上文。

When using import options like this: 使用这样的import options

def import options
  # code...
  import options 
end

You're making a recursive call to the import method defined in Someclass , thus getting an infinite loop. 您正在对Someclass定义的import方法进行递归调用,从而获得无限循环。

If you're including the module, then just do this: 如果要包含该模块,则只需执行以下操作:

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

I believe that extend adds class methods to Someclass, so you may need to do: 我相信,扩展将类方法添加到Someclass,所以您可能需要这样做:

class Someclass
  extend SomeModuleWithImportMethod

  def self.import
    ...
    super
  end

end

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

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