开发者

Trying to understand what Base.rakismet_binding is for

开发者 https://www.devze.com 2022-12-11 13:20 出处:网络
What does this part . . . unless Rakisme开发者_Go百科t::Base.rakismet_binding.nil? { :referrer => \'request.referer\', :user_ip => \'request.remote_ip\',

What does this part . . .

    unless Rakisme开发者_Go百科t::Base.rakismet_binding.nil?
      { :referrer => 'request.referer', :user_ip => 'request.remote_ip',
        :user_agent => 'request.user_agent' }.each_pair do |k,v|
          data[k] = eval(v, Rakismet::Base.rakismet_binding) || ''
      end
    end

of the following method do?

module InstanceMethods
  def spam?
    data = akismet_data

    unless Rakismet::Base.rakismet_binding.nil?
      { :referrer => 'request.referer', :user_ip => 'request.remote_ip',
        :user_agent => 'request.user_agent' }.each_pair do |k,v|
          data[k] = eval(v, Rakismet::Base.rakismet_binding) || ''
      end
    end

    self.akismet_response = Rakismet::Base.akismet_call('comment-check', data)
    self.akismet_response == 'true'
  end

I found other references to rakismet_binding in rakismet.rb:

  class Base
    cattr_accessor :valid_key, :rakismet_binding

and controller_extensions.rb:

def rakismet(&block)
  Rakismet::Base.rakismet_binding = binding
  yield
  Rakismet::Base.rakismet_binding = nil
end
private :rakismet

But I have no idea what it's for.


The Kernel binding is a special object holding the context of a method call including all instance variables. What rakismet(&block) method does, is to temporary assign the current binding (the ActionController instance) to a class variable so it can be accessible by any rakismet method calls and execute the content of the block.

The following code fragment

unless Rakismet::Base.rakismet_binding.nil?
  { :referrer => 'request.referer', :user_ip => 'request.remote_ip',
    :user_agent => 'request.user_agent' }.each_pair do |k,v|
      data[k] = eval(v, Rakismet::Base.rakismet_binding) || ''
  end
end

checks whether a binding is available and if so, it tries to automatically collect some information from the current binding such as the ActionController#request.referer, the ActionController#request.remote_ip and so on.

In a few words, this is a workaround to collect some variables from your current ActionController request that otherwise won't be available to Rakismet.


The last code fragment pretty much indicates its intention - its to be used in block form and wraps the current binding.

If you look at the some unit tests for this class:

http://github.com/jfrench/rakismet/blob/master/spec/models/model_extension_spec.rb?raw=true

You can see how its used.

0

精彩评论

暂无评论...
验证码 换一张
取 消