我正在尝试在 Rails 项目中干燥装饰器。
本质上,我想将任何缺少的方法委托给资源对象(或资源对象的类)。
这是一个简化的例子
# Decorator base class
class Decorator
attr_accessor :resource
private
def method_missing(name, *args, &block)
self.resource.send(name, *args, &block)
end
# infinite recursion happens here
def self.method_missing(name, *args, &block)
self.resource.class.send(name, *args, &block)
end
end
# Decorator class that will be used
class UserCreator < Decorator
attr_reader :user
def initialize(params)
@user = User.new(params[:user])
self.resource = @user
end
def save
# do special stuff with user object
if @user.save
# perhaps do some more stuff after the save
true
else
# perhaps handle the error here
false
end
end
end
# A simple controller example
class SomeController < ApplicationController
respond_to :json
def create
@user = UserCreator.new(params)
if @user.save
render :json => @user
else
render :json => @user.errors
end
end
end
然而,在类中Decorator
,无限递归发生在类(单例)方法self.method_missing
中。resource
它作为该方法中的参数传递name
。
我正试图围绕这里发生的事情的控制流。resource
基类中存在一个方法Decorator
via attr_accessor
,所以我想,子类UserCreator
也有这个方法。所以我不确定为什么它认为resource
是一种缺失的方法。如果我摆脱Decorator
超类并只在类中实现method_missing
s UserCreator
,一切都会按预期工作。
非常感谢在实现这个基类按预期工作方面的任何帮助,因此我不必method_missing
在每个装饰器中实现相同的方法。