Forum: Ruby rake namespace selection

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
243ae676bf9fcdbcf0118567bc45f8af?d=identicon&s=25 Peter Pk (peterk)
on 2009-05-19 17:17
is there a way to "explicitly" invoke a block inside the scope of a
parent or explicit "root relative" rake namespace instead of always
being a child of the current namespace ???


namespace :xxx do
  namespace :yyy do
    namespace ...

          ... called_in_unknown_namespace(arg);

    end
  end
end


def called_in_unknown_namespace(arg)

  # execute below block as if it were in "root" namespace
  namespace 'root namespace' do

    # declare additional item to do in root namespace task
    task :task_in_root do
      puts('invoked by "rake task_in_root"');
    end
  end
  # scope restored to wherever you were previously

end

Thanks!
243ae676bf9fcdbcf0118567bc45f8af?d=identicon&s=25 Peter Pk (peterk)
on 2009-05-19 20:41
Peter Pk wrote:
>
> is there a way to "explicitly" invoke a block inside the scope of a
> parent or explicit "root relative" rake namespace instead of always
> being a child of the current namespace ???
>
>
> namespace :xxx do
>   namespace :yyy do
>     namespace ...
>
>           ... called_in_unknown_namespace(arg);
>
>     end
>   end
> end
>
>
> def called_in_unknown_namespace(arg)
>
>   # execute below block as if it were in "root" namespace
>   namespace 'root namespace' do
>
>     # declare additional item to do in root namespace task
>     task :task_in_root do
>       puts('invoked by "rake task_in_root"');
>     end
>   end
>   # scope restored to wherever you were previously
>
> end
>
> Thanks!

Ok - figured it out looking at the source, since all tasks simply have
the "scope name" prepended to their name, and the "namespace" is just a
list of names that defines the currnet scope scope in the task manager,
then I was able to do the below to explicitly selct the "global scope".
building the list from a scope name ie: ':name1:name2:name3', '^name'
using the same format as a task name reference will make it general.
BTW what would be appropriate to specify an "absolute path" in rake
convention ???? since namespace :xxx specifies a relative *child* - ie:
desirably somethign like file paths '/' and '..' maybe '::' ???

module Rake
  module TaskManager
    def in_explicit_namespace(name)
      oldscope = @scope;
      @scope = Array.new();
      # build scope name list from name here
      ns = NameSpace.new(self,@scope);
      yield(ns)
      ns
    ensure
      @scope = oldscope;
    end
  end
end

# down below in a rakefile

namespace :num1 do
  namespace :num2 do
    Rake.application.in_explicit_namespace ':' do
      puts("in explicit block");

      task :x do |t|
        puts(" doing explicit task x");
      end
    end
  end
end

# from command line

$ rake x
  doing explicit task x
$
This topic is locked and can not be replied to.