0

我正在尝试在 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基类中存在一个方法Decoratorvia attr_accessor,所以我想,子类UserCreator也有这个方法。所以我不确定为什么它认为resource是一种缺失的方法。如果我摆脱Decorator超类并只在类中实现method_missings UserCreator,一切都会按预期工作。

非常感谢在实现这个基类按预期工作方面的任何帮助,因此我不必method_missing在每个装饰器中实现相同的方法。

4

1 回答 1

2

第二个method_missing是类方法。因此,这个方法里面self是指类,而不是实例。

但是,该方法尝试访问self.resource哪个是实例的属性,而不是类。

由于Decorator该类没有resource属性,method_missing因此被再次调用......再次......再次......

我正试图围绕这里发生的事情的控制流。方法资源通过 attr_accessor 存在于基础装饰器类中,

它存在于Decorator 类的实例中,但不存在于类本身中。

所以我想,子类 UserCreator 也有这个方法。

UserCreator 子类的实例有它,但子类本身没有。

于 2013-05-29T04:46:17.377 回答