Class: Virtuatable::Builders::Base
- Inherits:
-
Object
- Object
- Virtuatable::Builders::Base
- Extended by:
- Helpers::Loaders
- Includes:
- RequireAll, Helpers::Controllers, Helpers::Environment, Helpers::Folders, Helpers::Mongoid, Helpers::Registration
- Defined in:
- lib/virtuatable/builders/base.rb
Overview
The base class provides methods to load all elements of the application.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#name ⇒ String
The name of the micro-service.
Attributes included from Helpers::Loaders
Attributes included from Helpers::Registration
Instance Method Summary collapse
-
#all_loaders ⇒ Array<Symbol>
Gets the loaders of the current class and all its ancestors that have loaders.
-
#initialize(locations: caller_locations, path: '.', name:) ⇒ Base
constructor
Constructor of the builder, initializing needed attributes.
-
#load! ⇒ Object
Main method to load the application.
-
#require_folders(*folders) ⇒ Object
Loads a list of folders given as method parameters.
-
#sanitized_ancestors ⇒ Array<Class>
Returns the ancestors of this class without the included modules.
Methods included from Helpers::Loaders
Methods included from Helpers::Registration
Methods included from Helpers::Mongoid
Methods included from Helpers::Folders
Methods included from Helpers::Environment
Methods included from Helpers::Controllers
Constructor Details
#initialize(locations: caller_locations, path: '.', name:) ⇒ Base
Constructor of the builder, initializing needed attributes.
41 42 43 44 45 46 47 |
# File 'lib/virtuatable/builders/base.rb', line 41 def initialize(locations: caller_locations, path: '.', name:) # The base folder of the file calling the builder filedir = File.dirname(locations.first.absolute_path) @directory = File.absolute_path(File.join(filedir, path)) @mode = (ENV['RACK_ENV'] || 'development').to_sym @name = name.to_s end |
Instance Attribute Details
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
26 27 28 |
# File 'lib/virtuatable/builders/base.rb', line 26 def directory @directory end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
30 31 32 |
# File 'lib/virtuatable/builders/base.rb', line 30 def mode @mode end |
#name ⇒ String
Returns the name of the micro-service.
33 34 35 |
# File 'lib/virtuatable/builders/base.rb', line 33 def name @name end |
Instance Method Details
#all_loaders ⇒ Array<Symbol>
Gets the loaders of the current class and all its ancestors that have loaders
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/virtuatable/builders/base.rb', line 78 def all_loaders superclasses = sanitized_ancestors.reject do |ancestor| ancestor == self.class end ancestors_loaders = superclasses.map do |ancestor| ancestor.respond_to?(:loaders) ? ancestor.loaders : [] end flattened_loaders = (ancestors_loaders + self.class.loaders).flatten flattened_loaders.sort_by { |loader| loader[:priority] } end |
#load! ⇒ Object
Main method to load the application. This method is called after creating a builder in the Virtuatable::Application class.
51 52 53 54 55 |
# File 'lib/virtuatable/builders/base.rb', line 51 def load! all_loaders.each do |loader| send(:"load_#{loader[:name]}!") end end |
#require_folders(*folders) ⇒ Object
Loads a list of folders given as method parameters
59 60 61 62 63 64 65 66 |
# File 'lib/virtuatable/builders/base.rb', line 59 def require_folders(*folders) folders.each do |folder| base_path = File.join('.', folder, 'base.rb') require base_path if File.exist?(base_path) path = File.join(directory, folder) require_all(path) if File.directory?(path) end end |
#sanitized_ancestors ⇒ Array<Class>
Returns the ancestors of this class without the included modules. Ruby puts the included modules in the ancestors and we don’t want to search for the loaders in it as we know it won’t be there.
72 73 74 |
# File 'lib/virtuatable/builders/base.rb', line 72 def sanitized_ancestors self.class.ancestors - self.class.included_modules end |