Class: Carioca::Registry

Inherits:
Object
  • Object
show all
Includes:
Helpers, Singleton
Defined in:
lib/carioca/registry.rb

Constant Summary collapse

@@config =
Configuration.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#debug, #i18n, #log, #search_file_in_gem

Instance Attribute Details

#active_servicesObject

Returns the value of attribute active_services.



22
23
24
# File 'lib/carioca/registry.rb', line 22

def active_services
  @active_services
end

#servicesObject

Returns the value of attribute services.



22
23
24
# File 'lib/carioca/registry.rb', line 22

def services
  @services
end

Class Method Details

.configObject



9
10
11
# File 'lib/carioca/registry.rb', line 9

def self.config
  @@config
end

.configure {|@@config| ... } ⇒ Object

Yields:



13
14
15
# File 'lib/carioca/registry.rb', line 13

def self.configure
  yield(@@config)
end

Instance Method Details

#add(service:, definition:, skip_validation: false) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/carioca/registry.rb', line 51

def add(service:, definition:, skip_validation: false)
  raise "Service #{service} already exist." if @services.include?(service) && (skip_validation == false)

  if @active_services.include?(:logger) && @@config.debug?
    debug message: i18n.t('service.adding',
                          name: service)
  end
  checker = Carioca::Services::Validator.new(service:, definition:)
  checker.validate! unless skip_validation
  @services[service] = checker.definition
end

#configObject



47
48
49
# File 'lib/carioca/registry.rb', line 47

def config
  @@config
end

#get_service(name:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/carioca/registry.rb', line 24

def get_service(name:)
  raise "Service not found: #{name}" unless @services.include? name

  if @active_services.include? name
    debug message: i18n.t('service.getting', name:) if @active_services.include?(:logger) && !%i[logger
                                                                                                 i18n output].include?(name) && @@config.debug?
  else
    service = @services[name]
    if service.include? :depends
      service[:depends].each do |dep|
        debug message: i18n.t('service.depends', name: dep) if @active_services.include?(:logger) && !%i[logger
                                                                                                         i18n].include?(dep) && @@config.debug?
        get_service(name: dep) unless @active_services.include? dep
      end
    end
    debug message: i18n.t('service.starting', name:) if @active_services.include?(:logger) && !%i[logger
                                                                                                  i18n].include?(name) && @@config.debug?
    require service[:resource] if %i[gem file stdlib].include? service[:type]
    @active_services[name] ||= eval("lambda { #{service[:service]} }").call # lambda { Aservice::new }
  end
  @active_services[name]
end