At http://spoofed.org/go.tar.gz I've put a simplified version of a problem
I haven't yet found a good way to solve. I'm looking for input as to how
you'd solve the problem.
Basically I have a command line utility that takes three arguments -- an
action, a vendor and a product. Only three actions (eat, drink, rest) are
supported. Any number of vendors and products can be added by simply
creating a specially configured plugin.rb in the vendor/product directory.
The issue is that not every vendor product combination will support all
three actions. When 'go' is run and an action is specified that the given
vendor/product doesn't support, I want to:
1) Handle this gracefully
2) Show all vendors + products that support this action
go is as simple as:
#!/usr/bin/env ruby
unless (ARGV.size == 3)
raise "Usage: $0 <action> <vendor> <product>"
end
(action, vendor, product) = ARGV
require File.join("plugins", vendor, product, "plugin.rb")
case action
when /eat/
eat
when /rest/
rest
when /drink/
drink
else
raise "Unknown action #{action}"
end
And a given vendor + product, say Foo Bar, which lives in
plugins/foo/bar/plugin.rb contains:
module Go
module Foo
module Bar
def eat
puts "Foo::Bar eat!"
end
def rest
puts "Foo::Bar rest!"
end
end
end
end
include Go::Foo::Bar
Another vendor + product, Blaf Blarg is similarly implemented, but supports
all three methods.
As it stands today, if you specify an action that the vendor product does
not support (for example Foo::Bar.drink), an exception is thrown. Again, I
want to handle that more gracefully (which is easy enough with
begin/rescue) and then show all the vendor products that do support drink.
I feel like the answer lies somewhere around responds_to?, however I'd need
to have access to the module name to call responds_to?, and as currently
implemented this code doesn't know the module name.
Any feedback would be appreciated!
Thanks,
-jon