Class: Virtuatable::Builders::Base

Inherits:
Object
  • Object
show all
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.

Author:

Direct Known Subclasses

Tests

Instance Attribute Summary collapse

Attributes included from Helpers::Loaders

#loaders

Attributes included from Helpers::Registration

#service

Instance Method Summary collapse

Methods included from Helpers::Loaders

declare_loader

Methods included from Helpers::Registration

#load_registration!

Methods included from Helpers::Mongoid

#load_mongoid!

Methods included from Helpers::Folders

#load_folders!

Methods included from Helpers::Environment

#env_file, #load_environment!

Methods included from Helpers::Controllers

#controllers

Constructor Details

#initialize(locations: caller_locations, path: '.', name:) ⇒ Base

Constructor of the builder, initializing needed attributes.

Parameters:

  • locations (Array<String>) (defaults to: caller_locations)

    you should not modify this parameter, it determines from which folder the application is supposed to be loaded.

  • path (String) (defaults to: '.')

    the path from the loaded directory that leads to the root of the application (to find all other files and folders)

  • name (String)

    the name of the loaded service, determining its url.



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

#directoryObject (readonly)

Returns the value of attribute directory.



26
27
28
# File 'lib/virtuatable/builders/base.rb', line 26

def directory
  @directory
end

#modeObject (readonly)

Returns the value of attribute mode.



30
31
32
# File 'lib/virtuatable/builders/base.rb', line 30

def mode
  @mode
end

#nameString

Returns the name of the micro-service.

Returns:

  • (String)

    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_loadersArray<Symbol>

Gets the loaders of the current class and all its ancestors that have loaders

Returns:

  • (Array<Symbol>)

    the name of the loaders declared.



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

Parameters:

  • folders (Array<String>)

    the folders names passed as 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_ancestorsArray<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.

Returns:

  • (Array<Class>)

    the ancestors of the class without included modules.



72
73
74
# File 'lib/virtuatable/builders/base.rb', line 72

def sanitized_ancestors
  self.class.ancestors - self.class.included_modules
end