aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Richard Brown <rbrown@exherbo.org> 2007-11-09 00:58:22 +0000
committerAvatar Richard Brown <rbrown@exherbo.org> 2007-11-09 00:58:22 +0000
commitd227afc6de34d896d020d407b0b3057d68d7eed0 (patch)
treeca835f3a49401cf6c735342ebc5216f8d9f0dc90
parent0ecc1a49b0669525af75525e9b91e19f99ef04be (diff)
downloadpaludis-d227afc6de34d896d020d407b0b3057d68d7eed0.tar.gz
paludis-d227afc6de34d896d020d407b0b3057d68d7eed0.tar.xz
(ruby) Add version_operator and example.
-rw-r--r--doc/api/ruby/example_version_operator.rb38
-rw-r--r--ruby/paludis_ruby.cc41
-rw-r--r--ruby/paludis_ruby_TEST.rb21
3 files changed, 98 insertions, 2 deletions
diff --git a/doc/api/ruby/example_version_operator.rb b/doc/api/ruby/example_version_operator.rb
new file mode 100644
index 0000000..15e31b4
--- /dev/null
+++ b/doc/api/ruby/example_version_operator.rb
@@ -0,0 +1,38 @@
+#!/usr/bin/env ruby
+# vim: set sw=4 sts=4 et tw=100 :
+
+require 'Paludis'
+require 'example_command_line'
+
+include Paludis
+
+#Make an array of Versions
+versions = [VersionSpec.new('1.0'), VersionSpec.new('1.1'), VersionSpec.new('1.2'),
+ VersionSpec.new('1.2-r1'), VersionSpec.new('2.0')]
+
+#Make an array of VersionOperator strings
+operators = ['=', '>=', '~', '<', '~>']
+
+# Display a header
+print " #{'LHS'.ljust(8)} | #{'RHS'.ljust(8)}"
+operators.each do |operator|
+ print " | #{operator.ljust(8)}"
+end
+
+puts
+
+print '-' * 10
+puts ('+' + ('-' * 10)) * operators.length.succ
+
+#For each pair of versions
+versions.each do |v1|
+ versions.each do |v2|
+ print " #{v1.to_s.ljust(8)} | #{v2.to_s.ljust(8)}"
+
+ #Apply all of our operators, and show the results
+ operators.each do |operator|
+ print " | " + (Paludis::version_operator(operator, v1, v2) ? 'true' : 'false').ljust(8)
+ end
+ puts
+ end
+end
diff --git a/ruby/paludis_ruby.cc b/ruby/paludis_ruby.cc
index d328284..fc8932a 100644
--- a/ruby/paludis_ruby.cc
+++ b/ruby/paludis_ruby.cc
@@ -73,14 +73,16 @@ namespace
static VALUE c_install_action_error;
static VALUE c_uninstall_action_error;
static VALUE c_action_error;
+ static VALUE c_bad_version_operator_error;
static VALUE c_environment;
static VALUE c_no_config_environment;
- /* Document-method: match_package
+ /*
+ * Document-method: match_package
*
* call-seq:
- * match_package (environment, package_dep_spec, target) -> true or false
+ * match_package(environment, package_dep_spec, target) -> true or false
*
* Return whether the specified spec matches the specified target.
*
@@ -101,6 +103,31 @@ namespace
}
+ /*
+ * Document-method: version_operator
+ *
+ * call-seq:
+ * version_operator(operator, left_version_spec, right_version_spec) -> true of false
+ *
+ * Applies operator to left_version_spec andn right_version_spec
+ */
+ VALUE paludis_version_operator(VALUE, VALUE op, VALUE left, VALUE right)
+ {
+ try
+ {
+ const VersionOperator vo = VersionOperator(StringValuePtr(op));
+ const VersionSpec l = value_to_version_spec(left);
+ const VersionSpec r = value_to_version_spec(right);
+ if (vo.as_version_spec_comparator()(l, r))
+ return true;
+ return false;
+ }
+ catch (const std::exception & e)
+ {
+ exception_to_ruby_exception(e);
+ }
+ }
+
}
RegisterRubyClass::RegisterRubyClass() :
@@ -196,6 +223,8 @@ void paludis::ruby::exception_to_ruby_exception(const std::exception & ee)
rb_raise(c_uninstall_action_error, dynamic_cast<const paludis::UninstallActionError *>(&ee)->message().c_str());
else if (0 != dynamic_cast<const paludis::ActionError *>(&ee))
rb_raise(c_action_error, dynamic_cast<const paludis::ActionError *>(&ee)->message().c_str());
+ else if (0 != dynamic_cast<const paludis::BadVersionOperatorError *>(&ee))
+ rb_raise(c_bad_version_operator_error, dynamic_cast<const paludis::BadVersionOperatorError *>(&ee)->message().c_str());
else if (0 != dynamic_cast<const paludis::Exception *>(&ee))
rb_raise(rb_eRuntimeError, "Caught paludis::Exception: %s (%s)",
@@ -398,7 +427,15 @@ void PALUDIS_VISIBLE paludis::ruby::init()
*/
c_uninstall_action_error = rb_define_class_under(c_paludis_module, "UninstallActionError", c_action_error);
+ /*
+ * Document-class: Paludis::BadVersionOperatorError
+ *
+ * Thrown if a bad version operator is encountered
+ */
+ c_bad_version_operator_error = rb_define_class_under(c_paludis_module, "BadVersionOperatorError", rb_eRuntimeError);
+
rb_define_module_function(c_paludis_module, "match_package", RUBY_FUNC_CAST(&paludis_match_package), 3);
+ rb_define_module_function(c_paludis_module, "version_operator", RUBY_FUNC_CAST(&paludis_version_operator), 3);
rb_define_const(c_paludis_module, "Version", rb_str_new2((stringify(PALUDIS_VERSION_MAJOR) + "."
+ stringify(PALUDIS_VERSION_MINOR) + "." + stringify(PALUDIS_VERSION_MICRO)).c_str()));
diff --git a/ruby/paludis_ruby_TEST.rb b/ruby/paludis_ruby_TEST.rb
index c6860fe..02e2452 100644
--- a/ruby/paludis_ruby_TEST.rb
+++ b/ruby/paludis_ruby_TEST.rb
@@ -60,6 +60,27 @@ module Paludis
Paludis::match_package(env,spec,spec)
end
end
+
+ def test_version_operator
+ one = VersionSpec.new('1')
+ two = VersionSpec.new('2')
+ assert Paludis::version_operator('<', one, two)
+ assert Paludis::version_operator('<=', one, two)
+ assert !Paludis::version_operator('>=', one, two)
+ assert !Paludis::version_operator('>', one, two)
+ end
+
+ def test_bad_version_operator
+ one = VersionSpec.new('1')
+ two = VersionSpec.new('2')
+ assert_raise BadVersionOperatorError do
+ Paludis::version_operator('throw an error', one, two)
+ end
+
+ assert_raise ArgumentError do
+ Paludis::version_operator('throw an error')
+ end
+ end
end
end