Module: Treat::Autoload
- Defined in:
- lib/treat/autoload.rb
Overview
Basic mixin for all the main modules; takes care of requiring the right files in the right order for each one.
If a module’s folder (e.g. /entities) contains a file with a corresponding singular name (e.g. /entity), that base class is required first. Then, all the files that are found directly under that folder are required (but not those found in sub-folders).
Class Method Summary collapse
-
.get_module_name(mod) ⇒ Object
Return the downcased form of the module’s last name (e.g. “entities”).
-
.get_module_path(name) ⇒ Object
Returns the path to a module’s dir.
-
.included(base) ⇒ Object
Loads all the files for the base module in the appropriate order.
-
.singularize(w) ⇒ Object
Helper method to singularize words.
Class Method Details
.get_module_name(mod) ⇒ Object
Return the downcased form of the module’s last name (e.g. “entities”).
34 35 36 |
# File 'lib/treat/autoload.rb', line 34 def self.get_module_name(mod) mod.to_s.split('::')[-1].downcase end |
.get_module_path(name) ⇒ Object
Returns the path to a module’s dir.
26 27 28 29 30 |
# File 'lib/treat/autoload.rb', line 26 def self.get_module_path(name) file = File.(__FILE__) dirs = File.dirname(file).split('/') File.join(*dirs[0..-1], name) end |
.included(base) ⇒ Object
Loads all the files for the base module in the appropriate order.
16 17 18 19 20 21 22 23 |
# File 'lib/treat/autoload.rb', line 16 def self.included(base) m = self.get_module_name(base) d = self.get_module_path(m) n = self.singularize(m) + '.rb' f, p = File.join(d, n), "#{d}/*.rb" require f if File.readable?(f) Dir.glob(p).each { |f| require f } end |
.singularize(w) ⇒ Object
Helper method to singularize words.
39 40 41 42 |
# File 'lib/treat/autoload.rb', line 39 def self.singularize(w) if w[-3..-1] == 'ies'; w[0..-4] + 'y' else; (w[-1] == 's' ? w[0..-2] : w); end end |