Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
# http://pastie.caboo.se/36010 evolved # vjt@openssl.it # require 'blank' # http://pastie.caboo.se/26558 module Decorator def self.build *attributes, &block slate = Blank() slate.class_eval do def initialize(object) @_object = object end def method_missing(meth, *args, &block) @_object.send meth, *args, &block end end attributes.each { |attr| slate.send :attr_accessor, attr } slate.class_eval &block if block slate end def self.wrap object, *attributes, &block build(*attributes, &block).new(object) end end ## examples base = SomeClass.some_method wrapper = Decorator.build(:attribute, :another_attr) do def some_useful_method # actually useless end define_method(:abc) {} end wrapped = wrapper.new(base) # or wrapped = Decorator.wrap(base) wrapped.attribute # => nil wrapped.some_useful_method # => " wrapped.another_attr = SomeStuff.new
This paste will be private.
From the Design Piracy series on my blog: