Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Like Philip explained in his answer when using rake the colon defines the seperator between namespaces/tasks</p> <p>When using <code>rails g(enerate)</code> it's basically the same. The difference is that Rails generators aren't defined with rake's DSL, instead they're classes.</p> <p>But to answer your initial Questions: The Colon works as seperator, in both cases, that's it.</p> <p>In the code the only thing what it's important for is splitting up the string: <a href="https://github.com/rails/rails/blob/4-0-stable/railties/lib/rails/generators.rb#L124" rel="nofollow">https://github.com/rails/rails/blob/4-0-stable/railties/lib/rails/generators.rb#L124</a></p> <p>You can find some more infos about generators and how to create your one ones (which definetley will help you understanding the mechanics behind it) in the official <a href="http://guides.rubyonrails.org/generators.html" rel="nofollow">Ruby on Rails Guides</a> </p> <p>//EDIT Ok, let's take a closer look at the generator lookup process:</p> <p>First of all there is <a href="https://github.com/rails/rails/blob/master/railties/lib/rails/generators.rb#L152" rel="nofollow"><code>Rails::Generators.invoke</code></a></p> <p>It receives the namespaces passed on the CLI and split's it up (using the colon)</p> <pre><code> names = namespace.to_s.split(':') </code></pre> <p>then it get's the according class by passing the last part of the passed namespace (the actual generator name) and the remaining part joined with a colon again (the namespace path, in our case it's <em>devise</em>) to <a href="https://github.com/rails/rails/blob/4-0-stable/railties/lib/rails/generators.rb#L124" rel="nofollow"><code>Rails::Generators::Base.find_by_namespace</code></a></p> <pre><code> if klass = find_by_namespace(names.pop, names.any? &amp;&amp; names.join(':')) </code></pre> <p>This method again will join the base (namespace path) and name (generator name) and push it to an array:</p> <pre><code>lookups = [] lookups &lt;&lt; "#{base}:#{name}" if base </code></pre> <p>after that it calls <a href="https://github.com/rails/rails/blob/4-0-stable/railties/lib/rails/generators.rb#L272" rel="nofollow"><code>Rails::Generators.lookup</code></a> which will lookup the classes to invoke for the called generator:</p> <pre><code>lookup(lookups) </code></pre> <p>which will again will call <a href="https://github.com/rails/rails/blob/4-0-stable/railties/lib/rails/generators.rb#L308" rel="nofollow"><code>Rails::Generators.namepaces_to_paths</code></a></p> <p>There's no big magic in that method, it'll simply return an array of two possible source paths for the invoked generator in our case these two are "devise/install/install" and "devise/install".</p> <p>Whereby those aren't the actual paths rails will check, they are only the part's of it that are depend on the namespace:generator construct.</p> <p>The <code>lookup</code> method will now take those two, let's call them subpaths, and check for files to require at the following places:</p> <ol> <li>rails/generators/devise/install/install_generator</li> <li>generators/devise/install/install_generator</li> <li>rails/generators/devise/install_generator</li> <li>generators/devise/install_generator</li> </ol> <p>in our case the second path is the desired file, rails require's it and through that the <a href="https://github.com/rails/rails/blob/4-0-stable/railties/lib/rails/generators/base.rb#L225" rel="nofollow"><code>inherited</code></a> (<a href="http://www.ruby-doc.org/core-2.0/Class.html#method-i-inherited" rel="nofollow">more about that callback</a> callback on <a href="https://github.com/rails/rails/blob/4-0-stable/railties/lib/rails/generators/base.rb" rel="nofollow"><code>Rails::Generators::Base</code></a> will get called since <a href="https://github.com/plataformatec/devise/blob/master/lib/generators/devise/install_generator.rb" rel="nofollow"><code>Devise::Generators::InstallGenerator</code></a> inherits from it.</p> <p>This will add the Class to the <code>subclasses</code> array of <code>Rails::Generators</code> which is mapped to an Hash which will have the format { namespace => klass } and so rails is finally able to get the desired Generator Class</p> <p><a href="https://github.com/rails/rails/blob/4-0-stable/railties/lib/rails/generators.rb#L142" rel="nofollow"><code>klass = namespaces[namespace]</code></a></p> <p>and start it</p> <p><a href="https://github.com/rails/rails/blob/4-0-stable/railties/lib/rails/generators.rb#L156" rel="nofollow"><code>klass.start(args, config)</code></a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload