Skip to content

Instantly share code, notes, and snippets.

@vargonaut
Created May 2, 2018 16:18
Show Gist options
  • Select an option

  • Save vargonaut/0a0f55882d9254553ab960452c496933 to your computer and use it in GitHub Desktop.

Select an option

Save vargonaut/0a0f55882d9254553ab960452c496933 to your computer and use it in GitHub Desktop.
Strategy pattern w/ namespaces
module API
module Vendor
class Client
def hello!
puts "Vendor Client Hello!"
end
end
class Helper
def hello!
puts "Vendor Helper Hello!"
end
end
end
module Retailer
class Client
def hello!
puts "Retailer Client Hello!"
end
end
class Helper
def hello!
puts "Retailer Helper Hello!"
end
end
end
class Doer
attr_reader :namespace
def initialize(namespace)
@namespace = namespace
end
def do
client.new.hello!
helper.new.hello!
end
def client
namespace::Client
end
def helper
namespace::Helper
end
end
end
# ns = case something
# when x
# API::Vendor
# when y
# API::Retailer
# else
# raise "No namespace match for #{something}"
# end
# doer = API::Doer.new(ns)
# doer.do
doer = API::Doer.new(API::Vendor)
doer.do
puts ''
doer = API::Doer.new(API::Retailer)
doer.do
#$ ruby strategy.rb
# Vendor Client Hello!
# Vendor Helper Hello!
#
# Retailer Client Hello!
# Retailer Helper Hello!
#$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment