carioca 2.0.2 → 2.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,140 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ # monkey patching of Hash
1
4
  class Hash
2
- # monkey patching
3
- def deep_symbolize
4
- target = dup
5
- target.inject({}) do |memo, (key, value)|
6
- value = value.deep_symbolize if value.is_a?(Hash)
7
- memo[key.to_sym] = value
8
- memo
9
- end
5
+ def deep_symbolize
6
+ target = dup
7
+ target.each_with_object({}) do |(key, value), memo|
8
+ value = value.deep_symbolize if value.is_a?(Hash)
9
+ memo[key.to_sym] = value
10
10
  end
11
-
12
- def method_missing(name, *args, &block)
13
- if name.to_s =~ /(.+)=$/
14
- self[$1.to_sym] = args.first
15
- else
16
- self[name.to_sym]
17
- end
11
+ end
12
+
13
+ def method_missing(name, *args)
14
+ if name.to_s =~ /(.+)=$/
15
+ self[Regexp.last_match(1).to_sym] = args.first
16
+ else
17
+ self[name.to_sym]
18
18
  end
19
-
20
19
  end
21
-
22
-
23
-
24
- module Carioca
25
- module Services
26
- module Config
27
-
28
- class ConfigFile
29
- include Carioca::Constants
30
- attr_accessor :filename, :data
31
- attr_reader :error
32
-
33
- def initialize(filename:)
34
- @filename = filename
35
- @data = {}
36
- @error = ""
37
- open
38
-
39
- end
40
-
41
- def error?
42
- return !@error.empty?
43
- end
44
-
45
- def create!(force: false)
46
- write_ok = true
47
- write_ok = force if File::exist? @filename
48
- File.open(@filename, 'w') { |file| file.write(@data.to_yaml) } if write_ok
49
- end
50
-
51
- def open
52
- if File::exist?(@filename) then
53
- begin
54
- @data = YAML.load_file(@filename)
55
- rescue Exception => e
56
- @error = e.message
57
- @data = {}
58
- end
59
- end
60
- prepare!
61
- end
62
-
63
- private
64
- def prepare!
65
- config = Carioca::Registry.config
66
- @data = {} unless @data.class == Hash
67
- @data.delete_if {|key,value| config.config_root != key }
68
- @data[config.config_root] = {} unless @data.include? config.config_root
69
- config.supported_environment.each do |evt|
70
- @data[config.config_root][evt] = {} unless @data[config.config_root].include? evt
71
- end
72
- @data[config.config_root][:default] = {} unless @data[config.config_root].include? :default
73
- create!
74
- end
20
+ end
75
21
 
22
+ # the carioca Namespace
23
+ module Carioca
24
+ # the service Namespace
25
+ module Services
26
+ # the Config Namespade
27
+ module Config
28
+ # config file manager
29
+ class ConfigFile
30
+ include Carioca::Constants
31
+ attr_accessor :filename, :data
32
+ attr_reader :error
33
+
34
+ def initialize(filename:)
35
+ @filename = filename
36
+ @data = {}
37
+ @error = ''
38
+ open
39
+ end
40
+
41
+ def error?
42
+ !@error.empty?
43
+ end
44
+
45
+ def create!(force: false)
46
+ write_ok = true
47
+ write_ok = force if File.exist? @filename
48
+ File.write(@filename, @data.to_yaml) if write_ok
49
+ end
50
+
51
+ def open
52
+ if File.exist?(@filename)
53
+ begin
54
+ @data = YAML.load_file(@filename)
55
+ rescue StandardError => e
56
+ @error = e.message
57
+ @data = {}
76
58
  end
77
- class Settings < Hash
78
-
79
- attr_accessor :config_file
80
- attr_accessor :stage
81
-
82
- include Carioca::Helpers
83
-
84
- def initialize(config_filename: , stage:, root:)
85
- registry = Carioca::Registry.get
86
- @logger = registry.get_service name: :logger
87
- @i18n = registry.get_service name: :i18n
88
-
89
- @stage = stage
90
- @root = root
91
- @config_file = Carioca::Services::Config::ConfigFile::new filename: config_filename
92
- initconf
93
- end
94
-
95
- def refresh
96
- initconf
97
- end
98
-
99
-
100
-
101
- private
102
- def initconf
103
- newsets = {}
104
- @logger.debug("Carioca->Config") { @i18n.t('config.load.error', message: @config_file.error) } if @config_file.error?
105
- @content = @config_file.data
106
-
107
- unless @stage then
108
- newsets = @content
109
- else
110
- self.merge! @content[@root][:default]
111
- data = @content[@root][@stage]
112
- self.deep_merge! data
113
-
114
- end
115
-
116
-
117
- end
118
-
119
-
120
- end
59
+ end
60
+ prepare!
61
+ end
121
62
 
63
+ private
64
+
65
+ def prepare!
66
+ config = Carioca::Registry.config
67
+ @data = {} unless @data.instance_of?(Hash)
68
+ @data.delete_if { |key, _value| config.config_root != key }
69
+ @data[config.config_root] = {} unless @data.include? config.config_root
70
+ config.supported_environment.each do |evt|
71
+ @data[config.config_root][evt] = {} unless @data[config.config_root].include? evt
72
+ end
73
+ @data[config.config_root][:default] = {} unless @data[config.config_root].include? :default
74
+ create!
75
+ end
76
+ end
122
77
 
123
-
124
- class Factory
125
- extend Forwardable
126
-
127
- attr_accessor :settings
128
- def_delegators :@settings, :refresh
78
+ # A setting object
79
+ class Settings < Hash
80
+ attr_accessor :config_file, :stage
81
+
82
+ include Carioca::Helpers
83
+
84
+ def initialize(config_filename:, stage:, root:)
85
+ super
86
+ registry = Carioca::Registry.get
87
+ @logger = registry.get_service name: :logger
88
+ @i18n = registry.get_service name: :i18n
89
+ @debug = Carioca::Registry.config.debug?
90
+ @stage = stage
91
+ @root = root
92
+ @config_file = Carioca::Services::Config::ConfigFile.new filename: config_filename
93
+ initconf
94
+ end
129
95
 
96
+ def refresh
97
+ initconf
98
+ end
130
99
 
131
- def initialize(**keywords)
132
- @settings = Carioca::Services::Config::Settings.new(**keywords)
133
- end
134
-
100
+ private
101
+
102
+ def initconf
103
+ if @config_file.error? && @debug
104
+ @logger.debug('Carioca->Config') do
105
+ @i18n.t('config.load.error', message: @config_file.error)
106
+ end
107
+ end
108
+ @content = @config_file.data
109
+
110
+ if @stage
111
+ merge! @content[@root][:default]
112
+ deep_merge! @content[@root][@stage]
113
+ end
114
+ if @debug
115
+ @logger.debug('Carioca->Config') do
116
+ @i18n.t('config.load.success', from: @config_file.filename)
135
117
  end
118
+ end
119
+ end
120
+ end
121
+
122
+ # the settings object factory
123
+ class Factory
124
+ extend Forwardable
125
+
126
+ attr_accessor :settings
127
+
128
+ def_delegators :@settings, :refresh
129
+
130
+ def initialize(**keywords)
131
+ @settings = Carioca::Services::Config::Settings.new(**keywords)
136
132
  end
137
-
133
+ end
138
134
  end
135
+ end
139
136
  end
140
-
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: false
2
+
3
+ module Carioca
4
+ module Services
5
+ class Debugger
6
+ def self.get(service:, trace: Carioca::Registry.config.debugger_tracer)
7
+ ProxyDebug.new service: service, trace: trace
8
+ end
9
+ end
10
+
11
+ class ProxyDebug
12
+ def initialize(service:, trace:)
13
+ registry = Carioca::Registry.get
14
+ @service = registry.get_service name: service
15
+ @tracers = %i[output logger]
16
+ raise "Debugger :trace is not valid : #{trace}, must be in : #{@tracers}" unless @tracers.include? trace
17
+
18
+ @tracer = registry.get_service name: trace
19
+ @tracer_type = trace
20
+ end
21
+
22
+ def method_missing(methodname, *args, **keywords, &block)
23
+ trace message: "BEGIN CALL for service #{@service} "
24
+ trace message: "Method called: #{methodname} "
25
+ trace message: "args : #{args.join ' '}"
26
+ trace message: "keywords : #{keywords}"
27
+ if block_given?
28
+ trace message: 'block given'
29
+ a = @service.send(methodname, *args, **keywords, &block)
30
+ else
31
+ a = @service.send(methodname, *args, **keywords)
32
+ end
33
+ trace message: "=> method returned: #{a} "
34
+ trace message: 'END CALL'
35
+
36
+ a
37
+ end
38
+
39
+ def trace(message:)
40
+ if @tracer_type == :output
41
+ save = @tracer.mode
42
+ @tracer.mode = :mono
43
+ @tracer.debug message
44
+ @tracer.mode = save
45
+ else
46
+ @tracer.debug('Carioca->ProxyDebug') { message }
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,20 +1,19 @@
1
- module Carioca
2
- module Services
3
- class I18n
4
-
5
- def I18n.get(default_locale: , load_path:, locales_availables: )
6
- ::I18n::Backend::Simple.include(::I18n::Backend::Fallbacks)
7
- ::I18n.load_path << load_path
8
- ::I18n.default_locale = default_locale
9
- ::I18n.fallbacks = locales_availables
10
- return ::I18n
11
- end
12
-
13
- def I18n.get_system_locale
14
- return ::Locale.candidates.to_s.split('_').first.to_sym
15
- end
1
+ # frozen_string_literal: true
16
2
 
3
+ module Carioca
4
+ module Services
5
+ class I18n
6
+ def self.get(default_locale:, load_path:, locales_availables:)
7
+ ::I18n::Backend::Simple.include(::I18n::Backend::Fallbacks)
8
+ ::I18n.load_path << load_path
9
+ ::I18n.default_locale = default_locale
10
+ ::I18n.fallbacks = locales_availables
11
+ ::I18n
12
+ end
17
13
 
18
- end
14
+ def self.system_locale
15
+ ::Locale.candidates.to_s.split('_').first.to_sym
16
+ end
19
17
  end
18
+ end
20
19
  end
@@ -1,2 +1,3 @@
1
- # encoding: UTF-8
2
- Dir[File.dirname(__FILE__) + '/*.rb'].sort.each {|file| require file unless File.basename(file) == 'init.rb'}
1
+ # frozen_string_literal: true
2
+
3
+ Dir["#{File.dirname(__FILE__)}/*.rb"].sort.each { |file| require file unless File.basename(file) == 'init.rb' }