Class: DelegateDecorator

Inherits:
Object show all
Defined in:
lib/sugar-high/delegate.rb

Overview

Instance Method Summary collapse

Constructor Details

#initialize(subject, options = {}) ⇒ DelegateDecorator

Returns a new instance of DelegateDecorator.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sugar-high/delegate.rb', line 44

def initialize(subject, options = {})
  options[:only]   ||= []
  options[:except] ||= []
  options[:only].map!(&:to_s)
  options[:except].map!(&:to_s)

  meths = subject.public_methods(false)
  meths = meths & options[:only] unless options[:only].empty?

  meths.each do |meth|
    (class << self; self; end).class_eval do
      unless options[:except].include? meth.to_s
        define_method meth do |*args|
          subject.send meth, *args
        end
      end
    end
  end
end