aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar David Leverton <levertond@googlemail.com> 2008-06-01 17:50:26 +0000
committerAvatar David Leverton <levertond@googlemail.com> 2008-06-01 17:50:26 +0000
commit5d02d3bf61e7f3007a7455b1d7ac1e50765829bb (patch)
tree2e9d2a20c7658bf8027e308b60173f07723d3eed
parent53e54484e075d16a8b9de8d418abc3c982079227 (diff)
downloadpaludis-5d02d3bf61e7f3007a7455b1d7ac1e50765829bb.tar.gz
paludis-5d02d3bf61e7f3007a7455b1d7ac1e50765829bb.tar.xz
(ruby) Rubify example_selection.
-rw-r--r--doc/api/ruby/Makefile.am1
-rw-r--r--doc/api/ruby/example_selection.rb62
2 files changed, 63 insertions, 0 deletions
diff --git a/doc/api/ruby/Makefile.am b/doc/api/ruby/Makefile.am
index 37250d1..ce017d2 100644
--- a/doc/api/ruby/Makefile.am
+++ b/doc/api/ruby/Makefile.am
@@ -34,6 +34,7 @@ examples = \
example_mask.rb \
example_match_package.rb \
example_repository.rb \
+ example_selection.rb \
example_version_operator.rb \
example_version_spec.rb
diff --git a/doc/api/ruby/example_selection.rb b/doc/api/ruby/example_selection.rb
new file mode 100644
index 0000000..e794519
--- /dev/null
+++ b/doc/api/ruby/example_selection.rb
@@ -0,0 +1,62 @@
+#!/usr/bin/env ruby
+# vim: set sw=4 sts=4 et tw=100 :
+
+=begin description
+This example demonstrates how to use the standard Selection, Generator and
+Filter classes.
+=end
+
+require 'Paludis'
+require 'example_command_line'
+
+include Paludis
+
+# Run a particular selection, and show its results.
+def show_selection env, selection
+ # Selections support a crude form of stringification.
+ puts "#{selection}:"
+
+ # Usually the only thing clients will do with a Selection object is pass it
+ # to Environment#[].
+ ids = env[selection]
+
+ # Show the results
+ ids.each {|id| puts id}
+ puts
+end
+
+exit_status = 0
+
+# We start with an Environment, respecting the user's '--environment' choice.
+env = EnvironmentMaker.instance.make_from_spec(ExampleCommandLine.instance.environment)
+
+# Make some selections, and display what they give. The selection
+# object used determines the number and ordering of results. In the
+# simplest form, it takes a Generator as a parameter.
+show_selection(env, Selection::AllVersionsSorted.new(
+ Generator::Matches.new(parse_user_package_dep_spec("sys-apps/paludis", []))))
+
+# Generators can be passed through a Filter. The Selection optimises
+# the code internally to avoid doing excess work.
+show_selection(env, Selection::AllVersionsSorted.new(
+ Generator::Matches.new(parse_user_package_dep_spec("sys-apps/paludis", [])) |
+ Filter::SupportsAction.new(InstalledAction)))
+
+# Filters can be combined. Usually Filter::NotMasked should be combined
+# with Filter::SupportsAction.new(InstallAction), since installed packages
+# aren't masked.
+show_selection(env, Selection::AllVersionsSorted.new(
+ Generator::Matches.new(parse_user_package_dep_spec("sys-apps/paludis", [])) |
+ Filter::SupportsAction.new(InstallAction) |
+ Filter::NotMasked.new))
+
+# Selection::AllVersionsSorted can be expensive, particularly if there
+# is no metadata cache. Consider using other Selection objects if
+# you only need the best matching or some arbitrary matching ID.
+show_selection(env, Selection::BestVersionOnly.new(
+ Generator::Matches.new(parse_user_package_dep_spec("sys-apps/paludis", [])) |
+ Filter::SupportsAction.new(InstallAction) |
+ Filter::NotMasked.new))
+
+exit exit_status
+