carioca 2.0.4 → 2.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,48 +1,46 @@
1
- module Carioca
2
- module Helpers
1
+ # frozen_string_literal: true
3
2
 
4
- def log
5
- return self.get_service name: :logger
6
- end
3
+ module Carioca
4
+ module Helpers
5
+ def log
6
+ get_service name: :logger
7
+ end
7
8
 
8
- def i18n
9
- return self.get_service name: :i18n
10
- end
9
+ def i18n
10
+ get_service name: :i18n
11
+ end
11
12
 
12
- def debug(message: )
13
- log.debug(self.config.name) { "#{message}" }
14
- end
13
+ def debug(message:)
14
+ log.debug(config.name) { message.to_s }
15
+ end
15
16
 
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
17
+ # facility to find a file in gem path
18
+ # @param [String] gem a Gem name
19
+ # @param [String] file a file relative path in the gem
20
+ # @return [String] the path of the file, if found.
21
+ # @return [False] if not found
22
+ def search_file_in_gem(gem, file)
23
+ if Gem::Specification.respond_to?(:find_by_name)
24
+ begin
25
+ spec = Gem::Specification.find_by_name(gem)
26
+ rescue LoadError
27
+ spec = nil
44
28
  end
29
+ else
30
+ spec = Gem.searcher.find(gem)
31
+ end
32
+ if spec
33
+ res = if Gem::Specification.respond_to?(:find_by_name)
34
+ spec.lib_dirs_glob.split('/')
35
+ else
36
+ Gem.searcher.lib_dirs_for(spec).split('/')
37
+ end
38
+ res.pop
39
+ services_path = res.join('/').concat("/#{file}")
40
+ return services_path if File.exist?(services_path)
45
41
 
46
-
42
+ end
43
+ false
47
44
  end
48
- end
45
+ end
46
+ end
data/lib/carioca/mixin.rb CHANGED
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Carioca
2
4
  module Injector
3
- def inject(service: )
4
- self.create_methods(service){return Carioca::Registry.get.get_service name: service }
5
+ def inject(service:)
6
+ create_methods(service) { return Carioca::Registry.get.get_service name: service }
5
7
  end
6
8
 
7
- def register(service: , definition:)
8
- Carioca::Registry.get.add service: service, definition: definition
9
+ def register(service:, definition:)
10
+ Carioca::Registry.get.add service: service, definition: definition
9
11
  end
10
12
 
11
13
  def services
@@ -14,19 +16,15 @@ module Carioca
14
16
 
15
17
  def active_services
16
18
  Carioca::Registry.get.active_services
17
- end
19
+ end
18
20
 
19
21
  def create_methods(name, &block)
20
- self.define_method name, &block
22
+ define_method name, &block
21
23
  self.class.send(:define_method, name, &block)
22
24
  end
23
25
 
24
26
  def self.extended(base)
25
27
  base.include self
26
- end
27
-
28
-
29
-
28
+ end
30
29
  end
31
-
32
- end
30
+ end
@@ -1,10 +1,10 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'rake'
3
4
  require 'rubygems'
4
5
  require 'carioca'
5
- require "tty-prompt"
6
- require "pastel"
7
-
6
+ require 'tty-prompt'
7
+ require 'pastel'
8
8
 
9
9
  $VERBOSE = nil
10
10
  if Gem::Specification.respond_to?(:find_by_name)
@@ -12,7 +12,7 @@ if Gem::Specification.respond_to?(:find_by_name)
12
12
  spec = Gem::Specification.find_by_name('carioca')
13
13
  res = spec.lib_dirs_glob.split('/')
14
14
  rescue LoadError
15
- spec = nil
15
+ res = []
16
16
  end
17
17
  else
18
18
  spec = Gem.searcher.find('carioca')
@@ -1,94 +1,103 @@
1
+ # frozen_string_literal: true
2
+
1
3
  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
4
+ class Registry
5
+ include Carioca::Helpers
6
+ include Singleton
7
+ @@config = Configuration.new
18
8
 
19
- end
20
-
21
- attr_accessor :services
22
- attr_accessor :active_services
23
-
24
- def get_service(name: )
9
+ def self.config
10
+ @@config
11
+ end
25
12
 
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, :output].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
13
+ def self.configure
14
+ yield(@@config)
15
+ end
45
16
 
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
17
+ class << self
18
+ alias get instance
19
+ alias init instance
20
+ end
21
+
22
+ attr_accessor :services, :active_services
23
+
24
+ def get_service(name:)
25
+ raise "Service not found: #{name}" unless @services.include? name
65
26
 
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
27
+ if @active_services.include? name
28
+ debug message: i18n.t('service.getting', name: name) if @active_services.include?(:logger) && !%i[logger
29
+ i18n output].include?(name) && @@config.debug?
30
+ else
31
+ service = @services[name]
32
+ if service.include? :depends
33
+ service[:depends].each do |dep|
34
+ debug message: i18n.t('service.depends', name: dep) if @active_services.include?(:logger) && !%i[logger
35
+ i18n].include?(dep) && @@config.debug?
36
+ get_service(name: dep) unless @active_services.include? dep
78
37
  end
79
- open_registry_file if File::exist? @@config.filename and @@config.init_from_file?
80
38
  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
-
39
+ debug message: i18n.t('service.starting', name: name) if @active_services.include?(:logger) && !%i[logger
40
+ i18n].include?(name) && @@config.debug?
41
+ require service[:resource] if %i[gem file stdlib].include? service[:type]
42
+ @active_services[name] ||= eval("lambda { #{service[:service]} }").call # lambda { Aservice::new }
43
+ end
44
+ @active_services[name]
45
+ end
46
+
47
+ def config
48
+ @@config
49
+ end
50
+
51
+ def add(service:, definition:, skip_validation: false)
52
+ raise "Service #{service} already exist." if @services.include?(service) && (skip_validation == false)
53
+
54
+ if @active_services.include?(:logger) && @@config.debug?
55
+ debug message: i18n.t('service.adding',
56
+ name: service)
57
+ end
58
+ checker = Carioca::Services::Validator.new service: service, definition: definition
59
+ checker.validate! unless skip_validation
60
+ @services[service] = checker.definition
61
+ end
62
+
63
+ private
64
+
65
+ def prepare_logger
66
+ add service: :i18n, definition: @@config.builtins[:i18n], skip_validation: true
67
+ conf_logger = @@config.builtins[:logger]
68
+ conf_logger[:service] = @@config.log_target
69
+ add service: :logger, definition: @@config.builtins[:logger], skip_validation: true
70
+ log = get_service name: :logger
71
+ log.level = @@config.log_level
72
+ end
73
+
74
+ def initialize
75
+ @services = {}
76
+ @active_services = {}
77
+ prepare_logger
78
+ locale = @@config.default_locale
79
+ target = @@config.log_file? ? @@config.log_file : 'STDOUT'
80
+ debug message: i18n.t('notify.locale', loc: locale) if @@config.debug?
81
+ debug message: i18n.t('notify.logger', target: target) if @@config.debug?
82
+ debug message: i18n.t('init.carioca') if @@config.debug?
83
+ debug message: i18n.t('init.builtins') if @@config.debug?
84
+ @@config.builtins.each do |service, spec|
85
+ add service: service, definition: spec, skip_validation: true unless service == :logger
86
+ end
87
+ open_registry_file if File.exist?(@@config.filename) && @@config.init_from_file?
88
+ end
89
+
90
+ def open_registry_file
91
+ debug message: i18n.t('init.registry.processing', filename: @@config.filename) if @@config.debug?
92
+ registry_file = Carioca::RegistryFile.new filename: @@config.filename
93
+ if registry_file.altered? && @@config.debug?
94
+ debug message: i18n.t('notify.useless_entry', altered: registry_file.altered.to_s,
95
+ file_name: @@config.filename)
96
+ end
97
+ registry_file.validated.each do |service, spec|
98
+ add service: service, definition: spec
99
+ end
100
+ debug message: i18n.t('init.registry.success') if @@config.debug?
93
101
  end
94
- end
102
+ end
103
+ end
@@ -1,62 +1,59 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Carioca
4
+ class RegistryFile
5
+ attr_accessor :validated, :altered
6
+
7
+ include Carioca::Constants
8
+
9
+ def initialize(filename:)
10
+ @filename = filename
11
+ @candidates = {}
12
+ @validated = {}
13
+ @altered = []
14
+ open
15
+ end
16
+
17
+ def altered?
18
+ !@altered.empty?
19
+ end
2
20
 
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
21
+ def create!(force: false)
22
+ write_ok = true
23
+ write_ok = force if File.exist? @filename
24
+ File.write(@filename, @validated.to_yaml) if write_ok
25
+ end
26
+
27
+ def save!
28
+ create! force: true
29
+ end
59
30
 
31
+ def add(service:, definition:)
32
+ checker = Carioca::Services::Validator.new service: service, definition: definition
33
+ checker.validate!
34
+ @validated[service] = checker.definition
35
+ end
36
+
37
+ def open
38
+ if File.exist?(@filename)
39
+ @candidates = YAML.load_file(@filename)
40
+ else
41
+ create!
42
+ end
43
+ prepare!
44
+ end
60
45
 
46
+ private
47
+
48
+ def prepare!
49
+ save = @candidates.dup
50
+ @candidates.delete_if { |key, _value| BUILTINS.keys.include? key }
51
+ @altered = save.keys - @candidates.keys
52
+ @candidates.each do |service, definition|
53
+ checker = Carioca::Services::Validator.new service: service, definition: definition
54
+ checker.validate!
55
+ @validated[service] = checker.definition
56
+ end
61
57
  end
62
- end
58
+ end
59
+ end