Sha256: 06b7292a6819a8708fa4bf23518d00610c05a9a2d35d6da806240b76e151c3ff

Contents?: true

Size: 890 Bytes

Versions: 5

Compression:

Stored size: 890 Bytes

Contents

module Pickle
  module Injector
    def self.inject(delegate_class, options = {})
      target_class  = options[:into] || raise('inject requires a target class specified with :into')
      delegate_name = options[:name] || delegate_class.name.underscore.gsub('/','_')
      init_delegate = options[:init] || lambda { new }
      
      # create a session object on demand (in target)
      target_class.send(:define_method, delegate_name) do
        instance_variable_get("@#{delegate_name}") || instance_variable_set("@#{delegate_name}", delegate_class.instance_eval(&init_delegate))
      end
      
      # in the target, delegate the public instance methods of delegate_class to the delegate_name method
      delegate_methods = delegate_class.public_instance_methods - Object.instance_methods
      target_class.delegate *(delegate_methods + [{:to => delegate_name}])
    end
  end
end

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
ianwhite-pickle-0.1.5 lib/pickle/injector.rb
schlick-pickle-0.1.5.1 lib/pickle/injector.rb
schlick-pickle-0.1.5.2 lib/pickle/injector.rb
schlick-pickle-0.1.5.3 lib/pickle/injector.rb
schlick-schlick-pickle-0.1.5.1 lib/pickle/injector.rb