carioca 1.4 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  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 +58 -0
  7. data/README.md +123 -190
  8. data/Rakefile +5 -66
  9. data/bin/console +15 -0
  10. data/bin/setup +8 -0
  11. data/carioca.gemspec +37 -23
  12. data/config/locales/en.yml +23 -0
  13. data/config/locales/fr.yml +23 -0
  14. data/lib/carioca/configuration.rb +61 -0
  15. data/lib/carioca/constants.rb +68 -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} +5 -4
  21. data/lib/carioca/rake/tasks/config.tasks +46 -0
  22. data/lib/carioca/rake/tasks/gem.tasks +15 -0
  23. data/lib/carioca/rake/tasks/registry.tasks +64 -0
  24. data/lib/carioca/registry.rb +94 -0
  25. data/lib/carioca/registry_file.rb +62 -0
  26. data/lib/carioca/services/config.rb +141 -0
  27. data/lib/carioca/services/debug.rb +48 -61
  28. data/lib/carioca/services/i18n.rb +20 -0
  29. data/lib/carioca/services/init.rb +2 -0
  30. data/lib/carioca/services/output.rb +189 -0
  31. data/lib/carioca/validator.rb +49 -0
  32. data/lib/carioca.rb +2 -319
  33. data/samples/Rakefile +2 -0
  34. data/samples/config/carioca.registry +22 -0
  35. data/samples/config/locales/en.yml +2 -0
  36. data/samples/config/locales/es.yml +2 -0
  37. data/samples/config/locales/fr.yml +2 -0
  38. data/samples/config/settings.yml +24 -0
  39. data/samples/test.rb +118 -0
  40. metadata +77 -143
  41. data/AUTHORS +0 -8
  42. data/COPYRIGHT +0 -24
  43. data/ChangeLog +0 -10
  44. data/INSTALL +0 -7
  45. data/doc/manual.rdoc +0 -225
  46. data/lib/carioca/exceptions.rb +0 -9
  47. data/lib/carioca/private.rb +0 -168
  48. data/lib/carioca/services/configuration.rb +0 -203
  49. data/lib/carioca/services/logger.rb +0 -58
  50. data/lib/carioca/services.rb +0 -143
  51. data/lib/carioca/tasks/registry_init.rake +0 -12
  52. data/spec/carioca_spec.rb +0 -468
  53. data/spec/config/.config +0 -14
  54. data/spec/config/services.registry +0 -55
  55. data/spec/init_spec.rb +0 -2
  56. data/spec/samples/dummy.rb +0 -11
  57. data/spec/samples/otherdummy.rb +0 -11
  58. data/spec/samples/requireddummy.rb +0 -11
  59. data/spec/spec_helper.rb +0 -18
  60. data/ultragreen_roodi_coding_convention.yml +0 -25
@@ -0,0 +1,141 @@
1
+ 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
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
18
+ end
19
+
20
+ 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
75
+
76
+ 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
+ @debug = Carioca::Registry.config.debug?
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? and @debug
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
+ @logger.debug("Carioca->Config") { @i18n.t('config.load.success', from: @config_file.filename) } if @debug
116
+
117
+
118
+ end
119
+
120
+
121
+ end
122
+
123
+
124
+
125
+ class Factory
126
+ extend Forwardable
127
+
128
+ attr_accessor :settings
129
+ def_delegators :@settings, :refresh
130
+
131
+
132
+ def initialize(**keywords)
133
+ @settings = Carioca::Services::Config::Settings.new(**keywords)
134
+ end
135
+
136
+ end
137
+ end
138
+
139
+ end
140
+ end
141
+
@@ -1,71 +1,58 @@
1
- # coding: utf-8
2
- # $BUILTIN
3
- # $NAME debug
4
- # $SERVICE Carioca::Services::ProxyDebug
5
- # $RESOURCE debug
6
- # $DESCRIPTION Proxy class debugger Service for Carioca
7
- # Copyright Ultragreen (c) 2012
8
- #---
9
- # Author : Romain GEORGES
10
- # type : class definition Ruby
11
- # obj : Generic Debugs tools library
12
- #---
13
-
14
-
15
- require 'rubygems'
16
- require 'methodic'
17
-
18
-
19
1
  module Carioca
20
- module Services
2
+ module Services
3
+ class Debugger
21
4
 
5
+ def Debugger.get(service:, trace: Carioca::Registry.config.debugger_tracer)
6
+ return ProxyDebug::new service: service, trace: trace
7
+ end
22
8
 
23
- # Service Debug of Carioca
24
- # Proxy Class Debug for devs
25
- class ProxyDebug
26
9
 
27
- # ProxyDebug service constructor (has a class proxy => so a service proxy)
28
- # @param [Hash] _options the params
29
- # @option _options [String] :service the name of the service you want to proxyfying
30
- # @option _options [Hash] :params the params of the proxyfied service
31
- def initialize(_options)
32
- options = Methodic.get_options(_options)
33
- options.specify_classes_of :service => String
34
- options.specify_presence_of([:service])
35
- options.validate!
36
- if options[:params] then
37
- @obj = Registry.init.start_service :name => options[:service], :params => options[:params]
38
- else
39
- @obj = Registry.init.start_service :name => options[:service]
40
10
  end
41
- @log = Registry.init.get_service :name => 'logger'
42
- @mapped_service = options[:service]
43
- end
44
11
 
45
- # method_missing overload to make the class proxy efficient
46
- def method_missing(methodname, *args,&block)
47
- @log.debug("ProxyDebug") { "BEGIN CALL for mapped service #{@mapped_service} "}
48
- @log.debug("ProxyDebug") { "called: #{methodname} " }
49
- @log.debug("ProxyDebug") { "args : #{args.join " "}" }
50
- if block_given? then
51
- @log.debug("ProxyDebug") { "block given" }
52
- a = @obj.send(methodname, *args,&block)
53
- else
54
- a = @obj.send(methodname, *args)
55
- end
56
- @log.debug("ProxyDebug") { "=> returned: #{a} " }
57
- @log.debug("ProxyDebug") { 'END CALL' }
58
- return a
59
- end
12
+ class ProxyDebug
13
+ def initialize(service:, trace:)
14
+ registry = Carioca::Registry.get
15
+ @service = registry.get_service name: service
16
+ @tracers = [:output, :logger]
17
+ raise "Debugger :trace is not valid : #{trace}, must be in : #{@tracers.to_s}" unless @tracers.include? trace
18
+ @tracer = registry.get_service name: trace
19
+ @tracer_type = trace
20
+ end
21
+
22
+ def method_missing(methodname, *args, **keywords,&block)
23
+
24
+ trace message: "BEGIN CALL for service #{@service} "
25
+ trace message: "Method called: #{methodname} "
26
+ trace message: "args : #{args.join " "}"
27
+ trace message: "keywords : #{keywords.to_s}"
28
+ if block_given? then
29
+ trace message: "block given"
30
+ a = @service.send(methodname, *args, **keywords,&block)
31
+ else
32
+ a = @service.send(methodname, *args, **keywords)
33
+ end
34
+ trace message: "=> method returned: #{a} "
35
+ trace message: 'END CALL'
36
+
37
+ return a
38
+ end
39
+
40
+ def trace(message: )
41
+ if @tracer_type == :output then
42
+ save = @tracer.mode
43
+ @tracer.mode = :mono
44
+ @tracer.debug message
45
+ @tracer.mode = save
46
+ else
47
+ @tracer.debug("Carioca->ProxyDebug") { message }
48
+ end
49
+
50
+ end
51
+
52
+
60
53
 
61
54
 
62
- end
63
- end
64
- end
55
+ end
65
56
 
66
- # protection for loading libs
67
- if $0 == __FILE__ then
68
- puts "#{File::basename(__FILE__)}:"
69
- puts 'this is a RUBY library file'
70
- puts "Copyright (c) Ultragreen"
57
+ end
71
58
  end
@@ -0,0 +1,20 @@
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
16
+
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,2 @@
1
+ # encoding: UTF-8
2
+ Dir[File.dirname(__FILE__) + '/*.rb'].sort.each {|file| require file unless File.basename(file) == 'init.rb'}
@@ -0,0 +1,189 @@
1
+ module Carioca
2
+ module Services
3
+ module Output
4
+
5
+ module FormatsMapping
6
+ COLORS = {
7
+ :unknown => :red,
8
+ :fatal => :red,
9
+ :error => :red,
10
+ :ko => :yellow,
11
+ :warn => :yellow,
12
+ :item => :white,
13
+ :arrow => :white,
14
+ :sending => :white,
15
+ :calling => :white,
16
+ :scheduling => :white,
17
+ :trigger => :white,
18
+ :receive => :white,
19
+ :info => :cyan,
20
+ :ok => :green,
21
+ :success => :green,
22
+ :debug => :magenta,
23
+ :flat => :white
24
+ }
25
+
26
+ EMOJI = {
27
+ :unknown => {:value => "\u{1F4A5}", :alt => '[!!]', :text => "(UNKNOWN)"},
28
+ :fatal => {:value => "\u{26D4}", :alt => '[!!]', :text => "(FATAL)"},
29
+ :error => {:value => "\u{1F6AB}", :alt => '[!]', :text => "(ERROR)"},
30
+ :ko => {:value => "\u{1F44E}", :alt => '[-]', :text => "(KO)"},
31
+ :warn => {:value => "\u{26A0}", :alt => '[/!\]', :text => "(WARNING)"},
32
+ :info => {:value => "\u{2139}", :alt => '[i]', :text => "(INFO)"},
33
+ :item => {:value => " \u{1F539}", :alt => '', :text => " *"},
34
+ :arrow => {:value => " \u{27A1}", :alt => '', :text => " =>"},
35
+ :calling => {:value => "\u{1F4DE}" , :alt => '[C]', :text => "(CALLING)"},
36
+ :scheduling => {:value => "\u{23F2}" , :alt => '[S]', :text => "{SCHEDULING})"},
37
+ :trigger => {:value => "\u{23F0}", :alt => '[T]', :text => "(TRIGGER)"},
38
+ :sending => {:value => "\u{1F4E4}", :alt => '[>]', :text => "(SENDING)"},
39
+ :receive => {:value => "\u{1F4E5}" , :alt => '[<]', :text => "(RECEIVE)"},
40
+ :ok => {:value => "\u{1F44D}" , :alt => '[+]', :text => "(OK)"},
41
+ :success => {:value => "\u{1F4AA}" , :alt => '[+]', :text => "(SUCCESS)"},
42
+ :debug => {:value => "\u{1F41B}" , :alt => '[D]', :text => "(DEBUG)"},
43
+ :flat => {:value => "", :alt => ""}
44
+ }
45
+ LEVELS = [:debug, :info, :warn, :error, :fatal, :unknown ]
46
+ ALIAS = {
47
+ :flat => :info,
48
+ :item => :info,
49
+ :ok => :info,
50
+ :ko => :error,
51
+ :trigger => :info,
52
+ :scheduling => :info,
53
+ :arrow => :info,
54
+ :sending => :info,
55
+ :calling => :info,
56
+ :receive => :info,
57
+ :success => :info
58
+
59
+ }
60
+ end
61
+ class Provider
62
+ include FormatsMapping
63
+
64
+ attr_accessor :mode, :emoji, :color
65
+
66
+ @@alias = ALIAS.dup
67
+ @@colors = COLORS.dup
68
+ @@emoji = EMOJI.dup
69
+
70
+ MODE = [:mono, :dual]
71
+
72
+ LEVELS.each do |method|
73
+ define_method(method) do |message, session = '', source = 'Carioca->Output'|
74
+ self.display(level: method, message: message, session: session, source: source)
75
+ end
76
+ end
77
+ @@alias.keys.each do |method|
78
+ define_method(method) do |message, session = '',source = 'Carioca->Output'|
79
+ self.display( level: method, message: message, session: session, source: source)
80
+ end
81
+ end
82
+
83
+ def map_color(color: , analias: )
84
+ raise "Color must be a Symbol" unless color.class == Symbol
85
+ raise "Missing alias : #{analias}" unless LEVELS.include? analias
86
+ @@alias[analias] = color
87
+ end
88
+
89
+ def map_emoji(emoji: , analias: )
90
+ raise "Emoji must be a String" unless color.class == String
91
+ raise "Missing alias : #{analias}" unless LEVELS.include? analias
92
+ @@alias[analias] = emoji
93
+ end
94
+
95
+
96
+ def add_alias(newalias:, level:)
97
+ raise "Alias must be a Symbol" unless newalias.class == Symbol
98
+ raise "Bad Level : #{level}" unless LEVELS.include? level
99
+ self.class.define_method(newalias) do |message, session = ''|
100
+ self.display({ level: newalias, message: message, session: session})
101
+ end
102
+ end
103
+
104
+
105
+ # constructor
106
+ def initialize(level: :debug, mode: :mono , emoji: true, colors: true)
107
+ registry = Carioca::Registry.get
108
+ @logger = registry.get_service name: :logger
109
+ @i18n = registry.get_service name: :i18n
110
+ @debug = Carioca::Registry.config.debug?
111
+ self.level = level
112
+ @mode = mode
113
+ @emoji = (check_unicode_term)? emoji : false
114
+ @color = colors
115
+ set = []; set.push mode; set.push :emoji if @emoji ; set.push :colors if @color
116
+ @logger.debug("Carioca->Output") { @i18n.t('output.load.context', confset: set.to_s ) } if @debug
117
+ raise "Unknown output mode : #{@mode}" unless MODE.include? @mode
118
+ end
119
+
120
+ # build a session number
121
+ # @return [String] Session number
122
+ def get_session
123
+ return "#{Time.now.to_i.to_s}#{rand(999)}"
124
+ end
125
+
126
+ # getter for the current level
127
+ # @return [Symbol] level
128
+ def level
129
+ return @active_levels.first
130
+ end
131
+
132
+ # virtual setter for level, set the current level
133
+ # @raise a badLevel in case of bad level
134
+ # @param [Symbol] level
135
+ def level=(level)
136
+ raise "Bad Level : #{level}" unless LEVELS.include? level
137
+ @active_levels = LEVELS.dup
138
+ @active_levels.shift(LEVELS.index(level))
139
+ end
140
+
141
+ # check if unicode must be used with term ENV
142
+ # @return [Boolean]
143
+ def check_unicode_term
144
+ return false unless ENV.include? "TERM"
145
+ if ENV.values_at("LC_ALL","LC_CTYPE","LANG").compact.first.include?("UTF-8") and ENV.values_at('TERM').first.include? "xterm" then
146
+ return true
147
+ else
148
+ return false
149
+ end
150
+ end
151
+
152
+
153
+ # abstract method for log wrapper
154
+ # @param [Hash] params
155
+ # @option params [Symbol] :level, a valid level in LEVELS or ALIAS
156
+ # @option params [String] :message text
157
+ def display(level: , message: , session:, source:)
158
+ save = message.dup
159
+ target_level = (@@alias.keys.include? level)? @@alias[level] : level
160
+ if @active_levels.include? target_level then
161
+ if @color then
162
+ pastel = ::Pastel.new
163
+ message = pastel.send @@colors[level], message
164
+ end
165
+ if @@emoji.include? level
166
+ pattern = (@emoji)? @@emoji[level][:value] : @@emoji[level][:alt]
167
+ pattern = "#{pattern} #{@@emoji[level][:text]}" if @@emoji[level].include? :text and !@emoji
168
+ message = pattern + " " + message unless pattern.empty?
169
+ end
170
+ if @mode == :dual
171
+
172
+ pattern = @@emoji[level][:alt]
173
+ unless LEVELS.include? level
174
+ save = "#{@@emoji[level][:text]} #{save}" if @@emoji[level].include? :text
175
+ end
176
+ block = Proc::new {save}
177
+ @logger.send target_level, source, &block
178
+ end
179
+ puts message
180
+ end
181
+ end
182
+
183
+ end
184
+
185
+
186
+ end
187
+ end
188
+ end
189
+
@@ -0,0 +1,49 @@
1
+ module Carioca
2
+ module Services
3
+ class Validator
4
+
5
+ attr_reader :definition
6
+
7
+ include Carioca::Constants
8
+
9
+ def initialize(definition: , service: )
10
+ @definition = definition
11
+ @service = service
12
+ end
13
+
14
+ def validate!
15
+ validate_mandatories
16
+ validate_full_and_type
17
+ validate_not_builtins
18
+ fill_content
19
+ end
20
+
21
+ private
22
+ def validate_not_builtins
23
+
24
+ raise "Builtins reserved name #{@service.to_s}" if BUILTINS.keys.include? @service
25
+ end
26
+
27
+ def validate_mandatories
28
+ SERVICES_MANDATORY_SPECS.keys.each do |spec|
29
+ raise "Key : :#{spec} is mandatory in a service definition" unless @definition.include? spec
30
+ end
31
+ end
32
+
33
+ def validate_full_and_type
34
+ @definition.each do |spec,value|
35
+ raise "Key : :#{spec} is not allowed in a service definition" unless SERVICES_FULL_LIST_SPECS.include? spec
36
+ raise "key : #{spec} must be a : #{SERVICES_FULL_LIST_SPECS[spec].to_s}" unless value.class == SERVICES_FULL_LIST_SPECS[spec]
37
+ if SERVICES_SPECS_DETAIL.include? spec then
38
+ raise "key : #{spec} must be in : #{SERVICES_SPECS_DETAIL[spec].to_s}" unless SERVICES_SPECS_DETAIL[spec].include? value
39
+ end
40
+ end
41
+ end
42
+
43
+ def fill_content
44
+ @definition[:description] = @service.to_s unless @definition.include? :description
45
+ end
46
+
47
+ end
48
+ end
49
+ end