Skip to content

Instantly share code, notes, and snippets.

@certainty
Created October 8, 2015 10:38
Show Gist options
  • Save certainty/22a4c8abd262e5ed3c43 to your computer and use it in GitHub Desktop.
Save certainty/22a4c8abd262e5ed3c43 to your computer and use it in GitHub Desktop.
require "proxy"
# get the name of the manager
# let's say it's pacman in our case
pacman = PkgManagerProxy.new("pacman")
pacman.install("tmux")
class Pacman
def install(*args)
puts "Installing: #{args.inspect}"
end
end
class PkgManagerProxy
def initialize(name)
@instance = create_instance(name)
end
def method_missing(name, *args, &block)
@instance.public_send(name, *args, &block)
end
def respond_to_missing?(*args)
@instance.respond_to?(*args)
end
private
def load_implementation(name)
# determine the path
require_relative "#{name}.rb"
end
def create_instance(name)
load_implementation(name)
Object.const_get(name.capitalize).new
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment