diff --git a/.rubocop.yml b/.rubocop.yml index 98d389f..7f6c337 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -48,7 +48,5 @@ Style/Documentation: Layout/LineLength: Enabled: false - - - - +Metrics/ParameterLists: + Max: 8 \ No newline at end of file diff --git a/carioca.gemspec b/carioca.gemspec index 0e11d7f..a86038c 100644 --- a/carioca.gemspec +++ b/carioca.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |spec| spec.homepage = 'https://github.com/Ultragreen/carioca' spec.description = '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' - spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0') + spec.required_ruby_version = Gem::Requirement.new('>= 3.2.3') spec.metadata['allowed_push_host'] = 'https://rubygems.org' diff --git a/lib/carioca/configuration.rb b/lib/carioca/configuration.rb index 2ee801a..3ee3199 100644 --- a/lib/carioca/configuration.rb +++ b/lib/carioca/configuration.rb @@ -34,7 +34,7 @@ def initialize @secure_store_file = DEFAULT_SECURE_STORE_FILE.dup path = search_file_in_gem('carioca', 'config/locales') @locales_load_path = Dir["#{File.expand_path(path)}/*.yml"] - Dir["#{path}/*.yml"].sort.each do |file| + Dir["#{path}/*.yml"].each do |file| @locales_availables.push File.basename(file, '.yml').to_sym end @debugger_tracer = DEFAULT_DEBUGGER_TRACER.dup diff --git a/lib/carioca/constants.rb b/lib/carioca/constants.rb index 7008c5b..8cbd40e 100644 --- a/lib/carioca/constants.rb +++ b/lib/carioca/constants.rb @@ -16,8 +16,8 @@ module Constants DEFAULT_USER_CONFIG_PATH = '~/.carioca' - DEFAULT_MASTER_KEY_FILE = "#{DEFAULT_USER_CONFIG_PATH}/master.key" - DEFAULT_SECURE_STORE_FILE = "#{DEFAULT_USER_CONFIG_PATH}/secure.Store" + DEFAULT_MASTER_KEY_FILE = "#{DEFAULT_USER_CONFIG_PATH}/master.key".freeze + DEFAULT_SECURE_STORE_FILE = "#{DEFAULT_USER_CONFIG_PATH}/secure.Store".freeze DEFAULT_DEBUGGER_TRACER = :output diff --git a/lib/carioca/mixin.rb b/lib/carioca/mixin.rb index 65fdec7..5f815fc 100644 --- a/lib/carioca/mixin.rb +++ b/lib/carioca/mixin.rb @@ -7,7 +7,7 @@ def inject(service:) end def add(service:, definition:) - Carioca::Registry.get.add service: service, definition: definition + Carioca::Registry.get.add service:, definition: end def services @@ -18,9 +18,9 @@ def active_services Carioca::Registry.get.active_services end - def create_methods(name, &block) - define_method name, &block - self.class.send(:define_method, name, &block) + def create_methods(name, &) + define_method(name, &) + self.class.send(:define_method, name, &) end def self.extended(base) diff --git a/lib/carioca/registry.rb b/lib/carioca/registry.rb index e244bb0..305733f 100644 --- a/lib/carioca/registry.rb +++ b/lib/carioca/registry.rb @@ -25,8 +25,8 @@ 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: name) if @active_services.include?(:logger) && !%i[logger - i18n output].include?(name) && @@config.debug? + 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 @@ -36,9 +36,9 @@ def get_service(name:) get_service(name: dep) unless @active_services.include? dep end end - debug message: i18n.t('service.starting', name: name) if @active_services.include?(:logger) && !%i[logger - i18n].include?(name) && @@config.debug? - require service[:resource] if %i[gem file stdlib].include? service[:type] + 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] @@ -55,7 +55,7 @@ def add(service:, definition:, skip_validation: false) debug message: i18n.t('service.adding', name: service) end - checker = Carioca::Services::Validator.new service: service, definition: definition + checker = Carioca::Services::Validator.new(service:, definition:) checker.validate! unless skip_validation @services[service] = checker.definition end @@ -78,7 +78,7 @@ def initialize locale = @@config.default_locale target = @@config.log_file? ? @@config.log_file : 'STDOUT' debug message: i18n.t('notify.locale', loc: locale) if @@config.debug? - debug message: i18n.t('notify.logger', target: target) if @@config.debug? + debug message: i18n.t('notify.logger', target:) if @@config.debug? debug message: i18n.t('init.carioca') if @@config.debug? debug message: i18n.t('init.builtins') if @@config.debug? @@config.builtins.each do |service, spec| @@ -95,7 +95,7 @@ def open_registry_file file_name: @@config.filename) end registry_file.validated.each do |service, spec| - add service: service, definition: spec + add service:, definition: spec end debug message: i18n.t('init.registry.success') if @@config.debug? end diff --git a/lib/carioca/registry_file.rb b/lib/carioca/registry_file.rb index da79691..8a9ddd3 100644 --- a/lib/carioca/registry_file.rb +++ b/lib/carioca/registry_file.rb @@ -29,7 +29,7 @@ def save! end def add(service:, definition:) - checker = Carioca::Services::Validator.new service: service, definition: definition + checker = Carioca::Services::Validator.new(service:, definition:) checker.validate! @validated[service] = checker.definition end @@ -50,7 +50,7 @@ def prepare! @candidates.delete_if { |key, _value| BUILTINS.keys.include? key } @altered = save.keys - @candidates.keys @candidates.each do |service, definition| - checker = Carioca::Services::Validator.new service: service, definition: definition + checker = Carioca::Services::Validator.new(service:, definition:) checker.validate! @validated[service] = checker.definition end diff --git a/lib/carioca/services/debug.rb b/lib/carioca/services/debug.rb index fca8fa7..8dd3b89 100644 --- a/lib/carioca/services/debug.rb +++ b/lib/carioca/services/debug.rb @@ -4,7 +4,7 @@ module Carioca module Services class Debugger def self.get(service:, trace: Carioca::Registry.config.debugger_tracer) - ProxyDebug.new service: service, trace: trace + ProxyDebug.new service:, trace: end end @@ -19,14 +19,14 @@ def initialize(service:, trace:) @tracer_type = trace end - def method_missing(methodname, *args, **keywords, &block) + def method_missing(methodname, *args, **keywords, &) trace message: "BEGIN CALL for service #{@service} " trace message: "Method called: #{methodname} " trace message: "args : #{args.join ' '}" trace message: "keywords : #{keywords}" if block_given? trace message: 'block given' - a = @service.send(methodname, *args, **keywords, &block) + a = @service.send(methodname, *args, **keywords, &) else a = @service.send(methodname, *args, **keywords) end diff --git a/lib/carioca/services/finisher.rb b/lib/carioca/services/finisher.rb index f3246bb..aa70a8f 100644 --- a/lib/carioca/services/finisher.rb +++ b/lib/carioca/services/finisher.rb @@ -54,8 +54,8 @@ def initialize def terminate(return_case: nil, exit_case: nil, more: nil) raise 'Case must be a return or an exit' if return_case && exit_case - do_exit!(exit_case: exit_case, more: more) if exit_case - do_return(return_case: return_case, more: more) if return_case + do_exit!(exit_case:, more:) if exit_case + do_return(return_case:, more:) if return_case end # exiter @@ -86,39 +86,38 @@ def do_return(return_case: :status_ok, more: nil) end def secure_raise(message: 'unknown error', error_case: :status_ko) - raise SpecificError.new message, error_case: error_case + raise SpecificError.new(message, error_case:) end def secure_api_return(data: nil, return_case: nil, structured: false, json: true, status: true) result = {} begin data = yield if block_given? - result = structured ? do_return(return_case: return_case).merge({ data: data }) : data - rescue Exception => e - key = (e.respond_to? :error_case) ? e.error_case : :status_ko - more = (e.respond_to? :error_case) ? e.message : "#{e.class} : #{e.message}" - result = do_return return_case: key, more: more + result = structured ? do_return(return_case:).merge({ data: }) : data + rescue StandardError => e + key = e.respond_to?(:error_case) ? e.error_case : :status_ko + more = e.respond_to?(:error_case) ? e.message : "#{e.class} : #{e.message}" + result = do_return return_case: key, more: end if status && structured && json p result { status: result[:code], data: JSON.pretty_generate(JSON.parse(result.to_json)) } elsif json - return JSON.pretty_generate(JSON.parse(result.to_json)) if json + JSON.pretty_generate(JSON.parse(result.to_json)) if json else result end end def secure_execute!(exit_case: :success_exit) - result = {} begin more = yield - rescue Exception => e - key = (e.respond_to? :error_case) ? e.error_case : :error_exit - more = (e.respond_to? :error_case) ? e.message : "#{e.class} : #{e.message}" - exit_case = key + key = exit_case + rescue StandardError => e + key = e.respond_to?(:error_case) ? e.error_case : :error_exit + more = e.respond_to?(:error_case) ? e.message : "#{e.class} : #{e.message}" end - do_exit! exit_case: exit_case, more: more + do_exit! exit_case: key, more: end end end diff --git a/lib/carioca/services/init.rb b/lib/carioca/services/init.rb index b73e399..64a1b8a 100644 --- a/lib/carioca/services/init.rb +++ b/lib/carioca/services/init.rb @@ -1,3 +1,3 @@ # frozen_string_literal: true -Dir["#{File.dirname(__FILE__)}/*.rb"].sort.each { |file| require file unless File.basename(file) == 'init.rb' } +Dir["#{File.dirname(__FILE__)}/*.rb"].each { |file| require file unless File.basename(file) == 'init.rb' } diff --git a/lib/carioca/services/output.rb b/lib/carioca/services/output.rb index e8a6186..6f5c8ec 100644 --- a/lib/carioca/services/output.rb +++ b/lib/carioca/services/output.rb @@ -76,12 +76,12 @@ class Provider LEVELS.each do |method| define_method(method) do |message, session = nil, source = 'Carioca->Output'| - display(level: method, message: message, session: session, source: source) + display(level: method, message:, session:, source:) end end @@alias.each_key do |method| define_method(method) do |message, session = nil, source = 'Carioca->Output'| - display(level: method, message: message, session: session, source: source) + display(level: method, message:, session:, source:) end end @@ -104,7 +104,7 @@ def add_alias(newalias:, level:) raise "Bad Level : #{level}" unless LEVELS.include? level self.class.define_method(newalias) do |message, session = nil| - display({ level: newalias, message: message, session: session }) + display({ level: newalias, message:, session: }) end end diff --git a/lib/carioca/services/sanitycheck.rb b/lib/carioca/services/sanitycheck.rb index 315cbd6..6449721 100644 --- a/lib/carioca/services/sanitycheck.rb +++ b/lib/carioca/services/sanitycheck.rb @@ -14,14 +14,13 @@ def initialize @finisher = registry.get_service name: :finisher @schema = {} if @configuration.settings.include? :sanitycheck - @schema = (@configuration.settings.sanitycheck.include? :rules) ? @configuration.settings.sanitycheck.rules : {} + @schema = @configuration.settings.sanitycheck.include?(:rules) ? @configuration.settings.sanitycheck.rules : {} end end def run unless @schema.empty? begin - result = [] @output.info @i18n.t('sanitycheck.run.start') error_number = 0 @schema.each do |item| @@ -29,10 +28,10 @@ def run item.delete(:test) res = send(testcase, **item) if res.empty? - @output.ok @i18n.t('sanitycheck.run.ok', testcase: testcase, name: item[:name].to_s) + @output.ok @i18n.t('sanitycheck.run.ok', testcase:, name: item[:name].to_s) else pbm = res.map(&:to_s).join(',') - @output.ko @i18n.t('sanitycheck.run.ko', testcase: testcase, name: item[:name].to_s, pbm: pbm) + @output.ko @i18n.t('sanitycheck.run.ko', testcase:, name: item[:name].to_s, pbm:) error_number = + 1 end end @@ -41,7 +40,7 @@ def run else @output.success @i18n.t('sanitycheck.success') end - rescue Exception + rescue StandardError @finisher.secure_raise message: @i18n.t('sanitychek.error'), error_case: :status_ko end end @@ -62,7 +61,7 @@ def verify_folder(name:, mode: '755', owner: nil, group: nil) stat = File.stat(full_name) if mode - tested_mode = '%o' % stat.mode + tested_mode = format('%o', stat.mode) res << :mode if tested_mode[-3..] != mode end res << :owner if owner && (Etc.getpwuid(stat.uid).name != owner) @@ -93,7 +92,7 @@ def verify_file(name:, mode: '644', owner: nil, group: nil) stat = File.stat(full_name) if mode - tested_mode = '%o' % stat.mode + tested_mode = format('%o', stat.mode) res << :mode if tested_mode[-3..] != mode end res << :owner if owner && (Etc.getpwuid(stat.uid).name != owner) @@ -103,11 +102,10 @@ def verify_file(name:, mode: '644', owner: nil, group: nil) # TCP/IP service checker # @return [Bool] status - # @option [String] :name display name # @option [String] :host hostname # @option [String] :port TCP port # @option [String] :url full URL, priority on :host and :port - def verify_service(name: nil, url: nil, host: nil, port: nil) + def verify_service(url: nil, host: nil, port: nil) if url uri = URI.parse(url) host = uri.host diff --git a/lib/carioca/services/setup.rb b/lib/carioca/services/setup.rb index 46a54f0..9a581c3 100644 --- a/lib/carioca/services/setup.rb +++ b/lib/carioca/services/setup.rb @@ -14,7 +14,7 @@ def initialize @finisher = registry.get_service name: :finisher @schema = {} if @configuration.settings.include? :setup - @schema = (@configuration.settings.setup.include? :rules) ? @configuration.settings.setup.rules : {} + @schema = @configuration.settings.setup.include?(:rules) ? @configuration.settings.setup.rules : {} end end @@ -27,7 +27,7 @@ def execute! item.delete(:action) send action, **item end - rescue Exception + rescue StandardError @finisher.secure_raise message: @i18n.t('setup.error'), error_case: :status_ko end end @@ -47,7 +47,7 @@ def execute! def install_file(source:, target:, mode: '644', owner: nil, group: nil, force: true, gem: true, gem_name: 'carioca') @output.item @i18n.t('setup.install', file: target) full_target = File.expand_path(target) - source = gem ? @toolbox.search_file_in_gem(gem_name, source) : source + source = @toolbox.search_file_in_gem(gem: gem_name, file: source) if gem FileUtils.copy source, full_target if force FileUtils.chmod mode.to_i(8), full_target FileUtils.chown owner, group, full_target if owner && group @@ -70,9 +70,9 @@ def make_folder(path:, mode: '644', owner: nil, group: nil) # @option [String] :source path of the file # @option [String] :link path of the symlink def make_link(source:, link:) - full_source = File.expand_path(source) - full_link = File.expand_path(link) - @output.item @i18n.t('setup.ln', target: link, source: source) + File.expand_path(source) + File.expand_path(link) + @output.item @i18n.t('setup.ln', target: link, source:) FileUtils.rm link if File.symlink?(link) && !File.exist?(link) FileUtils.ln_s source, link unless File.exist? link end diff --git a/lib/carioca/services/toolbox.rb b/lib/carioca/services/toolbox.rb index dd2e15f..d02fdb0 100644 --- a/lib/carioca/services/toolbox.rb +++ b/lib/carioca/services/toolbox.rb @@ -10,7 +10,7 @@ def self.describe methods(false).each do |meth| next if meth == :describe - result[meth] = send meth, **{ describe: true } + result[meth] = send meth, describe: true end result end @@ -56,17 +56,17 @@ def self.get_processes(patterns: [], pattern: nil, full: false, description: 'Ge # @param [String] _file a file relative path in the gem # @return [String] the path of the file, if found. # @return [False] if not found - def self.search_file_in_gem(_gem = nil, _file = nil, description: 'Retrieve absolute path of a file in a specific gem', describe: false) + def self.search_file_in_gem(gem: nil, file: nil, description: 'Retrieve absolute path of a file in a specific gem', describe: false) return description if describe if Gem::Specification.respond_to?(:find_by_name) begin - spec = Gem::Specification.find_by_name(_gem) + spec = Gem::Specification.find_by_name(gem) rescue LoadError spec = nil end else - spec = Gem.searcher.find(_gem) + spec = Gem.searcher.find(gem) end if spec res = if Gem::Specification.respond_to?(:find_by_name) @@ -75,7 +75,7 @@ def self.search_file_in_gem(_gem = nil, _file = nil, description: 'Retrieve abso Gem.searcher.lib_dirs_for(spec).split('/') end res.pop - services_path = res.join('/').concat("/#{_file}") + services_path = res.join('/').concat("/#{file}") return services_path if File.exist?(services_path) end @@ -84,7 +84,7 @@ def self.search_file_in_gem(_gem = nil, _file = nil, description: 'Retrieve abso # facility to verifying if the active process run as root # @return [Bool] status - def self.is_root?(description: 'Verify if active current processus is running as root', describe: false) + def self.root?(description: 'Verify if active current processus is running as root', describe: false) return description if describe Process.uid.zero? diff --git a/samples/test.rb b/samples/test.rb index 57e2ab0..6acc0af 100644 --- a/samples/test.rb +++ b/samples/test.rb @@ -133,7 +133,7 @@ def test3 output.emoji = true output.color = true -puts "\nTest 9 : Service toolbox list of avaibles methodes" +puts "\nTest 9 : Service toolbox list of availables methodes" toolbox = Carioca::Registry.get.get_service name: :toolbox pp toolbox.describe @@ -141,7 +141,7 @@ def test3 pp toolbox.user_root puts "\nTest 11 : Service toolbox test of simple methode : :search_file_in_gem" -pp toolbox.search_file_in_gem('carioca', 'config/locales/en.yml') +pp toolbox.search_file_in_gem(gem: 'carioca', file: 'config/locales/en.yml') puts "\nTest 12 : Service setup execute setup schema from configuration" setup = Carioca::Registry.get.get_service name: :setup