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.
-
#check_variables! ⇒ Object
Checks the presence of all the needed environment variables.
-
#initialize(locations: caller_locations, path: '.', name:) ⇒ Base
constructor
Constructor of the builder, initializing needed attributes.
- #load! ⇒ Object
-
#require_folders(*folders) ⇒ Object
Loads a list of folders given as method parameters.
- #sanitized_ancestors ⇒ Object
-
#type ⇒ Symbol
Returns the type of the instance, default being a UNIX server.
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.
37 38 39 40 41 42 43 |
# File 'lib/virtuatable/builders/base.rb', line 37 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 = :development @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
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/virtuatable/builders/base.rb', line 83 def all_loaders superclasses = sanitized_ancestors.select 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 |
#check_variables! ⇒ Object
Checks the presence of all the needed environment variables.
54 55 56 57 58 59 60 |
# File 'lib/virtuatable/builders/base.rb', line 54 def check_variables! names = ['INSTANCE_TYPE'] names.each do |varname| exception_klass = Virtuatable::Builders::Errors::MissingEnv raise exception_klass.new(variable: varname) unless ENV.key?(varname) end end |
#load! ⇒ Object
45 46 47 48 49 |
# File 'lib/virtuatable/builders/base.rb', line 45 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
70 71 72 73 74 75 |
# File 'lib/virtuatable/builders/base.rb', line 70 def require_folders(*folders) folders.each do |folder| path = File.join(directory, folder) require_all(path) if File.directory?(path) end end |
#sanitized_ancestors ⇒ Object
77 78 79 |
# File 'lib/virtuatable/builders/base.rb', line 77 def sanitized_ancestors self.class.ancestors - self.class.included_modules end |
#type ⇒ Symbol
Returns the type of the instance, default being a UNIX server
64 65 66 |
# File 'lib/virtuatable/builders/base.rb', line 64 def type ENV['INSTANCE_TYPE'].nil? ? :unix : ENV['INSTANCE_TYPE'].to_sym end |