在普通Ruby类中使用方法回调
我有一个普通的ruby类在普通Ruby类中使用方法回调,ruby,activesupport,Ruby,Activesupport,我有一个普通的ruby类浓缩咖啡::MyExampleClass module Espresso class MyExampleClass def my_first_function(value) puts "my_first_function" end def my_function_to_run_before puts "Running before" end end end 对于类中的一些方法,我想在之前或之后执行一个回调
浓缩咖啡::MyExampleClass
module Espresso
class MyExampleClass
def my_first_function(value)
puts "my_first_function"
end
def my_function_to_run_before
puts "Running before"
end
end
end
对于类中的一些方法,我想在之前或之后执行一个回调,类似于ActiveSupport回调before\u action
或before\u filter
。我想在我的课堂上放一些类似的东西,在我的第一个函数
之前运行我的函数
:
before_method :my_function_to_run_before, only: :my_first_function
结果应该是:
klass = Espresso::MyExampleClass.new
klass.my_first_function("yes")
> "Running before"
> "my_first_function"
如何在Rails这样的普通ruby类中使用回调在每个指定方法之前运行一个方法
编辑2:
谢谢@tadman的推荐。我们面临的真正问题是API客户端具有令牌过期。在每次调用API之前,我们需要检查令牌是否过期。如果我们有大量的API函数,那么每次检查令牌是否过期都会很麻烦
下面是示例类:
require "rubygems"
require "bundler/setup"
require 'active_support/all'
require 'httparty'
require 'json'
module Espresso
class Client
include HTTParty
include ActiveSupport::Callbacks
def initialize
login("admin@example.com", "password")
end
def login(username, password)
puts "logging in"
uri = URI.parse("localhost:3000" + '/login')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(username: username, password: password)
response = http.request(request)
body = JSON.parse(response.body)
@access_token = body['access_token']
@expires_in = body['expires_in']
@expires = @expires_in.seconds.from_now
@options = {
headers: {
Authorization: "Bearer #{@access_token}"
}
}
end
def is_token_expired?
#if Time.now > @expires.
if 1.hour.ago > @expires
puts "Going to expire"
else
puts "not going to expire"
end
1.hour.ago > @expires ? false : true
end
# Gets posts
def get_posts
#Check if the token is expired, if is login again and get a new token
if is_token_expired?
login("admin@example.com", "password")
end
self.class.get('/posts', @options)
end
# Gets comments
def get_comments
#Check if the token is expired, if is login again and get a new token
if is_token_expired?
login("admin@example.com", "password")
end
self.class.get('/comments', @options)
end
end
end
klass = Espresso::Client.new
klass.get_posts
klass.get_comments
一个天真的实现将是
module Callbacks
def self.extended(base)
base.send(:include, InstanceMethods)
end
def overridden_methods
@overridden_methods ||= []
end
def callbacks
@callbacks ||= Hash.new { |hash, key| hash[key] = [] }
end
def method_added(method_name)
return if should_override?(method_name)
overridden_methods << method_name
original_method_name = "original_#{method_name}"
alias_method(original_method_name, method_name)
define_method(method_name) do |*args|
run_callbacks_for(method_name)
send(original_method_name, *args)
end
end
def should_override?(method_name)
overridden_methods.include?(method_name) || method_name =~ /original_/
end
def before_run(method_name, callback)
callbacks[method_name] << callback
end
module InstanceMethods
def run_callbacks_for(method_name)
self.class.callbacks[method_name].to_a.each do |callback|
send(callback)
end
end
end
end
class Foo
extend Callbacks
before_run :bar, :zoo
def bar
puts 'bar'
end
def zoo
puts 'This runs everytime you call `bar`'
end
end
Foo.new.bar #=> This runs everytime you call `bar`
#=> bar
模块回调
def自我扩展(基本)
base.send(:include,InstanceMethods)
结束
def覆盖的_方法
@重写的_方法| |=[]
结束
def回调
@回调| |=Hash.new{| Hash,key | Hash[key]=[]
结束
添加了def方法(方法名称)
返回是否应该覆盖?(方法名称)
重写的方法栏
这个实现中的难点是添加了方法
。每当一个方法被绑定时,method\u added
method就会被ruby用方法的名称调用。在这个方法内部,我所做的只是名称混乱,并用新方法覆盖原始方法,新方法首先运行回调,然后调用原始方法
注意,这个实现既不支持块回调,也不支持超类方法的回调。但是这两种方法都可以很容易地实现。这种方法链在实现方面变得非常丑陋,因为您必须重新定义x
来概括x
。ActiveRecord不包装方法,因为它有一个更好的内部调度系统。为了避免这种情况,制作一个比返回玩具字符串更能代表您意图的示例可能是有意义的。您正在使用的@DogEatDog可能与ActiveSupport::Callbacks
,但据我所知,似乎没有使用它。你有没有尝试过实现类似的功能?@DogEatDog值得一试。也许你可以展示你以前的尝试