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.
精彩评论