class Paludis::PaludisEnvironment

An Environment that corresponds to the normal operating evironment.

Public Class Methods

new → PaludisEnvironment click to toggle source
new(config_suffix) → PaludisEnvironment

Create a new PaludisEnvironment, with the specified config suffix if any, otherwise the empty suffix.

VALUE
    paludis_environment_new(int argc, VALUE* argv, VALUE self)
    {
        try
        {
            std::string config_suffix;
            if (1 == argc)
                config_suffix = StringValuePtr(argv[0]);
            else if (0 != argc)
                rb_raise(rb_eArgError, "PaludisEnvironment.new expects one or zero arguments, but got %d", argc);

            std::shared_ptr<Environment> * e = new std::shared_ptr<Environment>(std::make_shared<PaludisEnvironment>(config_suffix));
            VALUE tdata(Data_Wrap_Struct(self, 0, &Common<std::shared_ptr<Environment> >::free, e));
            rb_obj_call_init(tdata, argc, argv);
            return tdata;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     TestEnvironment.new -> TestEnvironment
     *
     * Create a new TestEnvironment.
     */
    VALUE
    test_environment_new(VALUE self)
    {
        try
        {
            std::shared_ptr<Environment> * e = new std::shared_ptr<Environment>(std::make_shared<TestEnvironment>());
            VALUE tdata(Data_Wrap_Struct(self, 0, &Common<std::shared_ptr<Environment> >::free, e));
            rb_obj_call_init(tdata, 0, &self);
            return tdata;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     config_dir -> String
     *
     * Configuration directory used by this PaludisEnvironment.
     */
    VALUE
    paludis_environment_config_dir(VALUE self)
    {
        return rb_str_new2(value_to_paludis_environment(self)->config_dir().c_str());
    }

    /*
     * call-seq:
     *     create(spec) -> Environment
     *
     * Create an environment from the given spec.
     * A spec consisits of <b>class:suffix</b> both of which may be omitted. <b>class</b> is the environment class,
     * e.g. paludis or portage, <b>suffix</b> is the configuration directory suffix.
     *
     */
    VALUE
    environment_factory_create(VALUE, VALUE spec)
    {
        try
        {
            std::shared_ptr<Environment> * e = new std::shared_ptr<Environment>(EnvironmentFactory::get_instance()->create(
                        StringValuePtr(spec)));

            VALUE tdata(Data_Wrap_Struct(c_environment, 0, &Common<std::shared_ptr<Environment> >::free, e));
            return tdata;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     fetch_unique_qualified_package_name(package_name) -> QualifiedPackageName
     *     fetch_unique_qualified_package_name(package_name, filter) -> QualifiedPackageName
     *
     * Disambiguate a package name.  If a filter is specified, limit
     * the potential results to packages that match.
     */
    VALUE
    environment_fetch_unique_qualified_package_name(int argc, VALUE *argv, VALUE self)
    {
        try
        {
            if (1 == argc || 2 == argc)
            {
                std::shared_ptr<Environment> * self_ptr;
                Data_Get_Struct(self, std::shared_ptr<Environment>, self_ptr);
                return qualified_package_name_to_value((*self_ptr)->fetch_unique_qualified_package_name(
                                PackageNamePart(StringValuePtr(argv[0])), 2 == argc ? value_to_filter(argv[1]) : filter::All()));
            }
            else
                rb_raise(rb_eArgError, "fetch_unique_qualified_package_name expects one or two arguments, but got %d",argc);
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     repositories -> Array
     *     repositories {|repository| block } -> Nil
     *
     *  Returns all the repositories in the package database, either as an Array, or as
     *  the parameters to a block.
     */
    VALUE
    environment_repositories(VALUE self)
    {
        try
        {
            std::shared_ptr<Environment> * self_ptr;
            Data_Get_Struct(self, std::shared_ptr<Environment>, self_ptr);

            if (rb_block_given_p())
            {
                for (Environment::RepositoryConstIterator r((*self_ptr)->begin_repositories()),
                        r_end((*self_ptr)->end_repositories()) ; r != r_end ; ++r)
                    rb_yield(repository_to_value(*r));
                return Qnil;
            }
            VALUE result(rb_ary_new());
            for (Environment::RepositoryConstIterator r((*self_ptr)->begin_repositories()),
                    r_end((*self_ptr)->end_repositories()) ; r != r_end ; ++r)
                rb_ary_push(result, repository_to_value(*r));

            return result;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     fetch_repository(repository_name) -> Repository
     *
     *  Fetch a named repository.
     */
    VALUE
    environment_fetch_repository(VALUE self, VALUE name)
    {
        try
        {
            std::shared_ptr<Environment> * self_ptr;
            Data_Get_Struct(self, std::shared_ptr<Environment>, self_ptr);

            return repository_to_value((*self_ptr)->fetch_repository(RepositoryName(StringValuePtr(name))));
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     more_important_than(repository_name_a, repository_name_b) -> bool
     *
     * True if repository_name_a is more important than repository_name_b .
     */
    VALUE
    environment_more_important_than(VALUE self, VALUE name1, VALUE name2)
    {
        try
        {
            std::shared_ptr<Environment> * self_ptr;
            Data_Get_Struct(self, std::shared_ptr<Environment>, self_ptr);

            return (*self_ptr)->more_important_than(RepositoryName(StringValuePtr(name1)),
                    RepositoryName(StringValuePtr(name2))) ? Qtrue : Qfalse;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     has_repository_named?(repository_name) -> true or false
     *
     *  Do we have a named repository?
     */
    VALUE
    environment_has_repository_named(VALUE self, VALUE name)
    {
        try
        {
            std::shared_ptr<Environment> * self_ptr;
            Data_Get_Struct(self, std::shared_ptr<Environment>, self_ptr);

            return ((*self_ptr)->has_repository_named(RepositoryName(StringValuePtr(name)))) ? true : false;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     add_repository(importance, repository) -> nil
     *
     *  Add a repository.
     */
    VALUE
    environment_add_repository(VALUE self, VALUE importance, VALUE repo_v)
    {
        try
        {
            std::shared_ptr<Environment> * self_ptr;
            Data_Get_Struct(self, std::shared_ptr<Environment>, self_ptr);

            std::shared_ptr<Repository> repo(value_to_repository(repo_v));

            (*self_ptr)->add_repository(NUM2INT(importance), repo);
            return Qnil;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }


    void do_register_environment()
    {
        rb_require("singleton");

        /*
         * Document-class: Paludis::Environment
         *
         * Represents a working environment, which contains an available packages database and provides
         * various methods for querying package visibility and options.
         */
        c_environment = rb_define_class_under(c_paludis_module, "Environment", rb_cObject);
        rb_funcall(c_environment, rb_intern("private_class_method"), 1, rb_str_new2("new"));
        rb_define_method(c_environment, "set", environment_set, 1);
        rb_define_method(c_environment, "distribution", environment_distribution, 0);
        rb_define_method(c_environment, "accept_license", environment_accept_license, 2);
        rb_define_method(c_environment, "accept_keywords", environment_accept_keywords, 2);
        rb_define_method(c_environment, "mirrors", environment_mirrors, 1);
        rb_define_method(c_environment, "[]", environment_square_brackets, 1);
        rb_define_method(c_environment, "format_key",                EnvironmentKey<MetadataValueKey<std::string> , Environment::format_key>::fetch, 0);
        rb_define_method(c_environment, "config_location_key",                template_methods, 0);
        rb_define_method(c_environment, "preferred_root_key",                template_methods, 0);
        rb_define_method(c_environment, "fetch_unique_qualified_package_name",                environment_fetch_unique_qualified_package_name, -1);
        rb_define_method(c_environment, "repositories",                environment_repositories, 0);
        rb_define_method(c_environment, "fetch_repository",                environment_fetch_repository, 1);
        rb_define_method(c_environment, "more_important_than",                environment_more_important_than, 2);
        rb_define_method(c_environment, "has_repository_named?",                environment_has_repository_named, 1);
        rb_define_method(c_environment, "add_repository",                environment_add_repository, 2);

        /*
         * Document-class: Paludis::PaludisEnvironment
         *
         * An Environment that corresponds to the normal operating evironment.
         */
        c_paludis_environment = rb_define_class_under(c_paludis_module, "PaludisEnvironment", c_environment);
        rb_define_singleton_method(c_paludis_environment, "new", paludis_environment_new, -1);
        rb_define_method(c_paludis_environment, "initialize", paludis_environment_init, -1);
        rb_define_method(c_paludis_environment, "config_dir", paludis_environment_config_dir, 0);

        /*
         * Document-class: Paludis::TestEnvironment
         *
         * A crude test environment.
         */
        c_test_environment = rb_define_class_under(c_paludis_module, "TestEnvironment", c_environment);
        rb_define_singleton_method(c_test_environment, "new", test_environment_new, 0);

        /*
         * Document-class: Paludis::EnvironmentFactory
         *
         * A class that holds methods to create environments.
         *
         * To access the default environment use create("")
         */
        c_environment_factory = rb_define_class_under(c_paludis_module, "EnvironmentFactory", rb_cObject);
        rb_funcall(rb_const_get(rb_cObject, rb_intern("Singleton")), rb_intern("included"), 1, c_environment_factory);
        rb_define_method(c_environment_factory, "create", environment_factory_create, 1);
    }
}

Public Instance Methods

config_dir → String click to toggle source

Configuration directory used by this PaludisEnvironment.

VALUE
    paludis_environment_config_dir(VALUE self)
    {
        return rb_str_new2(value_to_paludis_environment(self)->config_dir().c_str());
    }

    /*
     * call-seq:
     *     create(spec) -> Environment
     *
     * Create an environment from the given spec.
     * A spec consisits of <b>class:suffix</b> both of which may be omitted. <b>class</b> is the environment class,
     * e.g. paludis or portage, <b>suffix</b> is the configuration directory suffix.
     *
     */
    VALUE
    environment_factory_create(VALUE, VALUE spec)
    {
        try
        {
            std::shared_ptr<Environment> * e = new std::shared_ptr<Environment>(EnvironmentFactory::get_instance()->create(
                        StringValuePtr(spec)));

            VALUE tdata(Data_Wrap_Struct(c_environment, 0, &Common<std::shared_ptr<Environment> >::free, e));
            return tdata;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     fetch_unique_qualified_package_name(package_name) -> QualifiedPackageName
     *     fetch_unique_qualified_package_name(package_name, filter) -> QualifiedPackageName
     *
     * Disambiguate a package name.  If a filter is specified, limit
     * the potential results to packages that match.
     */
    VALUE
    environment_fetch_unique_qualified_package_name(int argc, VALUE *argv, VALUE self)
    {
        try
        {
            if (1 == argc || 2 == argc)
            {
                std::shared_ptr<Environment> * self_ptr;
                Data_Get_Struct(self, std::shared_ptr<Environment>, self_ptr);
                return qualified_package_name_to_value((*self_ptr)->fetch_unique_qualified_package_name(
                                PackageNamePart(StringValuePtr(argv[0])), 2 == argc ? value_to_filter(argv[1]) : filter::All()));
            }
            else
                rb_raise(rb_eArgError, "fetch_unique_qualified_package_name expects one or two arguments, but got %d",argc);
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     repositories -> Array
     *     repositories {|repository| block } -> Nil
     *
     *  Returns all the repositories in the package database, either as an Array, or as
     *  the parameters to a block.
     */
    VALUE
    environment_repositories(VALUE self)
    {
        try
        {
            std::shared_ptr<Environment> * self_ptr;
            Data_Get_Struct(self, std::shared_ptr<Environment>, self_ptr);

            if (rb_block_given_p())
            {
                for (Environment::RepositoryConstIterator r((*self_ptr)->begin_repositories()),
                        r_end((*self_ptr)->end_repositories()) ; r != r_end ; ++r)
                    rb_yield(repository_to_value(*r));
                return Qnil;
            }
            VALUE result(rb_ary_new());
            for (Environment::RepositoryConstIterator r((*self_ptr)->begin_repositories()),
                    r_end((*self_ptr)->end_repositories()) ; r != r_end ; ++r)
                rb_ary_push(result, repository_to_value(*r));

            return result;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     fetch_repository(repository_name) -> Repository
     *
     *  Fetch a named repository.
     */
    VALUE
    environment_fetch_repository(VALUE self, VALUE name)
    {
        try
        {
            std::shared_ptr<Environment> * self_ptr;
            Data_Get_Struct(self, std::shared_ptr<Environment>, self_ptr);

            return repository_to_value((*self_ptr)->fetch_repository(RepositoryName(StringValuePtr(name))));
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     more_important_than(repository_name_a, repository_name_b) -> bool
     *
     * True if repository_name_a is more important than repository_name_b .
     */
    VALUE
    environment_more_important_than(VALUE self, VALUE name1, VALUE name2)
    {
        try
        {
            std::shared_ptr<Environment> * self_ptr;
            Data_Get_Struct(self, std::shared_ptr<Environment>, self_ptr);

            return (*self_ptr)->more_important_than(RepositoryName(StringValuePtr(name1)),
                    RepositoryName(StringValuePtr(name2))) ? Qtrue : Qfalse;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     has_repository_named?(repository_name) -> true or false
     *
     *  Do we have a named repository?
     */
    VALUE
    environment_has_repository_named(VALUE self, VALUE name)
    {
        try
        {
            std::shared_ptr<Environment> * self_ptr;
            Data_Get_Struct(self, std::shared_ptr<Environment>, self_ptr);

            return ((*self_ptr)->has_repository_named(RepositoryName(StringValuePtr(name)))) ? true : false;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     add_repository(importance, repository) -> nil
     *
     *  Add a repository.
     */
    VALUE
    environment_add_repository(VALUE self, VALUE importance, VALUE repo_v)
    {
        try
        {
            std::shared_ptr<Environment> * self_ptr;
            Data_Get_Struct(self, std::shared_ptr<Environment>, self_ptr);

            std::shared_ptr<Repository> repo(value_to_repository(repo_v));

            (*self_ptr)->add_repository(NUM2INT(importance), repo);
            return Qnil;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }


    void do_register_environment()
    {
        rb_require("singleton");

        /*
         * Document-class: Paludis::Environment
         *
         * Represents a working environment, which contains an available packages database and provides
         * various methods for querying package visibility and options.
         */
        c_environment = rb_define_class_under(c_paludis_module, "Environment", rb_cObject);
        rb_funcall(c_environment, rb_intern("private_class_method"), 1, rb_str_new2("new"));
        rb_define_method(c_environment, "set", environment_set, 1);
        rb_define_method(c_environment, "distribution", environment_distribution, 0);
        rb_define_method(c_environment, "accept_license", environment_accept_license, 2);
        rb_define_method(c_environment, "accept_keywords", environment_accept_keywords, 2);
        rb_define_method(c_environment, "mirrors", environment_mirrors, 1);
        rb_define_method(c_environment, "[]", environment_square_brackets, 1);
        rb_define_method(c_environment, "format_key",                EnvironmentKey<MetadataValueKey<std::string> , Environment::format_key>::fetch, 0);
        rb_define_method(c_environment, "config_location_key",                template_methods, 0);
        rb_define_method(c_environment, "preferred_root_key",                template_methods, 0);
        rb_define_method(c_environment, "fetch_unique_qualified_package_name",                environment_fetch_unique_qualified_package_name, -1);
        rb_define_method(c_environment, "repositories",                environment_repositories, 0);
        rb_define_method(c_environment, "fetch_repository",                environment_fetch_repository, 1);
        rb_define_method(c_environment, "more_important_than",                environment_more_important_than, 2);
        rb_define_method(c_environment, "has_repository_named?",                environment_has_repository_named, 1);
        rb_define_method(c_environment, "add_repository",                environment_add_repository, 2);

        /*
         * Document-class: Paludis::PaludisEnvironment
         *
         * An Environment that corresponds to the normal operating evironment.
         */
        c_paludis_environment = rb_define_class_under(c_paludis_module, "PaludisEnvironment", c_environment);
        rb_define_singleton_method(c_paludis_environment, "new", paludis_environment_new, -1);
        rb_define_method(c_paludis_environment, "initialize", paludis_environment_init, -1);
        rb_define_method(c_paludis_environment, "config_dir", paludis_environment_config_dir, 0);

        /*
         * Document-class: Paludis::TestEnvironment
         *
         * A crude test environment.
         */
        c_test_environment = rb_define_class_under(c_paludis_module, "TestEnvironment", c_environment);
        rb_define_singleton_method(c_test_environment, "new", test_environment_new, 0);

        /*
         * Document-class: Paludis::EnvironmentFactory
         *
         * A class that holds methods to create environments.
         *
         * To access the default environment use create("")
         */
        c_environment_factory = rb_define_class_under(c_paludis_module, "EnvironmentFactory", rb_cObject);
        rb_funcall(rb_const_get(rb_cObject, rb_intern("Singleton")), rb_intern("included"), 1, c_environment_factory);
        rb_define_method(c_environment_factory, "create", environment_factory_create, 1);
    }
}