Created
October 8, 2015 10:38
-
-
Save certainty/22a4c8abd262e5ed3c43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "proxy" | |
# get the name of the manager | |
# let's say it's pacman in our case | |
pacman = PkgManagerProxy.new("pacman") | |
pacman.install("tmux") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Pacman | |
def install(*args) | |
puts "Installing: #{args.inspect}" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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