contraflows/grisaille — comment on commit e97b58da10ff9469c1baf1388c527e4a7b4548e3
File:  line 0
By carl-suster:

This part of the code isn't doing much that's terribly useful, it's just to let us use the pretty syntax to include the components, but it does also take care of some dependencies.

The idea is that when you do `Grisaille.core << :model`, it will attempt to load a file using the pattern declared a few lines down in this file (i.e. not visible in this excerpt) which is `"lib/grisaille/%s" % :model`, but if that file isn't there, you get a `LoadError`. We rescue that and instead guess that the path was referring to a directory and try and load `"lib/grisaille/%s/_core" % :model`. So that seems really convoluted, but it just means that when you want to use the models provided by grisaille, you say `Grisaille.core << :model`, and it will load the core models library. If you look in the file `"lib/grisaille/model/_core.rb"`, you'll see that the actual models are all declare with ruby's `autoload` keyword, which means that the file containing them will be loaded the first time they're mentioned.

You can track the loading of files by looking at the events fired (and logged) as this happens. So for example, try:

$ irb -r./lib/grisaille
> Grisaille.core
# => []
> Grisaille.core << :model
# (two events fired)
# => [:abstract_helpers, :abstract_domain_model]
> Grisaille.models
# => []
> Grisaile::DomainModel::Artist
# (1 event fired)
> Grisaille.models
# => [:artist]

(The bits in comments are what IRB should return after each command).

As for predefined helpers, the only ones used in this excerpt are:

* `require_relative` - a keyword to require a file relative to the current file, with an implicit `.rb` file extension
* `File.expand_path` - expands out `../` and friends in the file path, relative to the second argument
* `__FILE__` - the path of the current ruby file
* `File.join` - sticks path fragments together, adding slashes if needed
* `File.directory?` - is the argument a directory (as opposed to a file)?
* The `a % b` syntax is to substitute the second argument (`b`) into the first string (`a`) at the places where there are markers (like `%s`)

That help?