carioca 2.0.4 → 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,189 +1,187 @@
1
+ # frozen_string_literal: false
2
+
1
3
  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
- }
4
+ module Services
5
+ module Output
6
+ module FormatsMapping
7
+ COLORS = {
8
+ unknown: :red,
9
+ fatal: :red,
10
+ error: :red,
11
+ ko: :yellow,
12
+ warn: :yellow,
13
+ item: :white,
14
+ arrow: :white,
15
+ sending: :white,
16
+ calling: :white,
17
+ scheduling: :white,
18
+ trigger: :white,
19
+ receive: :white,
20
+ info: :cyan,
21
+ ok: :green,
22
+ success: :green,
23
+ debug: :magenta,
24
+ flat: :white
25
+ }.freeze
26
+
27
+ EMOJI = {
28
+ unknown: { value: "\u{1F4A5}", alt: '[!!]', text: '(UNKNOWN)' },
29
+ fatal: { value: "\u{26D4}", alt: '[!!]', text: '(FATAL)' },
30
+ error: { value: "\u{1F6AB}", alt: '[!]', text: '(ERROR)' },
31
+ ko: { value: "\u{1F44E}", alt: '[-]', text: '(KO)' },
32
+ warn: { value: "\u{26A0}", alt: '[/!\]', text: '(WARNING)' },
33
+ info: { value: "\u{2139}", alt: '[i]', text: '(INFO)' },
34
+ item: { value: " \u{1F539}", alt: '', text: ' *' },
35
+ arrow: { value: " \u{27A1}", alt: '', text: ' =>' },
36
+ calling: { value: "\u{1F4DE}", alt: '[C]', text: '(CALLING)' },
37
+ scheduling: { value: "\u{23F2}", alt: '[S]', text: '{SCHEDULING})' },
38
+ trigger: { value: "\u{23F0}", alt: '[T]', text: '(TRIGGER)' },
39
+ sending: { value: "\u{1F4E4}", alt: '[>]', text: '(SENDING)' },
40
+ receive: { value: "\u{1F4E5}", alt: '[<]', text: '(RECEIVE)' },
41
+ ok: { value: "\u{1F44D}", alt: '[+]', text: '(OK)' },
42
+ success: { value: "\u{1F4AA}", alt: '[+]', text: '(SUCCESS)' },
43
+ debug: { value: "\u{1F41B}", alt: '[D]', text: '(DEBUG)' },
44
+ flat: { value: '', alt: '' }
45
+ }.freeze
46
+ LEVELS = %i[debug info warn error fatal unknown].freeze
47
+ ALIAS = {
48
+ flat: :info,
49
+ item: :info,
50
+ ok: :info,
51
+ ko: :error,
52
+ trigger: :info,
53
+ scheduling: :info,
54
+ arrow: :info,
55
+ sending: :info,
56
+ calling: :info,
57
+ receive: :info,
58
+ success: :info
59
+
60
+ }.freeze
61
+ end
62
+
63
+ class Provider
64
+ include FormatsMapping
65
+
66
+ attr_accessor :mode, :emoji, :color
67
+
68
+ @@alias = ALIAS.dup
69
+ @@colors = COLORS.dup
70
+ @@emoji = EMOJI.dup
71
+
72
+ MODE = %i[mono dual].freeze
73
+
74
+ LEVELS.each do |method|
75
+ define_method(method) do |message, session = '', source = 'Carioca->Output'|
76
+ display(level: method, message: message, session: session, source: source)
77
+ end
78
+ end
79
+ @@alias.each_key do |method|
80
+ define_method(method) do |message, session = '', source = 'Carioca->Output'|
81
+ display(level: method, message: message, session: session, source: source)
82
+ end
83
+ end
84
+
85
+ def map_color(color:, analias:)
86
+ raise 'Color must be a Symbol' unless color.instance_of?(Symbol)
87
+ raise "Missing alias : #{analias}" unless LEVELS.include? analias
88
+
89
+ @@alias[analias] = color
90
+ end
91
+
92
+ def map_emoji(emoji:, analias:)
93
+ raise 'Emoji must be a String' unless color.instance_of?(String)
94
+ raise "Missing alias : #{analias}" unless LEVELS.include? analias
95
+
96
+ @@alias[analias] = emoji
97
+ end
98
+
99
+ def add_alias(newalias:, level:)
100
+ raise 'Alias must be a Symbol' unless newalias.instance_of?(Symbol)
101
+ raise "Bad Level : #{level}" unless LEVELS.include? level
102
+
103
+ self.class.define_method(newalias) do |message, session = ''|
104
+ display({ level: newalias, message: message, session: session })
105
+ end
106
+ end
107
+
108
+ # constructor
109
+ def initialize(level: :debug, mode: :mono, emoji: true, colors: true)
110
+ registry = Carioca::Registry.get
111
+ @logger = registry.get_service name: :logger
112
+ @i18n = registry.get_service name: :i18n
113
+ @debug = Carioca::Registry.config.debug?
114
+ self.level = level
115
+ @mode = mode
116
+ @emoji = check_unicode_term ? emoji : false
117
+ @color = colors
118
+ set = []
119
+ set.push mode
120
+ set.push :emoji if @emoji
121
+ set.push :colors if @color
122
+ @logger.debug('Carioca->Output') { @i18n.t('output.load.context', confset: set.to_s) } if @debug
123
+ raise "Unknown output mode : #{@mode}" unless MODE.include? @mode
124
+ end
125
+
126
+ # build a session number
127
+ # @return [String] Session number
128
+ def generate_session
129
+ "#{Time.now.to_i}#{rand(999)}"
130
+ end
131
+
132
+ # getter for the current level
133
+ # @return [Symbol] level
134
+ def level
135
+ @active_levels.first
136
+ end
137
+
138
+ # virtual setter for level, set the current level
139
+ # @raise a badLevel in case of bad level
140
+ # @param [Symbol] level
141
+ def level=(level)
142
+ raise "Bad Level : #{level}" unless LEVELS.include? level
143
+
144
+ @active_levels = LEVELS.dup
145
+ @active_levels.shift(LEVELS.index(level))
146
+ end
147
+
148
+ # check if unicode must be used with term ENV
149
+ # @return [Boolean]
150
+ def check_unicode_term
151
+ return false unless ENV.include? 'TERM'
152
+
153
+ ENV.values_at('LC_ALL', 'LC_CTYPE',
154
+ 'LANG').compact.first.include?('UTF-8') && ENV.values_at('TERM').first.include?('xterm')
155
+ end
156
+
157
+ # abstract method for log wrapper
158
+ # @param [Hash] params
159
+ # @option params [Symbol] :level, a valid level in LEVELS or ALIAS
160
+ # @option params [String] :message text
161
+ # @option params [String] :session an id/timestamp of session
162
+ def display(level:, message:, session:, source:)
163
+ message << "(#{session})" if session
164
+ save = message.dup
165
+ target_level = @@alias.keys.include?(level) ? @@alias[level] : level
166
+ if @active_levels.include? target_level
167
+ if @color
168
+ pastel = ::Pastel.new
169
+ message = pastel.send @@colors[level], message
60
170
  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
-
171
+ if @@emoji.include? level
172
+ pattern = @emoji ? @@emoji[level][:value] : @@emoji[level][:alt]
173
+ pattern = "#{pattern} #{@@emoji[level][:text]}" if @@emoji[level].include?(:text) && !@emoji
174
+ message = "#{pattern} #{message}" unless pattern.empty?
183
175
  end
184
-
185
-
176
+ if @mode == :dual
177
+ save = "#{@@emoji[level][:text]} #{save}" if !LEVELS.include?(level) && (@@emoji[level].include? :text)
178
+ block = proc { save }
179
+ @logger.send target_level, source, &block
180
+ end
181
+ puts message
182
+ end
186
183
  end
184
+ end
187
185
  end
186
+ end
188
187
  end
189
-
@@ -1,49 +1,48 @@
1
+ # frozen_string_literal: true
2
+
1
3
  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
4
+ module Services
5
+ class Validator
6
+ attr_reader :definition
7
+
8
+ include Carioca::Constants
9
+
10
+ def initialize(definition:, service:)
11
+ @definition = definition
12
+ @service = service
13
+ end
14
+
15
+ def validate!
16
+ validate_mandatories
17
+ validate_full_and_type
18
+ validate_not_builtins
19
+ fill_content
20
+ end
21
+
22
+ private
46
23
 
24
+ def validate_not_builtins
25
+ raise "Builtins reserved name #{@service}" if BUILTINS.keys.include? @service
26
+ end
27
+
28
+ def validate_mandatories
29
+ SERVICES_MANDATORY_SPECS.each_key do |spec|
30
+ raise "Key : :#{spec} is mandatory in a service definition" unless @definition.include? spec
47
31
  end
32
+ end
33
+
34
+ def validate_full_and_type
35
+ @definition.each do |spec, value|
36
+ raise "Key : :#{spec} is not allowed in a service definition" unless SERVICES_FULL_LIST_SPECS.include? spec
37
+ raise "key : #{spec} must be a : #{SERVICES_FULL_LIST_SPECS[spec]}" unless value.instance_of?(SERVICES_FULL_LIST_SPECS[spec])
38
+
39
+ raise "key : #{spec} must be in : #{SERVICES_SPECS_DETAIL[spec]}" if SERVICES_SPECS_DETAIL.include?(spec) && !(SERVICES_SPECS_DETAIL[spec].include? value)
40
+ end
41
+ end
42
+
43
+ def fill_content
44
+ @definition[:description] = @service.to_s unless @definition.include? :description
45
+ end
48
46
  end
49
- end
47
+ end
48
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: false
2
+
3
+ require 'version'
4
+
5
+ # Carioca module
6
+ # @note namespace
7
+ module Carioca
8
+ # the version of Carioca
9
+ VERSION = Version.current
10
+ end
data/lib/carioca.rb CHANGED
@@ -1,7 +1,4 @@
1
1
  # frozen_string_literal: false
2
2
 
3
- require_relative "carioca/dependencies"
4
-
5
- module Carioca
6
-
7
- end
3
+ require_relative 'carioca/version'
4
+ require_relative 'carioca/dependencies'
data/samples/Rakefile CHANGED
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rubygems'
2
4
  require 'carioca/rake/manage'