Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby Bindings: Implement Enumerable for iterable collections #1781

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions bindings/libdnf5/advisory.i
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
%module "libdnf5/advisory"
#endif

#if defined(SWIGRUBY)
%mixin libdnf5::advisory::AdvisorySet "Enumerable";
#endif

%include <exception.i>
%include <std_vector.i>

Expand Down Expand Up @@ -57,3 +61,8 @@
%template(VectorAdvisoryReference) std::vector<libdnf5::advisory::AdvisoryReference>;

add_iterator(AdvisorySet)

#if defined(SWIGRUBY)
fix_swigtype_trait(libdnf5::advisory::Advisory)
#endif
add_ruby_each(libdnf5::advisory::AdvisorySet)
36 changes: 36 additions & 0 deletions bindings/libdnf5/common.i
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,42 @@ del ClassName##__iter__
#endif
%enddef

%define fix_swigtype_trait(ClassName)
%traits_swigtype(ClassName)
%fragment(SWIG_Traits_frag(ClassName));
%enddef

%define add_ruby_each(ClassName)
#if defined(SWIGRUBY)
/* Using swig::from method on a class instance (= $self in %extend blocks below) fails.
* We need to define these traits so that it compiles. This seems to be an issue related
* to namespacing:
* https://github.com/swig/swig/issues/973
* and to using a pointer in some places:
* https://github.com/swig/swig/issues/2938
*/
fix_swigtype_trait(ClassName)

%extend ClassName {
VALUE each() {
// If no block is provided, returns Enumerator.
RETURN_ENUMERATOR(swig::from($self), 0, 0);
jackorp marked this conversation as resolved.
Show resolved Hide resolved

VALUE r;
auto i = self->begin();
auto e = self->end();

for (; i != e; ++i) {
r = swig::from(*i);
rb_yield(r);
}

return swig::from($self);
}
}
#endif
%enddef

%define add_str(ClassName)
#if defined(SWIGPYTHON)
%extend ClassName {
Expand Down
14 changes: 14 additions & 0 deletions bindings/libdnf5/rpm.i
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
%module "libdnf5/rpm"
#endif

#if defined(SWIGRUBY)
// Mixin modules for Ruby. This has to be declared before inclusion of the
// related header file.
%mixin libdnf5::rpm::PackageSet "Enumerable";
%mixin libdnf5::rpm::ReldepList "Enumerable";
#endif

%include <exception.i>
%include <std_string.i>
%include <std_vector.i>
Expand Down Expand Up @@ -99,6 +106,13 @@ add_hash(libdnf5::rpm::Package)
add_iterator(PackageSet)
add_iterator(ReldepList)

add_ruby_each(libdnf5::rpm::PackageSet)
// Reldep needs special treatment so that the add_ruby_each can use it.
#if defined(SWIGRUBY)
fix_swigtype_trait(libdnf5::rpm::Reldep)
#endif
add_ruby_each(libdnf5::rpm::ReldepList)

%feature("director") TransactionCallbacks;
%include "libdnf5/rpm/transaction_callbacks.hpp"
wrap_unique_ptr(TransactionCallbacksUniquePtr, libdnf5::rpm::TransactionCallbacks);
Expand Down
30 changes: 28 additions & 2 deletions test/ruby/libdnf5/rpm/test_package_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def test_filter_name()
query.filter_name(["pkg"])
assert_equal(1, query.size())

# TODO(dmach): implement each() so the query can be easily iterated or converted to an array
actual = []
it = query.begin()
while it != query.end()
Expand All @@ -55,7 +54,6 @@ def test_filter_name()
query.filter_name(["pk*"], Common::QueryCmp_GLOB)
assert_equal(2, query.size())

# TODO(dmach): implement each() so the query can be easily iterated or converted to an array
actual = []
it = query.begin()
while it != query.end()
Expand All @@ -65,4 +63,32 @@ def test_filter_name()

assert_equal(["pkg-1.2-3.x86_64", "pkg-libs-1:1.3-4.x86_64"], actual)
end

def test_implements_enumerable()
query = Rpm::PackageQuery.new(@base)
query.filter_name(["pkg"])
assert_equal(1, query.size())

# Using each() without a block should return Enumerator.
assert_instance_of(Enumerator, query.each)

# Using each() with a block should return the collection.
assert_instance_of(Rpm::PackageSet, query.each(&:get_name))

actual_nevra = query.map { |pkg| pkg.get_nevra }

assert_equal(["pkg-1.2-3.x86_64"], actual_nevra)

# ---

query = Rpm::PackageQuery.new(@base)
query.filter_name(["pk*"], Common::QueryCmp_GLOB)
assert_equal(2, query.size())

# Test other method than each that comes with Enumerable
actual = query.select { |pkg| pkg.get_name == "pkg-libs" }

assert_equal(1, actual.size)
assert_equal('pkg-libs-1:1.3-4.x86_64', actual.first.get_nevra)
end
end
Loading