carioca 2.0.2 → 2.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,16 +1,14 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module Carioca
3
- class Container
4
+ class Container
5
+ extend Carioca::Injector
4
6
 
5
- extend Carioca::Injector
6
-
7
- inject service: :logger
8
- inject service: :configuration
9
-
10
- def initialize(name: 'Carioca')
11
- @name = name
12
- end
7
+ inject service: :logger
8
+ inject service: :configuration
13
9
 
10
+ def initialize(name: 'Carioca')
11
+ @name = name
14
12
  end
15
-
13
+ end
16
14
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'yaml'
2
4
  require 'forwardable'
3
5
  require 'singleton'
@@ -8,11 +10,11 @@ require 'locale'
8
10
  require 'deep_merge'
9
11
  require 'pastel'
10
12
 
13
+ require_relative 'helpers'
11
14
  require_relative 'constants'
12
15
  require_relative 'validator'
13
16
  require_relative 'mixin'
14
17
  require_relative 'container'
15
- require_relative 'helpers'
16
18
  require_relative 'configuration'
17
19
 
18
20
  require_relative 'registry_file'
@@ -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,7 +1,10 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'rake'
3
4
  require 'rubygems'
4
5
  require 'carioca'
6
+ require 'tty-prompt'
7
+ require 'pastel'
5
8
 
6
9
  $VERBOSE = nil
7
10
  if Gem::Specification.respond_to?(:find_by_name)
@@ -9,7 +12,7 @@ if Gem::Specification.respond_to?(:find_by_name)
9
12
  spec = Gem::Specification.find_by_name('carioca')
10
13
  res = spec.lib_dirs_glob.split('/')
11
14
  rescue LoadError
12
- spec = nil
15
+ res = []
13
16
  end
14
17
  else
15
18
  spec = Gem.searcher.find('carioca')
@@ -0,0 +1,46 @@
1
+ namespace :carioca do
2
+ namespace :services do
3
+ namespace :config do
4
+ desc "Initialise Service configuration file ./config/settings.yml file"
5
+ task :init do
6
+ begin
7
+ prompt = TTY::Prompt.new
8
+ pastel = ::Pastel.new
9
+
10
+ if File::exist? "./config/settings.yml" then
11
+ puts pastel.yellow "WARNING : config file already exist, if you continue, you will destroy it !"
12
+ continue = prompt.yes?("continue ? ") do |q|
13
+ q.default false
14
+ end
15
+ print "Carioca : "
16
+ unless continue then
17
+ puts pastel.yellow "canceled"
18
+ exit 5
19
+ else
20
+ File::unlink "./config/settings.yml"
21
+ puts pastel.cyan "Reset File"
22
+ end
23
+ end
24
+
25
+ unless File::exist? "./config" then
26
+ puts pastel.red "Carioca is not initialized for Gem usage, perhaps need to run :"
27
+ puts pastel.red "$ rake carioca:gem:init_path"
28
+ exit 10
29
+ end
30
+ puts "Carioca : initializing default config file (./config/settings.yml): "
31
+ root = prompt.ask("Root config name ? (like your gem/App name)") { |q|
32
+ q.modify :down
33
+ q.required true }.to_sym
34
+ print "Carioca : Generating config file : "
35
+ structure = {root => {:production => {}, :staging => {}, :development => {}, :test => {}, :default => {}}}
36
+ File.open('./config/settings.yml', 'w') { |file| file.write(structure.to_yaml) }
37
+ puts pastel.green 'done'
38
+ rescue TTY::Reader::InputInterrupt
39
+ print "Carioca : "
40
+ puts pastel.yellow 'interrupted'
41
+ exit 5
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,15 @@
1
+ namespace :carioca do
2
+ namespace :gem do
3
+ desc "prepare Gem vitals path for Carioca"
4
+ task :init_path do
5
+ pastel = Pastel.new
6
+ if File::exist? "./config/locales"
7
+ puts pastel.yellow "Carioca path already initialized"
8
+ else
9
+ print 'Carioca : Initialising vitals gem path : '
10
+ FileUtils.mkdir_p "./config/locales"
11
+ puts pastel.green "done"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,15 +1,18 @@
1
- require "tty-prompt"
2
- require "pastel"
3
-
4
1
  namespace :carioca do
5
2
  namespace :registry do
6
3
  desc "Adding service to Carioca Registry file"
7
4
  task :add_service do
5
+ prompt = TTY::Prompt.new
6
+ pastel = ::Pastel.new
8
7
  begin
8
+ unless File::exist? "./config" then
9
+
10
+ puts pastel.yellow "Carioca is not initialized for Gem usage, perhaps need to run :"
11
+ puts pastel.yellow "$ rake carioca:gem:init_path"
12
+ exit unless prompt.yes?("Do you want to continue, with a standalone registry (not recommanded). ? ")
13
+ end
9
14
  puts "Carioca : registering service :"
10
15
  config = Carioca::Configuration::new
11
- prompt = TTY::Prompt.new
12
- pastel = Pastel.new
13
16
  filename = prompt.ask("Registry File path ?", default: config.filename)
14
17
  registry_file = Carioca::RegistryFile::new filename: filename
15
18
  name = prompt.ask("Service name ?") { |q| q.required true }.to_sym
@@ -36,22 +39,26 @@ namespace :carioca do
36
39
  end
37
40
  is_correct = prompt.yes?("Is it correct ? ")
38
41
  rescue TTY::Reader::InputInterrupt
39
- puts 'Carioca : interrupted'
42
+ print "Carioca : "
43
+ puts pastel.yellow 'interrupted'
40
44
  exit 5
41
45
  end
46
+ print "Carioca : Registry saving : "
42
47
  if is_correct then
43
48
  begin
44
49
  registry_file.add service: name, definition: definition
45
50
  registry_file.save!
46
51
  rescue => e
47
- puts "Carioca: Can't save : #{e}"
52
+ print pastel.red "failed"
53
+ puts " error : #{e}"
48
54
  exit 10
49
55
  end
50
56
 
51
- puts "Carioca : Registry saved"
57
+ puts pastel.green "done"
52
58
  else
53
- puts 'Carioca : Nothing saved'
59
+ puts pastel.yellow 'canceled'
54
60
  end
55
61
  end
56
62
  end
57
- end
63
+
64
+ end
@@ -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].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