What is the preferred way (better style) to name a namespace in Ruby? Singular or Plural?
Use:
module FooLib endmodule FooLib::Plugins endclass FooLib::Plugins::Plugin; end #the base for pluginsclass FooLib::Plugins::Bar < FooLib::Plugins::Plugin; endclass FooLib::Plugins::Bar2 < FooLib::Plugins::Plugin; end
or in a different words:
module FooLib module Plugins class Plugin; end #the base for plugins class Bar < Plugin; end class Bar2 < Plugin; end endend
Also arrange the files like this:
- foo_lib/ - plugins/ - plugin.rb - bar.rb - bar2.rb
This is how Rails does it (so this is the Rails Way). I.e. look at the Associations namespace and the Associations::Association class from which all of the classes form the Associations namespace inherits (i.e. Associations::SingularAssociation).
To me FooLib::Plugins
appears like a module, used as a namespace which various plugin classes are kept in. FooLib::Plugin
looks like a superclass for FooLib plugins.
In FooLib::Plugins::Bar
, Bar
definitely seems like the name of a plugin. With FooLib::Plugin::Bar
, I would be doubtful whether Bar
was a helper class used by Foo::Plugin
, or the name of a plugin.
Assuming Plugin
is a base class:
class FooLib::Plugin::Bar < FooLib::Plugin
This is the one I use and recommend.
Bar
is aPlugin
inFooLib
and it inherits fromFooLib::Plugin
. It also keeps the plugins provided by theFooLib
library nested under the namespace of the general class, which reads naturally:# Assign the Bar Plugin of the FooLib library to p.p = FooLib::Plugin::Bar
If I were to develop a third party plugin for your library, I would create the following structure:
# Baz is a Plugin for the FooLib library provided by BarLib.class BarLib::FooLib::Plugin::Baz < ::FooLib::Plugin
Note that I mirror the
FooLib
hierarchy, but underBarLib
's namespace. I would not extend it directly.class FooLib::Plugins::Bar < FooLib::Plugin
I have also used this one, and I think it makes the most sense.
Bar
extendsFooLib::Plugin
and is one of thePlugins
provided byFooLib
. However, it creates a potentially needless module.I think this would be a great choice if
Plugins
was a central plugin repository that implements methods likePlugins.add
,Plugins.all
andPlugins.loaded
.Use it if you can justify the extra module.
class FooLib::Plugins::Bar < FooLib::Plugins
Doesn't make a lot of sense to me.
Bar
is one of thePlugins
inFooLib
, that part looks fine. However, it inherits fromPlugins
. Is it inheriting from more than one plugin? It sounds strange to me; the class name shouldn't suggest something that is impossible.