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

#instance, #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

#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:

  • directory (String)

    the directory from which load the application.



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

#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.



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.

Raises:



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

Parameters:

  • *folders (Array<String>)

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



77
78
79
# File 'lib/virtuatable/builders/base.rb', line 77

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

#typeSymbol

Returns the type of the instance, default being a UNIX server

Returns:

  • (Symbol)

    the type of instance currently loading.



64
65
66
# File 'lib/virtuatable/builders/base.rb', line 64

def type
  ENV['INSTANCE_TYPE'].nil? ? :unix : ENV['INSTANCE_TYPE'].to_sym
end