carioca 1.3 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/main.yml +18 -0
  3. data/.gitignore +11 -0
  4. data/.rspec +3 -0
  5. data/Gemfile +3 -1
  6. data/Gemfile.lock +44 -40
  7. data/README.md +123 -190
  8. data/Rakefile +5 -59
  9. data/bin/console +15 -0
  10. data/bin/setup +8 -0
  11. data/carioca.gemspec +45 -32
  12. data/config/locales/en.yml +19 -0
  13. data/config/locales/fr.yml +19 -0
  14. data/lib/carioca/configuration.rb +60 -0
  15. data/lib/carioca/constants.rb +60 -0
  16. data/lib/carioca/container.rb +16 -0
  17. data/lib/carioca/dependencies.rb +20 -0
  18. data/lib/carioca/helpers.rb +44 -31
  19. data/lib/carioca/mixin.rb +32 -0
  20. data/lib/carioca/{tasks/rake.rb → rake/manage.rb} +4 -6
  21. data/lib/carioca/rake/tasks/registry.tasks +57 -0
  22. data/lib/carioca/registry.rb +94 -0
  23. data/lib/carioca/registry_file.rb +62 -0
  24. data/lib/carioca/services/config.rb +140 -0
  25. data/lib/carioca/services/i18n.rb +20 -0
  26. data/lib/carioca/services/init.rb +2 -0
  27. data/lib/carioca/services/output.rb +175 -0
  28. data/lib/carioca/validator.rb +49 -0
  29. data/lib/carioca.rb +2 -319
  30. data/samples/Rakefile +2 -0
  31. data/samples/config/carioca.registry +22 -0
  32. data/samples/config/locales/en.yml +2 -0
  33. data/samples/config/locales/es.yml +2 -0
  34. data/samples/config/locales/fr.yml +2 -0
  35. data/samples/config/settings.yml +24 -0
  36. data/samples/test.rb +98 -0
  37. metadata +76 -168
  38. data/AUTHORS +0 -8
  39. data/COPYRIGHT +0 -24
  40. data/ChangeLog +0 -7
  41. data/INSTALL +0 -7
  42. data/doc/manual.rdoc +0 -225
  43. data/lib/carioca/exceptions.rb +0 -9
  44. data/lib/carioca/private.rb +0 -170
  45. data/lib/carioca/services/configuration.rb +0 -201
  46. data/lib/carioca/services/debug.rb +0 -73
  47. data/lib/carioca/services/logger.rb +0 -58
  48. data/lib/carioca/services.rb +0 -143
  49. data/lib/carioca/tasks/registry_init.rake +0 -11
  50. data/spec/carioca_spec.rb +0 -474
  51. data/spec/config/services.registry +0 -55
  52. data/spec/init_spec.rb +0 -1
  53. data/spec/samples/dummy.rb +0 -10
  54. data/spec/samples/otherdummy.rb +0 -10
  55. data/spec/samples/requireddummy.rb +0 -10
  56. data/spec/spec_helper.rb +0 -18
  57. data/test.rb +0 -12
  58. data/ultragreen_roodi_coding_convention.yml +0 -25
data/carioca.gemspec CHANGED
@@ -1,33 +1,46 @@
1
- Gem::Specification.new do |s|
2
- s.name = %q{carioca}
3
- s.author = "Romain GEORGES"
4
- s.version = "1.3"
5
- s.license = "BSD-2"
6
- s.date = %q{2013-02-18}
7
- s.summary = %q{Carioca : Configuration Agent and Registry with Inversion Of Control for your Applications}
8
- s.email = %q{romain@ultragreen.net}
9
- s.homepage = %q{http://www.ultragreen.net}
10
- s.description = %q{Carioca : provide a full IoC light Container for designing your applications}
11
- s.has_rdoc = true
12
- s.files = Dir['*/*/*/*'] + Dir['*/*/*'] + Dir['*/*'] + Dir['*']
13
-
14
- s.add_development_dependency "bundler", "~> 1.5"
15
- s.add_development_dependency "rake", "~> 10.5"
16
- s.add_development_dependency 'rspec', '~> 2.14', '>= 2.14.1'
17
- s.add_development_dependency 'yard', '~> 0.8', '>= 0.8.7.2'
18
- s.add_development_dependency 'rdoc', '~> 4.0', '>= 4.0.1'
19
- s.add_development_dependency 'roodi', '~> 3.1', '>= 3.1.1'
20
- s.add_development_dependency 'code_statistics', '~> 0.2', '>= 0.2.13'
21
- s.add_development_dependency 'yard-rspec', '~> 0.1'
22
-
23
-
24
-
25
- s.add_dependency 'dorsal', "~> 1.0"
26
- s.add_dependency 'methodic', "~> 1.2", '>= 1.2'
27
- s.add_dependency 'xml-simple', "~> 1.1", '>= 1.1.5'
28
-
29
- s.required_ruby_version = '>= 1.8.1'
30
- s.rdoc_options << '--title' << 'Carioca : Gem documentation' << '--main' << 'doc/manual.rdoc' << '--line-numbers'
31
- # << '--diagram'
32
- s.rubyforge_project = "nowarning"
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/carioca/constants"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "carioca"
7
+ spec.version = Carioca::Constants::VERSION
8
+ spec.authors = ["Romain GEORGES"]
9
+ spec.email = ["romain@ultragreen.net"]
10
+
11
+
12
+
13
+
14
+ spec.license = "BSD-3-Clause"
15
+
16
+ spec.summary = %q{Carioca : Container And Registry with Inversion Of Control for your Applications}
17
+ spec.homepage = %q{https://github.com/Ultragreen/carioca}
18
+ spec.description = %q{Carioca 2: is a complete rewrite who provide a full IoC/DI light Container and a services registry, build with logs, config and Internationalization facilities for designing your applications}
19
+
20
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
21
+
22
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
23
+
24
+ spec.metadata["homepage_uri"] = spec.homepage
25
+ spec.metadata["source_code_uri"] = spec.homepage
26
+ spec.metadata["changelog_uri"] = spec.homepage
27
+
28
+
29
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ # Uncomment to register a new dependency of your gem
37
+ spec.add_dependency "tty-prompt", "~>0.23.1"
38
+ spec.add_dependency "pastel", "~>0.8.0"
39
+ spec.add_dependency 'i18n', "~> 1.10"
40
+ spec.add_dependency 'locale', "~> 2.1"
41
+ spec.add_dependency "deep_merge", "~> 1.2"
42
+ spec.add_development_dependency "rake", "~> 13.0"
43
+ spec.add_development_dependency "rspec", "~> 3.0"
44
+
45
+
33
46
  end
@@ -0,0 +1,19 @@
1
+ en:
2
+ service:
3
+ adding: "Adding service %{name}"
4
+ starting: "Starting service %{name}"
5
+ getting: "Getting service %{name}"
6
+ depends: "Dependencie service %{name}"
7
+ notify:
8
+ locale: "Preloaded service :i18n on locale : %{loc}"
9
+ logger: "Preloaded service :logger ready on %{target}"
10
+ useless_entry: "Useless entry in registry (builtin service) %{altered} in %{filename}"
11
+ init:
12
+ carioca: "Initializing Carioca registry"
13
+ builtins: "Preparing builtins services"
14
+ registry:
15
+ processing: "Initializing registry from file : %{filename}"
16
+ success: "Registry initialized successfully"
17
+ config:
18
+ load:
19
+ error: "Config file ignored, error : %{message}"
@@ -0,0 +1,19 @@
1
+ fr:
2
+ service:
3
+ adding: "Service %{name} Ajouté"
4
+ starting: "Démarrage du service %{name}"
5
+ getting: "Appel du service %{name}"
6
+ depends: "Dépendence du service %{name}"
7
+ notify:
8
+ locale: "Service pré-chargé :i18n sur la locale : %{loc}"
9
+ logger: "Service pré-chargé :logger sur la cible : %{target}"
10
+ useless_entry: "Entrée inutile dans le registre (service pré-defini) %{altered} dans %{filename}"
11
+ init:
12
+ carioca: "Initialisation du registre Carioca"
13
+ builtins: "Préparation des services pré-définis"
14
+ registry:
15
+ processing: "Initialisation du registre depuis le fichier : %{filename}"
16
+ success: "Registre initialisé avec succès"
17
+ config:
18
+ load:
19
+ error: "Fichier de configuration ignoré, erreur : %{message}"
@@ -0,0 +1,60 @@
1
+ module Carioca
2
+ class Configuration
3
+ include Carioca::Constants
4
+ include Carioca::Helpers
5
+ attr_accessor :filename, :name, :builtins, :log_target, :default_locale, :locales_load_path
6
+ attr_accessor :config_file, :config_root, :environment, :supported_environment, :output_mode, :log_level
7
+ attr_writer :debug, :init_from_file, :output_colors, :output_emoji
8
+ attr_reader :log_file, :locales_availables
9
+ def initialize
10
+ @init_from_file = true
11
+ @filename = DEFAULT_REGISTRY_FILE.dup
12
+ @debug = false
13
+ @name = 'Carioca'
14
+ @builtins = BUILTINS
15
+ @log_file = ''
16
+ @log_level = DEFAULT_LOG_LEVEL.dup
17
+ @log_level = :info if @debug == false and @log_level == :debug
18
+ @config_file = DEFAULT_CONFIG_FILE.dup
19
+ @environment = DEFAULT_ENVIRONMENT.dup
20
+ @config_root = DEFAULT_CONFIG_ROOT.dup
21
+ @log_target = '::Logger::new(STDOUT)'
22
+ @supported_environment = DEFAULT_ENVIRONMENTS_LIST.dup
23
+ @default_locale = DEFAULT_LOCALE
24
+ @locales_availables = []
25
+ @output_mode = DEFAULT_OUTPUT_MODE.dup
26
+ @output_colors = DEFAULT_COLORS_STATUS.dup
27
+ @output_emoji = DEFAULT_EMOJI_STATUS.dup
28
+ path = search_file_in_gem('carioca',"config/locales")
29
+ @locales_load_path = Dir[File.expand_path(path) + "/*.yml"]
30
+ Dir[path + '/*.yml'].sort.each do |file|
31
+ @locales_availables.push File::basename(file,'.yml').to_sym
32
+ end
33
+ end
34
+
35
+ def debug?
36
+ return @debug
37
+ end
38
+
39
+ def output_colors?
40
+ return @output_colors
41
+ end
42
+
43
+ def output_emoji?
44
+ return @output_emoji
45
+ end
46
+
47
+ def init_from_file?
48
+ return @init_from_file
49
+ end
50
+
51
+ def log_file?
52
+ return !@log_file.empty?
53
+ end
54
+
55
+ def log_file=(name)
56
+ @log_file = name
57
+ @log_target = "::Logger::new('#{name}')"
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,60 @@
1
+ module Carioca
2
+ module Constants
3
+
4
+ VERSION = '2.0.2'
5
+ DEFAULT_REGISTRY_FILE = './config/carioca.registry'
6
+ DEFAULT_CONFIG_FILE = './config/settings.yml'
7
+ DEFAULT_ENVIRONMENT = :development
8
+ DEFAULT_CONFIG_ROOT = :carioca
9
+ DEFAULT_LOCALE = :en
10
+
11
+
12
+ DEFAULT_OUTPUT_MODE = :mono
13
+ DEFAULT_EMOJI_STATUS = true
14
+ DEFAULT_COLORS_STATUS = true
15
+ DEFAULT_LOG_LEVEL = :info
16
+
17
+ # service definitions specs
18
+ SERVICES_MANDATORY_SPECS = {type: Symbol, service: String}
19
+ SERVICES_FULL_LIST_SPECS = SERVICES_MANDATORY_SPECS.merge({depends: Array, description: String, resource: String })
20
+ SERVICES_SPECS_DETAIL = {type: [:gem, :stdlib, :file, :internal]}
21
+
22
+ DEFAULT_ENVIRONMENTS_LIST = [:production, :staging, :test, :development]
23
+
24
+ BUILTINS = {
25
+ configuration: {
26
+ type: :internal,
27
+ depends: [:logger],
28
+ description: "The configuration service of Carioca",
29
+ service: "Carioca::Services::Config::Factory::new(
30
+ config_filename: Carioca::Registry.config.config_file,
31
+ stage: Carioca::Registry.config.environment,
32
+ root: Carioca::Registry.config.config_root)" },
33
+ logger: {
34
+ type: :stdlib,
35
+ resource: "logger",
36
+ description: "The Logger service of Carioca",
37
+ depends: [:i18n]
38
+ },
39
+ i18n:{
40
+ type: :internal,
41
+ description: "The Internalisation service of Carocia",
42
+ service: "Carioca::Services::I18n.get(
43
+ default_locale: Carioca::Registry.config.default_locale,
44
+ load_path: Carioca::Registry.config.locales_load_path,
45
+ locales_availables: Carioca::Registry.config.locales_availables)"
46
+ },
47
+ output:{
48
+ type: :internal,
49
+ description: "The Output serice of Carioca",
50
+ service: "Carioca::Services::Output::Provider::new(
51
+ mode: Carioca::Registry.config.output_mode,
52
+ emoji: Carioca::Registry.config.output_emoji?,
53
+ colors: Carioca::Registry.config.output_colors?,
54
+ level: Carioca::Registry.config.log_level
55
+ )"
56
+ }
57
+ }
58
+
59
+ end
60
+ end
@@ -0,0 +1,16 @@
1
+
2
+ module Carioca
3
+ class Container
4
+
5
+ extend Carioca::Injector
6
+
7
+ inject service: :logger
8
+ inject service: :configuration
9
+
10
+ def initialize(name: 'Carioca')
11
+ @name = name
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,20 @@
1
+ require 'yaml'
2
+ require 'forwardable'
3
+ require 'singleton'
4
+
5
+ require 'rubygems'
6
+ require 'i18n'
7
+ require 'locale'
8
+ require 'deep_merge'
9
+ require 'pastel'
10
+
11
+ require_relative 'constants'
12
+ require_relative 'validator'
13
+ require_relative 'mixin'
14
+ require_relative 'container'
15
+ require_relative 'helpers'
16
+ require_relative 'configuration'
17
+
18
+ require_relative 'registry_file'
19
+ require_relative 'registry'
20
+ require_relative 'services/init'
@@ -1,35 +1,48 @@
1
- #!/usr/bin/env ruby
2
- #---
3
- # Author : Romain GEORGES
4
- # type : gem component library
5
- # obj : Carioca Helpers definition Module
6
- #---
1
+ module Carioca
2
+ module Helpers
7
3
 
8
- class Class
9
- # Usage:
10
- # prelaod_service :name => 'service', :params => { :arg => 'value'}
11
- def preload_service(_options = {})
12
- Carioca::Services::Registry.init.start_service _options
13
- end
4
+ def log
5
+ return self.get_service name: :logger
6
+ end
14
7
 
15
- def use_configuration(_options = {})
16
- options = Methodic.get_options(_options)
17
- options.specify_classes_of :with_file => String
18
- options.specify_default_value_of :with_file => 'services/registry.yml'
19
- options.merge
20
- options.validate!
21
- Carioca::Services::Registry.init.start_service :name => 'configuration', :params => { :config_file => options[:with_file]}
22
- end
23
- end
8
+ def i18n
9
+ return self.get_service name: :i18n
10
+ end
24
11
 
12
+ def debug(message: )
13
+ log.debug(self.config.name) { "#{message}" }
14
+ end
25
15
 
26
- Module.class_eval do
27
- def init_registry _options={}
28
- options = Methodic.get_options(_options)
29
- options.specify_classes_of :with_file => String
30
- options.specify_default_value_of :with_file => 'services/registry.yml'
31
- options.merge
32
- options.validate!
33
- Carioca::Services::Registry.init :file => options[:with_file]
34
- end
35
- end
16
+ # facility to find a file in gem path
17
+ # @param [String] _gem a Gem name
18
+ # @param [String] _file a file relative path in the gem
19
+ # @return [String] the path of the file, if found.
20
+ # @return [False] if not found
21
+ def search_file_in_gem(_gem,_file)
22
+ if Gem::Specification.respond_to?(:find_by_name)
23
+ begin
24
+ spec = Gem::Specification.find_by_name(_gem)
25
+ rescue LoadError
26
+ spec = nil
27
+ end
28
+ else
29
+ spec = Gem.searcher.find(_gem)
30
+ end
31
+ if spec then
32
+ if Gem::Specification.respond_to?(:find_by_name)
33
+ res = spec.lib_dirs_glob.split('/')
34
+ else
35
+ res = Gem.searcher.lib_dirs_for(spec).split('/')
36
+ end
37
+ res.pop
38
+ services_path = res.join('/').concat("/#{_file}")
39
+ return services_path if File::exist?(services_path)
40
+ return false
41
+ else
42
+ return false
43
+ end
44
+ end
45
+
46
+
47
+ end
48
+ end
@@ -0,0 +1,32 @@
1
+ module Carioca
2
+ module Injector
3
+ def inject(service: )
4
+ self.create_methods(service){return Carioca::Registry.get.get_service name: service }
5
+ end
6
+
7
+ def register(service: , definition:)
8
+ Carioca::Registry.get.add service: service, definition: definition
9
+ end
10
+
11
+ def services
12
+ Carioca::Registry.get.services
13
+ end
14
+
15
+ def active_services
16
+ Carioca::Registry.get.active_services
17
+ end
18
+
19
+ def create_methods(name, &block)
20
+ self.define_method name, &block
21
+ self.class.send(:define_method, name, &block)
22
+ end
23
+
24
+ def self.extended(base)
25
+ base.include self
26
+ end
27
+
28
+
29
+
30
+ end
31
+
32
+ end
@@ -1,5 +1,6 @@
1
+ # coding: utf-8
1
2
  require 'rake'
2
- require 'rubygems'
3
+ require 'rubygems'
3
4
  require 'carioca'
4
5
 
5
6
  $VERBOSE = nil
@@ -16,8 +17,5 @@ else
16
17
  end
17
18
 
18
19
  res.pop
19
- tasks_path = res.join('/').concat('/lib/carioca/tasks/')
20
-
21
-
22
- Dir["#{tasks_path}/*.rake"].each { |ext| load ext }
23
-
20
+ tasks_path = res.join('/').concat('/lib/carioca/rake/tasks/')
21
+ Dir["#{tasks_path}/*.task*"].each { |ext| load ext }
@@ -0,0 +1,57 @@
1
+ require "tty-prompt"
2
+ require "pastel"
3
+
4
+ namespace :carioca do
5
+ namespace :registry do
6
+ desc "Adding service to Carioca Registry file"
7
+ task :add_service do
8
+ begin
9
+ puts "Carioca : registering service :"
10
+ config = Carioca::Configuration::new
11
+ prompt = TTY::Prompt.new
12
+ pastel = Pastel.new
13
+ filename = prompt.ask("Registry File path ?", default: config.filename)
14
+ registry_file = Carioca::RegistryFile::new filename: filename
15
+ name = prompt.ask("Service name ?") { |q| q.required true }.to_sym
16
+ if config.builtins.include? name or registry_file.validated.include? name then
17
+ puts 'Carioca : service already defined or Builtins'
18
+ exit 1
19
+ end
20
+ definition = {
21
+ :type => prompt.select("Choose the service type ?", [:gem, :stdlib, :file, :internal]),
22
+ :description => prompt.ask("Description ?", default: "The #{name} service"),
23
+ :service => prompt.ask("Service [#{name}] inline Proc Ruby code ?", default: name.to_s.capitalize)
24
+ }
25
+ map = {:gem => "Rubygem name", :stdlib => "StdLib name", :file => "absolut path of the Ruby file"}
26
+ definition[:resource] = prompt.ask("Give the #{map[definition[:type]]} ? ", default: name.to_s ) if map.keys.include? definition[:type]
27
+ have_depends = prompt.yes?("Did this service have dependencies ? ")
28
+ if have_depends then
29
+ potentials = config.builtins.merge(registry_file.validated).keys
30
+ definition[:depends] = prompt.multi_select("Choose your depends ?", potentials, min: 1)
31
+ end
32
+ puts "\n => Service : #{name}"
33
+ puts "Definition "
34
+ definition.each do |key,value|
35
+ puts " * #{key}: #{value}"
36
+ end
37
+ is_correct = prompt.yes?("Is it correct ? ")
38
+ rescue TTY::Reader::InputInterrupt
39
+ puts 'Carioca : interrupted'
40
+ exit 5
41
+ end
42
+ if is_correct then
43
+ begin
44
+ registry_file.add service: name, definition: definition
45
+ registry_file.save!
46
+ rescue => e
47
+ puts "Carioca: Can't save : #{e}"
48
+ exit 10
49
+ end
50
+
51
+ puts "Carioca : Registry saved"
52
+ else
53
+ puts 'Carioca : Nothing saved'
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,94 @@
1
+ module Carioca
2
+ class Registry
3
+ include Carioca::Helpers
4
+ include Singleton
5
+ @@config = Configuration::new
6
+
7
+ def Registry.config
8
+ return @@config
9
+ end
10
+
11
+ def Registry.configure(&block)
12
+ yield(@@config)
13
+ end
14
+
15
+ class << self
16
+ alias_method :get, :instance
17
+ alias_method :init, :instance
18
+
19
+ end
20
+
21
+ attr_accessor :services
22
+ attr_accessor :active_services
23
+
24
+ def get_service(name: )
25
+
26
+ raise "Service not found: #{name}" unless @services.include? name
27
+ if @active_services.include? name then
28
+ debug message: i18n.t('service.getting', name: name) if @active_services.include? :logger and ![:logger, :i18n].include? name and @@config.debug?
29
+ else
30
+ service = @services[name]
31
+ service[:depends].each do|dep|
32
+ debug message: i18n.t('service.depends', name: dep) if @active_services.include? :logger and ![:logger, :i18n].include? dep and @@config.debug?
33
+ get_service(name: dep) unless @active_services.include? dep
34
+ end if service.include? :depends
35
+ debug message: i18n.t('service.starting', name: name) if @active_services.include? :logger and ![:logger, :i18n].include? name and @@config.debug?
36
+ require service[:resource] if [:gem, :file, :stdlib].include? service[:type]
37
+ @active_services[name] ||= eval("lambda { #{service[:service]} }").call
38
+ end
39
+ return @active_services[name]
40
+ end
41
+
42
+ def config
43
+ return @@config
44
+ end
45
+
46
+ def add(service: , definition:, skip_validation: false )
47
+ mess =
48
+ raise "Service #{service} already exist." if @services.include? service and skip_validation == false
49
+ debug message: i18n.t('service.adding', name: service) if @active_services.include? :logger and @@config.debug?
50
+ checker = Carioca::Services::Validator::new service: service , definition: definition
51
+ checker.validate! unless skip_validation
52
+ @services[service] = checker.definition
53
+ end
54
+
55
+ private
56
+ def prepare_logger
57
+ conf_i18n = @@config.builtins[:i18n]
58
+ add service: :i18n, definition: @@config.builtins[:i18n], skip_validation: true
59
+ conf_logger = @@config.builtins[:logger]
60
+ conf_logger[:service] = @@config.log_target
61
+ add service: :logger, definition: @@config.builtins[:logger], skip_validation: true
62
+ log = get_service name: :logger
63
+ log.level = @@config.log_level
64
+ end
65
+
66
+ def initialize
67
+ @services = Hash::new
68
+ @active_services = Hash::new
69
+ prepare_logger
70
+ locale = @@config.default_locale
71
+ target = (@@config.log_file?)? @@config.log_file : "STDOUT"
72
+ debug message: i18n.t('notify.locale', loc: locale) if @@config.debug?
73
+ debug message: i18n.t('notify.logger', target: target) if @@config.debug?
74
+ debug message: i18n.t('init.carioca') if @@config.debug?
75
+ debug message: i18n.t('init.builtins') if @@config.debug?
76
+ @@config.builtins.each do |service, spec|
77
+ add service: service, definition: spec, skip_validation: true unless service == :logger
78
+ end
79
+ open_registry_file if File::exist? @@config.filename and @@config.init_from_file?
80
+ end
81
+
82
+ def open_registry_file
83
+ debug message: i18n.t('init.registry.processing', filename: @@config.filename) if @@config.debug?
84
+ registry_file = Carioca::RegistryFile::new filename: @@config.filename
85
+ debug message: i18n.t('notify.useless_entry', altered: registry_file.altered.to_s, file_name: @@config.filename ) if registry_file.altered? and @@config.debug?
86
+ registry_file.validated.each do |service,spec|
87
+ add service: service, definition: spec
88
+ end
89
+ debug message: i18n.t('init.registry.success') if @@config.debug?
90
+ end
91
+
92
+
93
+ end
94
+ end
@@ -0,0 +1,62 @@
1
+ module Carioca
2
+
3
+
4
+
5
+
6
+ class RegistryFile
7
+
8
+ attr_accessor :validated, :altered
9
+ include Carioca::Constants
10
+
11
+ def initialize(filename:)
12
+ @filename = filename
13
+ @candidates = Hash::new
14
+ @validated = Hash::new
15
+ @altered = []
16
+ open
17
+ end
18
+
19
+ def altered?
20
+ return !@altered.empty?
21
+ end
22
+
23
+ def create!(force: false)
24
+ write_ok = true
25
+ write_ok = force if File::exist? @filename
26
+ File.open(@filename, 'w') { |file| file.write(@validated.to_yaml) } if write_ok
27
+ end
28
+
29
+ def save!
30
+ create! force: true
31
+ end
32
+
33
+ def add(service:, definition: )
34
+ checker = Carioca::Services::Validator::new service: service , definition: definition
35
+ checker.validate!
36
+ @validated[service] = checker.definition
37
+ end
38
+
39
+ def open
40
+ if File::exist?(@filename) then
41
+ @candidates = YAML.load_file(@filename)
42
+ else
43
+ create!
44
+ end
45
+ prepare!
46
+ end
47
+
48
+ private
49
+ def prepare!
50
+ save = @candidates.dup
51
+ @candidates.delete_if {|key, value| BUILTINS.keys.include? key }
52
+ @altered = save.keys - @candidates.keys
53
+ @candidates.each do |service, definition|
54
+ checker = Carioca::Services::Validator::new service: service , definition: definition
55
+ checker.validate!
56
+ @validated[service] = checker.definition
57
+ end
58
+ end
59
+
60
+
61
+ end
62
+ end