aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Richard Brown <rbrown@exherbo.org> 2006-11-10 19:50:07 +0000
committerAvatar Richard Brown <rbrown@exherbo.org> 2006-11-10 19:50:07 +0000
commit8f27e941a16cc0bada04630c3d915aa89d181e85 (patch)
tree6535c35d12ba57e09f5c95150f374e5d3d59993e
parent861348bf093096a025bdc4c76021661f8c62f9cc (diff)
downloadpaludis-8f27e941a16cc0bada04630c3d915aa89d181e85.tar.gz
paludis-8f27e941a16cc0bada04630c3d915aa89d181e85.tar.xz
Add QualifiedPackageName to ruby bindings.
-rw-r--r--ruby/Makefile.am10
-rw-r--r--ruby/package_database_entry.cc35
-rw-r--r--ruby/package_database_entry_TEST.rb2
-rw-r--r--ruby/paludis_ruby.cc8
-rw-r--r--ruby/paludis_ruby.hh2
-rw-r--r--ruby/qualified_package_name.cc148
-rw-r--r--ruby/qualified_package_name_TEST.rb97
-rw-r--r--ruby/repository.cc10
8 files changed, 302 insertions, 10 deletions
diff --git a/ruby/Makefile.am b/ruby/Makefile.am
index 9349039..a02053f 100644
--- a/ruby/Makefile.am
+++ b/ruby/Makefile.am
@@ -25,7 +25,8 @@ IF_RUBY_TESTS = \
package_database_TEST.rb \
repository_TEST.rb \
version_metadata_TEST.rb \
- portage_dep_parser_TEST.rb
+ portage_dep_parser_TEST.rb \
+ qualified_package_name_TEST.rb
IF_RUBY_SOURCES = \
paludis_ruby.cc paludis_ruby.hh \
@@ -38,7 +39,8 @@ IF_RUBY_SOURCES = \
package_database.cc \
repository.cc \
version_metadata.cc \
- portage_dep_parser.cc
+ portage_dep_parser.cc \
+ qualified_package_name.cc
EXTRA_DIST = $(IF_RUBY_TESTS) $(IF_RUBY_SOURCES) \
environment_TEST_setup.sh environment_TEST_cleanup.sh \
@@ -74,6 +76,7 @@ OUR_OBJECTS = \
repository.o \
version_metadata.o \
portage_dep_parser.o \
+ qualified_package_name.o \
paludis_ruby.o
TESTS = $(IF_RUBY_TESTS)
@@ -119,6 +122,9 @@ repository.o : repository.cc paludis_ruby.hh
portage_dep_parser.o : portage_dep_parser.cc paludis_ruby.hh
$(OUR_CXXCOMPILE) -o $@ $(srcdir)/portage_dep_parser.cc
+qualified_package_name.o : qualified_package_name.cc paludis_ruby.hh
+ $(OUR_CXXCOMPILE) -o $@ $(srcdir)/qualified_package_name.cc
+
.libs/Paludis.so : $(OUR_OBJECTS)
mkdir -p .libs
$(CXX) -fPIC -shared $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
diff --git a/ruby/package_database_entry.cc b/ruby/package_database_entry.cc
index 485b064..ebab07c 100644
--- a/ruby/package_database_entry.cc
+++ b/ruby/package_database_entry.cc
@@ -90,6 +90,37 @@ namespace
}
}
+ VALUE
+ package_database_entry_name(VALUE self)
+ {
+ try
+ {
+ PackageDatabaseEntry * p;
+ Data_Get_Struct(self, PackageDatabaseEntry, p);
+ return qualified_package_name_to_value(p->name);
+ }
+ catch (const std::exception & e)
+ {
+ exception_to_ruby_exception(e);
+ }
+ }
+
+ VALUE
+ package_database_entry_name_set(VALUE self, VALUE qpn)
+ {
+ try
+ {
+ PackageDatabaseEntry * p;
+ Data_Get_Struct(self, PackageDatabaseEntry, p);
+ p->name = value_to_qualified_package_name(qpn);
+ return self;
+ }
+ catch (const std::exception & e)
+ {
+ exception_to_ruby_exception(e);
+ }
+ }
+
template <typename T_, T_ PackageDatabaseEntry::* m_>
struct EntryValue
{
@@ -126,8 +157,8 @@ namespace
rb_define_method(c_package_database_entry, "<=>", RUBY_FUNC_CAST(&Common<PackageDatabaseEntry>::compare), 1);
rb_include_module(c_package_database_entry, rb_mComparable);
rb_define_method(c_package_database_entry, "to_s", RUBY_FUNC_CAST(&Common<PackageDatabaseEntry>::to_s), 0);
- rb_define_method(c_package_database_entry, "name", RUBY_FUNC_CAST((&EntryValue<QualifiedPackageName, &PackageDatabaseEntry::name>::fetch)), 0);
- rb_define_method(c_package_database_entry, "name=", RUBY_FUNC_CAST((&EntryValue<QualifiedPackageName, &PackageDatabaseEntry::name>::set)), 1);
+ rb_define_method(c_package_database_entry, "name", RUBY_FUNC_CAST(&package_database_entry_name), 0);
+ rb_define_method(c_package_database_entry, "name=", RUBY_FUNC_CAST(&package_database_entry_name_set), 1);
rb_define_method(c_package_database_entry, "version", RUBY_FUNC_CAST(&package_database_entry_version), 0);
rb_define_method(c_package_database_entry, "version=", RUBY_FUNC_CAST(&package_database_entry_version_set), 1);
rb_define_method(c_package_database_entry, "repository", RUBY_FUNC_CAST((&EntryValue<RepositoryName, &PackageDatabaseEntry::repository>::fetch)), 0);
diff --git a/ruby/package_database_entry_TEST.rb b/ruby/package_database_entry_TEST.rb
index 22485bf..99a4fb8 100644
--- a/ruby/package_database_entry_TEST.rb
+++ b/ruby/package_database_entry_TEST.rb
@@ -45,7 +45,7 @@ module Paludis
def test_name
pde = PackageDatabaseEntry.new('foo/bar', VersionSpec.new('1.0-r1'), 'moo')
- assert_instance_of String, pde.name
+ assert_instance_of QualifiedPackageName, pde.name
assert_equal pde.name ,'foo/bar'
end
diff --git a/ruby/paludis_ruby.cc b/ruby/paludis_ruby.cc
index 4e5dd92..fc00272 100644
--- a/ruby/paludis_ruby.cc
+++ b/ruby/paludis_ruby.cc
@@ -42,6 +42,8 @@ namespace
{
static VALUE c_paludis_module;
static VALUE c_name_error;
+ static VALUE c_category_name_part_error;
+ static VALUE c_package_name_part_error;
static VALUE c_bad_version_spec_error;
static VALUE c_package_dep_atom_error;
static VALUE c_package_database_error;
@@ -101,6 +103,10 @@ void paludis::ruby::exception_to_ruby_exception(const std::exception & ee)
dynamic_cast<const paludis::InternalError *>(&ee)->message().c_str(), ee.what());
else if (0 != dynamic_cast<const paludis::BadVersionSpecError *>(&ee))
rb_raise(c_bad_version_spec_error, dynamic_cast<const paludis::BadVersionSpecError *>(&ee)->message().c_str());
+ else if (0 != dynamic_cast<const paludis::PackageNamePartError *>(&ee))
+ rb_raise(c_package_name_part_error, dynamic_cast<const paludis::PackageNamePartError *>(&ee)->message().c_str());
+ else if (0 != dynamic_cast<const paludis::CategoryNamePartError *>(&ee))
+ rb_raise(c_category_name_part_error, dynamic_cast<const paludis::CategoryNamePartError *>(&ee)->message().c_str());
else if (0 != dynamic_cast<const paludis::NameError *>(&ee))
rb_raise(c_name_error, dynamic_cast<const paludis::NameError *>(&ee)->message().c_str());
else if (0 != dynamic_cast<const paludis::PackageDepAtomError *>(&ee))
@@ -165,6 +171,8 @@ extern "C"
{
c_paludis_module = rb_define_module("Paludis");
c_name_error = rb_define_class_under(c_paludis_module, "NameError", rb_eRuntimeError);
+ c_category_name_part_error = rb_define_class_under(c_paludis_module, "CategoryNamePartError", c_name_error);
+ c_package_name_part_error = rb_define_class_under(c_paludis_module, "PackageNamePartError", c_name_error);
c_bad_version_spec_error = rb_define_class_under(c_paludis_module, "BadVersionSpecError", c_name_error);
c_package_dep_atom_error = rb_define_class_under(c_paludis_module, "PackageDepAtomError", rb_eRuntimeError);
c_package_database_error = rb_define_class_under(c_paludis_module, "PackageDatabaseError", rb_eRuntimeError);
diff --git a/ruby/paludis_ruby.hh b/ruby/paludis_ruby.hh
index 752a214..da15c75 100644
--- a/ruby/paludis_ruby.hh
+++ b/ruby/paludis_ruby.hh
@@ -50,10 +50,12 @@ namespace paludis
VALUE version_spec_to_value(const VersionSpec &);
VALUE version_metadata_to_value(VersionMetadata::ConstPointer);
VALUE dep_atom_to_value(DepAtom::ConstPointer);
+ VALUE qualified_package_name_to_value(const QualifiedPackageName &);
VersionSpec value_to_version_spec(VALUE v);
VersionMetadata::ConstPointer value_to_version_metadata(VALUE);
PackageDepAtom::ConstPointer value_to_package_dep_atom(VALUE v);
+ QualifiedPackageName value_to_qualified_package_name(VALUE v);
/* registration */
diff --git a/ruby/qualified_package_name.cc b/ruby/qualified_package_name.cc
new file mode 100644
index 0000000..1079134
--- /dev/null
+++ b/ruby/qualified_package_name.cc
@@ -0,0 +1,148 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org>
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <paludis_ruby.hh>
+#include <paludis/version_spec.hh>
+#include <ruby.h>
+
+using namespace paludis;
+using namespace paludis::ruby;
+
+#define RUBY_FUNC_CAST(x) reinterpret_cast<VALUE (*)(...)>(x)
+
+namespace
+{
+ static VALUE c_qualified_package_name;
+
+ VALUE
+ qualified_package_name_init(int, VALUE *, VALUE self)
+ {
+ return self;
+ }
+
+ VALUE
+ qualified_package_name_new(int argc, VALUE *argv, VALUE self)
+ {
+ QualifiedPackageName * ptr(0);
+ try
+ {
+ if (1 == argc)
+ {
+ ptr = new QualifiedPackageName(STR2CSTR(argv[0]));
+ }
+ else if (2 == argc)
+ {
+ ptr = new QualifiedPackageName(CategoryNamePart(STR2CSTR(argv[0])), PackageNamePart(STR2CSTR(argv[1])));
+ }
+ else
+ {
+ rb_raise(rb_eArgError, "QualifiedPackageName expects one or two arguments, but got %d",argc);
+ }
+ VALUE tdata(Data_Wrap_Struct(self, 0, &Common<QualifiedPackageName>::free, ptr));
+ rb_obj_call_init(tdata,argc, argv);
+ return tdata;
+ }
+ catch (const std::exception & e)
+ {
+ delete ptr;
+ exception_to_ruby_exception(e);
+ }
+ }
+
+ template <typename T_, T_ QualifiedPackageName::* m_>
+ struct QPNMember
+ {
+ static VALUE
+ fetch(VALUE self)
+ {
+ QualifiedPackageName * p;
+ Data_Get_Struct(self, QualifiedPackageName, p);
+ return rb_str_new2(stringify(p->*m_).c_str());
+ }
+
+ static VALUE
+ set(VALUE self, VALUE str)
+ {
+ try
+ {
+ QualifiedPackageName * p;
+ Data_Get_Struct(self, QualifiedPackageName, p);
+ p->*m_ = T_ ((STR2CSTR(str)));
+ return self;
+ }
+ catch (const std::exception & e)
+ {
+ exception_to_ruby_exception(e);
+ }
+ }
+ };
+
+ VALUE
+ qualified_package_name_compare(VALUE left, VALUE right)
+ {
+ try
+ {
+ QualifiedPackageName l = value_to_qualified_package_name(left);
+ QualifiedPackageName r = value_to_qualified_package_name(right);
+ return INT2FIX(paludis::compare(l,r));
+ }
+ catch (const std::exception & e)
+ {
+ exception_to_ruby_exception(e);
+ }
+ }
+
+ void do_register_qualified_package_name()
+ {
+ c_qualified_package_name = rb_define_class_under(paludis_module(), "QualifiedPackageName", rb_cObject);
+ rb_define_singleton_method(c_qualified_package_name, "new", RUBY_FUNC_CAST(&qualified_package_name_new), -1);
+ rb_define_method(c_qualified_package_name, "initialize", RUBY_FUNC_CAST(&qualified_package_name_init), -1);
+ rb_define_method(c_qualified_package_name, "<=>", RUBY_FUNC_CAST(qualified_package_name_compare), 1);
+ rb_include_module(c_qualified_package_name, rb_mComparable);
+ rb_define_method(c_qualified_package_name, "to_s", RUBY_FUNC_CAST(&Common<QualifiedPackageName>::to_s), 0);
+ rb_define_alias(c_qualified_package_name, "to_str", "to_s");
+ rb_define_method(c_qualified_package_name, "category", RUBY_FUNC_CAST((&QPNMember<CategoryNamePart, &QualifiedPackageName::category>::fetch)), 0);
+ rb_define_method(c_qualified_package_name, "category=", RUBY_FUNC_CAST((&QPNMember<CategoryNamePart, &QualifiedPackageName::category>::set)), 1);
+ rb_define_method(c_qualified_package_name, "package", RUBY_FUNC_CAST((&QPNMember<PackageNamePart, &QualifiedPackageName::package>::fetch)), 0);
+ rb_define_method(c_qualified_package_name, "package=", RUBY_FUNC_CAST((&QPNMember<PackageNamePart, &QualifiedPackageName::package>::set)), 1);
+ }
+}
+
+QualifiedPackageName
+paludis::ruby::value_to_qualified_package_name(VALUE v)
+{
+ if (T_STRING == TYPE(v))
+ return QualifiedPackageName(STR2CSTR(v));
+ else
+ {
+ QualifiedPackageName * v_ptr;
+ Data_Get_Struct(v, QualifiedPackageName, v_ptr);
+ return *v_ptr;
+ }
+}
+
+VALUE
+paludis::ruby::qualified_package_name_to_value(const QualifiedPackageName & qpn)
+{
+ QualifiedPackageName * qpn2(new QualifiedPackageName(qpn));
+ return Data_Wrap_Struct(c_qualified_package_name, 0, &Common<QualifiedPackageName>::free, qpn2);
+}
+
+RegisterRubyClass::Register paludis_ruby_register_qualified_package_name PALUDIS_ATTRIBUTE((used))
+ (&do_register_qualified_package_name);
diff --git a/ruby/qualified_package_name_TEST.rb b/ruby/qualified_package_name_TEST.rb
new file mode 100644
index 0000000..b93cce0
--- /dev/null
+++ b/ruby/qualified_package_name_TEST.rb
@@ -0,0 +1,97 @@
+#!/usr/bin/ruby
+# vim: set sw=4 sts=4 et tw=80 :
+
+#
+# Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org>
+#
+# This file is part of the Paludis package manager. Paludis is free software;
+# you can redistribute it and/or modify it under the terms of the GNU General
+# Public License version 2, as published by the Free Software Foundation.
+#
+# Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+# Place, Suite 330, Boston, MA 02111-1307 USA
+#
+
+require 'test/unit'
+require 'Paludis'
+
+module Paludis
+ class TestCase_QualifiedPackageName < Test::Unit::TestCase
+ def test_create
+ qpn = QualifiedPackageName.new('foo-bar','baz')
+ qpn2 = QualifiedPackageName.new('foo-bar/baz')
+ end
+
+ def test_create_error
+ assert_raise TypeError do
+ qpn = QualifiedPackageName.new(0)
+ end
+
+ assert_raise ArgumentError do
+ qpn = QualifiedPackageName.new()
+ end
+
+ assert_raise ArgumentError do
+ qpn = QualifiedPackageName.new('a','b','c')
+ end
+
+ assert_raise CategoryNamePartError do
+ qpn = QualifiedPackageName.new('foo*','baz')
+ end
+
+ assert_raise PackageNamePartError do
+ qpn = QualifiedPackageName.new('foo-bar','baz*')
+ end
+ end
+
+ def test_to_s
+ assert_equal 'foo-bar/baz', QualifiedPackageName.new('foo-bar','baz').to_s
+ end
+
+ def test_compare
+ qpn0 = QualifiedPackageName.new('foo-bar/baz')
+ qpn1 = QualifiedPackageName.new('foo-bar/quux')
+ qpn2 = QualifiedPackageName.new('foo-baz/bar')
+
+ assert_operator qpn0, :<, qpn1
+ assert_operator qpn0, :<, qpn2
+ assert_operator qpn1, :<, qpn2
+
+ assert_operator qpn0, :<=, qpn0
+ assert_operator qpn0, :<=, qpn2
+
+ assert_equal qpn0, 'foo-bar/baz'
+ assert_equal 'foo-bar/baz', qpn0
+ end
+
+ def test_getters
+ cat = 'foo-bar'
+ pkg = 'baz'
+ qpn = QualifiedPackageName.new(cat,pkg)
+
+ assert_equal cat, qpn.category
+ assert_equal pkg, qpn.package
+ end
+
+ def test_setters
+ cat = 'foo-bar'
+ pkg = 'baz'
+ cat_new = 'foo-baz'
+ pkg_new = 'bar'
+ qpn = QualifiedPackageName.new(cat,pkg)
+
+ qpn.category = cat_new
+ assert_equal cat_new, qpn.category
+
+ qpn.package = pkg_new
+ assert_equal pkg_new, qpn.package
+ end
+ end
+end
+
diff --git a/ruby/repository.cc b/ruby/repository.cc
index 510029a..c4bed13 100644
--- a/ruby/repository.cc
+++ b/ruby/repository.cc
@@ -70,7 +70,7 @@ namespace
{
Repository::ConstPointer * self_ptr;
Data_Get_Struct(self, Repository::ConstPointer, self_ptr);
- return (*self_ptr)->has_package_named(QualifiedPackageName(STR2CSTR(name))) ? Qtrue : Qfalse;
+ return (*self_ptr)->has_package_named(value_to_qualified_package_name(name)) ? Qtrue : Qfalse;
}
catch (const std::exception & e)
{
@@ -85,7 +85,7 @@ namespace
{
Repository::ConstPointer * self_ptr;
Data_Get_Struct(self, Repository::ConstPointer, self_ptr);
- return (*self_ptr)->has_version(QualifiedPackageName(STR2CSTR(name)),
+ return (*self_ptr)->has_version(value_to_qualified_package_name(name),
value_to_version_spec(version)) ? Qtrue : Qfalse;
}
catch (const std::exception & e)
@@ -125,7 +125,7 @@ namespace
VALUE result(rb_ary_new());
QualifiedPackageNameCollection::ConstPointer c((*self_ptr)->package_names(category));
for (QualifiedPackageNameCollection::Iterator i(c->begin()), i_end(c->end()) ; i != i_end ; ++i)
- rb_ary_push(result, rb_str_new2(stringify(*i).c_str()));
+ rb_ary_push(result, qualified_package_name_to_value(*i));
return result;
}
catch (const std::exception & e)
@@ -141,7 +141,7 @@ namespace
{
Repository::ConstPointer * self_ptr;
Data_Get_Struct(self, Repository::ConstPointer, self_ptr);
- QualifiedPackageName q(STR2CSTR(qpn));
+ QualifiedPackageName q = value_to_qualified_package_name(qpn);
VALUE result(rb_ary_new());
VersionSpecCollection::ConstPointer c((*self_ptr)->version_specs(q));
@@ -162,7 +162,7 @@ namespace
{
Repository::ConstPointer * self_ptr;
Data_Get_Struct(self, Repository::ConstPointer, self_ptr);
- return version_metadata_to_value((*self_ptr)->version_metadata(QualifiedPackageName(STR2CSTR(name)),
+ return version_metadata_to_value((*self_ptr)->version_metadata(value_to_qualified_package_name(name),
value_to_version_spec(version)));
}
catch (const std::exception & e)