aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-11-06 13:42:46 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-11-06 13:42:46 +0000
commitc06d033ee6a41492a338b65e73aa48abd0a1a775 (patch)
tree542116acdb3f814663f88c6bc1c637fe8be1f4c5
parent478f21572fba5d4130530070d8d89aa3861fdd9e (diff)
downloadpaludis-c06d033ee6a41492a338b65e73aa48abd0a1a775.tar.gz
paludis-c06d033ee6a41492a338b65e73aa48abd0a1a775.tar.xz
(ruby) Add missing PackageDepSpec methods to Ruby. Implement example_dep_spec in Ruby.
-rw-r--r--doc/api/index.html.part2
-rw-r--r--doc/api/ruby/Makefile.am3
-rw-r--r--doc/api/ruby/example_dep_spec.rb98
-rw-r--r--ruby/dep_spec.cc63
-rw-r--r--ruby/dep_spec_TEST.rb37
5 files changed, 198 insertions, 5 deletions
diff --git a/doc/api/index.html.part b/doc/api/index.html.part
index 945ad19..f281693 100644
--- a/doc/api/index.html.part
+++ b/doc/api/index.html.part
@@ -51,7 +51,7 @@ how concepts map on to different language bindings.</p>
<tr>
<td>example_dep_spec</td>
<td><a href="cplusplus/example__dep__spec_8cc-example.html">C++</a></td>
- <td></td>
+ <td><a href="ruby/example_dep_spec.html">Ruby</a></td>
<td></td>
</tr>
<tr>
diff --git a/doc/api/ruby/Makefile.am b/doc/api/ruby/Makefile.am
index 3a70e28..b7e305b 100644
--- a/doc/api/ruby/Makefile.am
+++ b/doc/api/ruby/Makefile.am
@@ -25,7 +25,8 @@ examples = \
example_command_line.rb \
example_about.rb \
example_action.rb \
- example_contents.rb
+ example_contents.rb \
+ example_dep_spec.rb
EXTRA_DIST = $(examples)
diff --git a/doc/api/ruby/example_dep_spec.rb b/doc/api/ruby/example_dep_spec.rb
new file mode 100644
index 0000000..6fa6ff6
--- /dev/null
+++ b/doc/api/ruby/example_dep_spec.rb
@@ -0,0 +1,98 @@
+#!/usr/bin/env ruby
+# vim: set sw=4 sts=4 et tw=100 :
+
+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)
+
+# For each command line parameter:
+ARGV.each do | arg |
+ # Create a PackageDepSpec from the parameter. For user-inputted data,
+ # PackageDepSpecParseMode::Permissive or PackageDepSpecParseMode::Unspecific should be used
+ # (only the latter allows wildcards).
+ spec = PackageDepSpec.new(arg, PackageDepSpecParseMode::Unspecific)
+
+ # Display information about the PackageDepSpec.
+ puts "Information about '#{spec}':"
+
+ if spec.package
+ puts " Package: #{spec.package}"
+ end
+
+ if spec.category_name_part
+ puts " Category part: #{spec.category_name_part}"
+ end
+
+ if spec.package_name_part
+ puts " Package part: #{spec.package_name_part}"
+ end
+
+ if spec.version_requirements and not spec.version_requirements.empty?
+ print " Version requirements: "
+ need_join = false
+ spec.version_requirements.each do | r |
+ if need_join
+ case spec.version_requirements_mode
+ when VersionRequirementsMode::And
+ print " and "
+ when VersionRequirementsMode::Or
+ print " or "
+ end
+ end
+
+ print r[:operator], r[:spec]
+ need_join = true
+ end
+ puts
+ end
+
+ if spec.slot
+ puts " Slot: #{spec.slot}"
+ end
+
+ if spec.repository
+ puts " Repository: #{spec.repository}"
+ end
+
+ if spec.use_requirements and not spec.use_requirements.empty?
+ print " Use requirements: "
+ need_join = false
+ spec.use_requirements.each do | u |
+ if need_join
+ print " and "
+ end
+
+ if not u[:state]
+ print "-"
+ end
+
+ print u[:flag]
+ need_join = true;
+ end
+ puts
+ end
+
+ # And display packages matching that spec
+ print " Matches: "
+ ids = env.package_database.query(Query::Matches.new(spec), QueryOrder::OrderByVersion)
+ need_indent = false
+ ids.each do | id |
+ if need_indent
+ puts
+ print " "
+ end
+ print id
+ need_indent = true
+ end
+ puts
+ puts
+end
+
+exit exit_status
+
diff --git a/ruby/dep_spec.cc b/ruby/dep_spec.cc
index 6d97cf6..9a788a1 100644
--- a/ruby/dep_spec.cc
+++ b/ruby/dep_spec.cc
@@ -206,6 +206,38 @@ namespace
/*
* call-seq:
+ * package_name_part -> String or Nil
+ *
+ * Fetch the package name part.
+ */
+ VALUE
+ package_dep_spec_package_name_part(VALUE self)
+ {
+ tr1::shared_ptr<const PackageDepSpec> * ptr;
+ Data_Get_Struct(self, tr1::shared_ptr<const PackageDepSpec>, ptr);
+ if (0 == (*ptr)->package_name_part_ptr())
+ return Qnil;
+ return rb_str_new2(stringify(*(*ptr)->package_name_part_ptr()).c_str());
+ }
+
+ /*
+ * call-seq:
+ * category_name_part -> String or Nil
+ *
+ * Fetch the category name part.
+ */
+ VALUE
+ package_dep_spec_category_name_part(VALUE self)
+ {
+ tr1::shared_ptr<const PackageDepSpec> * ptr;
+ Data_Get_Struct(self, tr1::shared_ptr<const PackageDepSpec>, ptr);
+ if (0 == (*ptr)->category_name_part_ptr())
+ return Qnil;
+ return rb_str_new2(stringify(*(*ptr)->category_name_part_ptr()).c_str());
+ }
+
+ /*
+ * call-seq:
* text -> String
*
* Fetch our text.
@@ -277,6 +309,34 @@ namespace
return result;
}
+ /*
+ * call-seq:
+ * use_requirements -> Array
+ *
+ * Fetch the use requirements. E.g. [ {:flag => 'a', :state => true } ]
+ */
+ VALUE
+ package_dep_spec_use_requirements(VALUE self)
+ {
+ tr1::shared_ptr<const PackageDepSpec> * ptr;
+ Data_Get_Struct(self, tr1::shared_ptr<const PackageDepSpec>, ptr);
+ VALUE result(rb_ary_new());
+ VALUE result_hash;
+ if ((*ptr)->use_requirements_ptr())
+ for (UseRequirements::ConstIterator i((*ptr)->use_requirements_ptr()->begin()),
+ i_end((*ptr)->use_requirements_ptr()->end()) ; i != i_end; ++i)
+ {
+ result_hash = rb_hash_new();
+ rb_hash_aset(result_hash, ID2SYM(rb_intern("flag")),
+ rb_str_new2(stringify(i->first).c_str()));
+ rb_hash_aset(result_hash, ID2SYM(rb_intern("state")),
+ i->second == use_disabled ? Qfalse : Qtrue);
+
+ rb_ary_push(result, result_hash);
+ }
+ return result;
+ }
+
VALUE
package_dep_spec_version_requirements_mode(VALUE self)
{
@@ -340,11 +400,14 @@ namespace
rb_define_method(c_package_dep_spec, "initialize", RUBY_FUNC_CAST(&package_dep_spec_init), -1);
rb_define_method(c_package_dep_spec, "to_s", RUBY_FUNC_CAST(&Common<tr1::shared_ptr<const PackageDepSpec> >::to_s_via_ptr), 0);
rb_define_method(c_package_dep_spec, "package", RUBY_FUNC_CAST(&package_dep_spec_package), 0);
+ rb_define_method(c_package_dep_spec, "package_name_part", RUBY_FUNC_CAST(&package_dep_spec_package_name_part), 0);
+ rb_define_method(c_package_dep_spec, "category_name_part", RUBY_FUNC_CAST(&package_dep_spec_category_name_part), 0);
rb_define_method(c_package_dep_spec, "text", RUBY_FUNC_CAST(&package_dep_spec_text), 0);
rb_define_method(c_package_dep_spec, "slot", RUBY_FUNC_CAST(&package_dep_spec_slot_ptr), 0);
rb_define_method(c_package_dep_spec, "repository", RUBY_FUNC_CAST(&package_dep_spec_repository_ptr), 0);
rb_define_method(c_package_dep_spec, "version_requirements", RUBY_FUNC_CAST(&package_dep_spec_version_requirements_ptr), 0);
rb_define_method(c_package_dep_spec, "version_requirements_mode", RUBY_FUNC_CAST(&package_dep_spec_version_requirements_mode), 0);
+ rb_define_method(c_package_dep_spec, "use_requirements", RUBY_FUNC_CAST(&package_dep_spec_use_requirements), 0);
/*
* Document-class: Paludis::PlainTextDepSpec
diff --git a/ruby/dep_spec_TEST.rb b/ruby/dep_spec_TEST.rb
index 36b2283..03de04a 100644
--- a/ruby/dep_spec_TEST.rb
+++ b/ruby/dep_spec_TEST.rb
@@ -41,11 +41,16 @@ module Paludis
class TestCase_PackageDepSpec < Test::Unit::TestCase
def pda
- PackageDepSpec.new('>=foo/bar-1:100::testrepo', PackageDepSpecParseMode::Permissive)
+ PackageDepSpec.new('>=foo/bar-1:100::testrepo[a][-b]', PackageDepSpecParseMode::Permissive)
+ end
+
+ def pdb
+ PackageDepSpec.new('*/bar', PackageDepSpecParseMode::Unspecific)
end
def test_create
pda
+ pdb
end
def test_create_error
@@ -58,23 +63,38 @@ module Paludis
end
def test_to_s
- assert_equal ">=foo/bar-1:100::testrepo", pda.to_s
+ assert_equal ">=foo/bar-1:100::testrepo[a][-b]", pda.to_s
+ assert_equal "*/bar", pdb.to_s
end
def test_text
- assert_equal ">=foo/bar-1:100::testrepo", pda.text
+ assert_equal ">=foo/bar-1:100::testrepo[a][-b]", pda.text
+ assert_equal "*/bar", pdb.text
end
def test_slot
assert_equal "100", pda.slot
+ assert_nil pdb.slot
end
def test_package
assert_equal "foo/bar", pda.package
+ assert_nil pdb.package
end
def test_repository
assert_equal "testrepo", pda.repository
+ assert_nil pdb.package
+ end
+
+ def test_package_name_part
+ assert_nil pda.package_name_part
+ assert_equal "bar", pdb.package_name_part
+ end
+
+ def test_category_name_part
+ assert_nil pda.category_name_part
+ assert_nil pdb.category_name_part
end
def test_version_requirements
@@ -88,6 +108,17 @@ module Paludis
assert_kind_of Fixnum, pda.version_requirements_mode
assert_equal VersionRequirementsMode::And, pda.version_requirements_mode
end
+
+ def test_use_requirements
+ assert_kind_of Array, pda.use_requirements
+ assert_equal 2, pda.use_requirements.size
+
+ assert_equal 'a', pda.use_requirements[0][:flag]
+ assert_equal true, pda.use_requirements[0][:state]
+
+ assert_equal 'b', pda.use_requirements[1][:flag]
+ assert_equal false, pda.use_requirements[1][:state]
+ end
end
class TestCase_PlainTextDepSpec < Test::Unit::TestCase