carioca 1.3 → 2.0.2

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.
Files changed (58) hide show
  1. checksums.yaml +5 -5
  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 +44 -40
  7. data/README.md +123 -190
  8. data/Rakefile +5 -59
  9. data/bin/console +15 -0
  10. data/bin/setup +8 -0
  11. data/carioca.gemspec +45 -32
  12. data/config/locales/en.yml +19 -0
  13. data/config/locales/fr.yml +19 -0
  14. data/lib/carioca/configuration.rb +60 -0
  15. data/lib/carioca/constants.rb +60 -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} +4 -6
  21. data/lib/carioca/rake/tasks/registry.tasks +57 -0
  22. data/lib/carioca/registry.rb +94 -0
  23. data/lib/carioca/registry_file.rb +62 -0
  24. data/lib/carioca/services/config.rb +140 -0
  25. data/lib/carioca/services/i18n.rb +20 -0
  26. data/lib/carioca/services/init.rb +2 -0
  27. data/lib/carioca/services/output.rb +175 -0
  28. data/lib/carioca/validator.rb +49 -0
  29. data/lib/carioca.rb +2 -319
  30. data/samples/Rakefile +2 -0
  31. data/samples/config/carioca.registry +22 -0
  32. data/samples/config/locales/en.yml +2 -0
  33. data/samples/config/locales/es.yml +2 -0
  34. data/samples/config/locales/fr.yml +2 -0
  35. data/samples/config/settings.yml +24 -0
  36. data/samples/test.rb +98 -0
  37. metadata +76 -168
  38. data/AUTHORS +0 -8
  39. data/COPYRIGHT +0 -24
  40. data/ChangeLog +0 -7
  41. data/INSTALL +0 -7
  42. data/doc/manual.rdoc +0 -225
  43. data/lib/carioca/exceptions.rb +0 -9
  44. data/lib/carioca/private.rb +0 -170
  45. data/lib/carioca/services/configuration.rb +0 -201
  46. data/lib/carioca/services/debug.rb +0 -73
  47. data/lib/carioca/services/logger.rb +0 -58
  48. data/lib/carioca/services.rb +0 -143
  49. data/lib/carioca/tasks/registry_init.rake +0 -11
  50. data/spec/carioca_spec.rb +0 -474
  51. data/spec/config/services.registry +0 -55
  52. data/spec/init_spec.rb +0 -1
  53. data/spec/samples/dummy.rb +0 -10
  54. data/spec/samples/otherdummy.rb +0 -10
  55. data/spec/samples/requireddummy.rb +0 -10
  56. data/spec/spec_helper.rb +0 -18
  57. data/test.rb +0 -12
  58. data/ultragreen_roodi_coding_convention.yml +0 -25
@@ -0,0 +1,140 @@
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
+
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
121
+
122
+
123
+
124
+ class Factory
125
+ extend Forwardable
126
+
127
+ attr_accessor :settings
128
+ def_delegators :@settings, :refresh
129
+
130
+
131
+ def initialize(**keywords)
132
+ @settings = Carioca::Services::Config::Settings.new(**keywords)
133
+ end
134
+
135
+ end
136
+ end
137
+
138
+ end
139
+ end
140
+
@@ -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,175 @@
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 = ''|
74
+ self.display(level: method, message: message, session: session)
75
+ end
76
+ end
77
+ @@alias.keys.each do |method|
78
+ define_method(method) do |message, session = ''|
79
+ self.display( level: method, message: message, session: session)
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
+ self.level = level
108
+ @mode = mode
109
+ @emoji = emoji
110
+ @color = colors
111
+ if @mode == :dual then
112
+ registry = Carioca::Registry.get
113
+ @logger = registry.get_service name: :logger
114
+ end
115
+ raise "Unknown output mode : #{@mode}" unless MODE.include? @mode
116
+ end
117
+
118
+ # build a session number
119
+ # @return [String] Session number
120
+ def get_session
121
+ return "#{Time.now.to_i.to_s}#{rand(999)}"
122
+ end
123
+
124
+ # getter for the current level
125
+ # @return [Symbol] level
126
+ def level
127
+ return @active_levels.first
128
+ end
129
+
130
+ # virtual setter for level, set the current level
131
+ # @raise a badLevel in case of bad level
132
+ # @param [Symbol] level
133
+ def level=(level)
134
+ raise "Bad Level : #{level}" unless LEVELS.include? level
135
+ @active_levels = LEVELS.dup
136
+ @active_levels.shift(LEVELS.index(level))
137
+ end
138
+
139
+
140
+ # abstract method for log wrapper
141
+ # @param [Hash] params
142
+ # @option params [Symbol] :level, a valid level in LEVELS or ALIAS
143
+ # @option params [String] :message text
144
+ def display(level: , message: , session:)
145
+ save = message.dup
146
+ target_level = (@@alias.keys.include? level)? @@alias[level] : level
147
+ if @active_levels.include? target_level then
148
+ if @color then
149
+ pastel = ::Pastel.new
150
+ message = pastel.send @@colors[level], message
151
+ end
152
+ if @@emoji.include? level
153
+ pattern = (@emoji)? @@emoji[level][:value] : @@emoji[level][:alt]
154
+ pattern = "#{pattern} #{@@emoji[level][:text]}" if @@emoji[level].include? :text and !@emoji
155
+ message = pattern + " " + message unless pattern.empty?
156
+ end
157
+ if @mode == :dual
158
+
159
+ pattern = @@emoji[level][:alt]
160
+ unless LEVELS.include? level
161
+ save = "#{@@emoji[level][:text]} #{save}" if @@emoji[level].include? :text
162
+ end
163
+ @logger.send target_level, save
164
+ end
165
+ puts message
166
+ end
167
+ end
168
+
169
+ end
170
+
171
+
172
+ end
173
+ end
174
+ end
175
+
@@ -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