Re: Design Question - multiple XML parsers/ dependencies
Marvin Gülker <[email protected]>
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <20190331140539.ugwuyvi72bapmc4a@atlantis> |
Am 31. März 2019 um 14:31 Uhr +0800 schrieb Mohit Sindhwani:
> How should we organise such code? Would it basically be two internal
> implementations selected based on a parameter, or detection of the
> installation? Just want to get some ideas or a link to a best practice or
> somehing similar.
If #require fails, it raises a LoadError exception, which can be
rescued. This gives you the neccessary information for loading the
correct parts of your library without having to ask the user:
begin
require "ox"
# ox is available
rescue LoadError
# ox is not available
end
If you keep your library modular, you could then just load the correct
variant:
begin
require "ox"
require_relative "your-library-with/ox"
rescue LoadError
require_relative "your-library-with/rexml"
end
Gerald's suggestion with using a meta library that abstracts the actual
XML library in use is similar to this.
Alternatively, you could set some global piece of information using a
private API, e.g.
begin
require "ox"
YourLibrary.ox_is_available = true
rescue LoadError
YourLibrary.ox_is_available = false
end
--
Blog: https://mg.guelker.eu
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>