在我的Rails應用程序中,我需要一個包裝ActiveRecord對象並禁用其所有setter的類。其他方法應該委託給內部對象。委託所有方法但設置器
例子:
class Person < ActiveRecord::Base
attr_accessible :name, :age
def adult?
age >= 18
end
end
class PersonWrapper
def initialize(person)
@person = person
end
end
person = Person.new(name: "John", age: 12)
wrapper = PersonWrapper.new(Person)
wrapper.age # => 12
wrapper.adult? # => false
wrapper.age = 123 # error
有任何方便的方式做到這一點,除了method_missing
?