aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Richard Brown <rbrown@exherbo.org> 2007-12-15 23:18:21 +0000
committerAvatar Richard Brown <rbrown@exherbo.org> 2007-12-15 23:18:21 +0000
commit8371766fa48f3214d25f374c37e58f92f31a6431 (patch)
tree5e93e50aae4b59f118fe1c909eeb9a329a50171b
parentf9f3e2fbb9b312fa72a708b2aaf1a3eb5ab05bce (diff)
downloadpaludis-8371766fa48f3214d25f374c37e58f92f31a6431.tar.gz
paludis-8371766fa48f3214d25f374c37e58f92f31a6431.tar.xz
(ruby) Add example_package_database.rb, AmbiguousPackageNameError.options.
-rw-r--r--doc/api/index.html.part2
-rw-r--r--doc/api/ruby/example_package_database.rb42
-rw-r--r--ruby/paludis_ruby.cc46
3 files changed, 86 insertions, 4 deletions
diff --git a/doc/api/index.html.part b/doc/api/index.html.part
index df2af9f..8659124 100644
--- a/doc/api/index.html.part
+++ b/doc/api/index.html.part
@@ -151,7 +151,7 @@ how concepts map on to different language bindings.</p>
<tr>
<td>example_package_database</td>
<td><a href="cplusplus/example__package__database_8cc-example.html">C++</a></td>
- <td></td>
+ <td><a href="ruby/example_package_database.html">Ruby</a></td>
<td></td>
<td>How to use PackageDatabase</td>
</tr>
diff --git a/doc/api/ruby/example_package_database.rb b/doc/api/ruby/example_package_database.rb
new file mode 100644
index 0000000..913d202
--- /dev/null
+++ b/doc/api/ruby/example_package_database.rb
@@ -0,0 +1,42 @@
+#!/usr/bin/env ruby
+# vim: set sw=4 sts=4 et tw=100 :
+
+=begin description
+This example demonstrates how to use Mask. It displays all the
+mask keys for a particular PackageID.
+=end
+
+require 'Paludis'
+require 'example_command_line'
+
+include Paludis
+
+exit_status = 0
+
+# We start with an Environment, respecting the user's '--environment' choice.
+env = EnvironmentMaker.instance.make_from_spec(ExampleCommandLine.instance.environment)
+
+# The most useful PackageDatabase method is PackageDatabase::query,
+# which is covered in other examples. But there are others:
+if env.package_database.has_repository_named?('gentoo')
+ repo = env.package_database.fetch_repository('gentoo')
+ puts "Repository 'gentoo' exists, and has format '" +
+ (repo.format_key ? repo.format_key.value : '') + "'"
+end
+
+puts "Our favourite repository is '#{env.package_database.favourite_repository}'"
+
+begin
+ name = env.package_database.fetch_unique_qualified_package_name('git')
+ puts "The only package named 'git' is '#{name}'"
+rescue NoSuchPackageError
+ puts "There is no package named 'git'"
+rescue AmbiguousPackageNameError
+ puts "There are several packages named 'git':"
+ $!.options.each do |o|
+ puts " #{o}"
+ end
+end
+
+exit exit_status
+
diff --git a/ruby/paludis_ruby.cc b/ruby/paludis_ruby.cc
index f35f48c..323b2fe 100644
--- a/ruby/paludis_ruby.cc
+++ b/ruby/paludis_ruby.cc
@@ -169,7 +169,16 @@ void paludis::ruby::exception_to_ruby_exception(const std::exception & ee)
else if (0 != dynamic_cast<const paludis::NoSuchRepositoryError *>(&ee))
rb_raise(c_no_such_repository_error, dynamic_cast<const paludis::NoSuchRepositoryError *>(&ee)->message().c_str());
else if (0 != dynamic_cast<const paludis::AmbiguousPackageNameError *>(&ee))
- rb_raise(c_ambiguous_package_name_error, dynamic_cast<const paludis::AmbiguousPackageNameError *>(&ee)->message().c_str());
+ {
+ VALUE ex_args[2];
+ ex_args[0] = rb_str_new2(dynamic_cast<const paludis::AmbiguousPackageNameError *>(&ee)->message().c_str());
+ ex_args[1] = rb_ary_new();
+ const AmbiguousPackageNameError * e = dynamic_cast<const paludis::AmbiguousPackageNameError *>(&ee);
+ for (AmbiguousPackageNameError::OptionsConstIterator f(e->begin_options()), f_end(e->end_options()) ;
+ f != f_end ; ++f)
+ rb_ary_unshift(ex_args[1], rb_str_new2(stringify(*f).c_str()));
+ rb_exc_raise(rb_class_new_instance(2, ex_args, c_ambiguous_package_name_error));
+ }
else if (0 != dynamic_cast<const paludis::NoSuchPackageError *>(&ee))
rb_raise(c_no_such_package_error, dynamic_cast<const paludis::NoSuchPackageError *>(&ee)->message().c_str());
else if (0 != dynamic_cast<const paludis::PackageDatabaseLookupError *>(&ee))
@@ -202,6 +211,7 @@ void paludis::ruby::exception_to_ruby_exception(const std::exception & ee)
else if (0 != dynamic_cast<const paludis::FetchActionError *>(&ee))
{
VALUE ex_args[2];
+ ex_args[0] = rb_str_new2(dynamic_cast<const paludis::FetchActionError *>(&ee)->message().c_str());
ex_args[1] = rb_ary_new();
const FetchActionError * e = dynamic_cast<const paludis::FetchActionError *>(&ee);
for (Sequence<FetchActionFailure>::ConstIterator f(e->failures()->begin()), f_end(e->failures()->end()) ;
@@ -306,6 +316,29 @@ fetch_action_error_failures(VALUE self)
return rb_attr_get(self, rb_intern("failures"));
}
+static VALUE
+ambiguous_package_name_error_init(int argc, VALUE* argv, VALUE self)
+{
+ VALUE options;
+
+ options = (argc > 1) ? argv[--argc] : Qnil;
+ rb_call_super(argc, argv);
+ rb_iv_set(self, "options", options);
+ return self;
+}
+
+/*
+ * call-seq:
+ * options -> Array
+ *
+ * Our options
+ */
+VALUE
+ambiguous_package_name_error_failures(VALUE self)
+{
+ return rb_attr_get(self, rb_intern("options"));
+}
+
void PALUDIS_VISIBLE paludis::ruby::init()
{
/*
@@ -320,7 +353,6 @@ void PALUDIS_VISIBLE paludis::ruby::init()
c_package_dep_spec_error = rb_define_class_under(c_paludis_module, "PackageDepSpecError", rb_eRuntimeError);
c_package_database_error = rb_define_class_under(c_paludis_module, "PackageDatabaseError", rb_eRuntimeError);
c_package_database_lookup_error = rb_define_class_under(c_paludis_module, "PackageDatabaseLookupError", c_package_database_error);
- c_ambiguous_package_name_error = rb_define_class_under(c_paludis_module, "AmbiguousPackageNameError", c_package_database_lookup_error);
c_no_such_package_error = rb_define_class_under(c_paludis_module, "NoSuchPackageError", c_package_database_lookup_error);
c_no_such_repository_error = rb_define_class_under(c_paludis_module, "NoSuchRepositoryError", c_package_database_lookup_error);
c_configuration_error = rb_define_class_under(c_paludis_module, "ConfigurationError", rb_eRuntimeError);
@@ -374,11 +406,19 @@ void PALUDIS_VISIBLE paludis::ruby::init()
* Thrown if a PackageID fails to perform a FetchAction.
*/
c_fetch_action_error = rb_define_class_under(c_paludis_module, "FetchActionError", c_action_error);
- rb_define_module_function(c_fetch_action_error, "failures", RUBY_FUNC_CAST(&fetch_action_error_failures), 0);
rb_define_method(c_fetch_action_error, "initialize", RUBY_FUNC_CAST(&fetch_action_error_init), -1);
rb_define_method(c_fetch_action_error, "failures", RUBY_FUNC_CAST(&fetch_action_error_failures), 0);
/*
+ * Document-class: Paludis::AmbiguousPackageNameError
+ *
+ * Thrown if a PackageDatabase query results in more than one matching Package.
+ */
+ c_ambiguous_package_name_error = rb_define_class_under(c_paludis_module, "AmbiguousPackageNameError", c_package_database_lookup_error);
+ rb_define_method(c_ambiguous_package_name_error, "initialize", RUBY_FUNC_CAST(&ambiguous_package_name_error_init), -1);
+ rb_define_method(c_ambiguous_package_name_error, "options", RUBY_FUNC_CAST(&ambiguous_package_name_error_failures), 0);
+
+ /*
* Document-class: Paludis::InfoActionError
*
* Thrown if a PackageID fails to perform a InfoAction.