carioca 2.0.4 → 2.0.6

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,189 @@
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
+ skip: { value: 'U{23E9}', alt: '[I]', text: '(SKIPPED)'}
46
+ }.freeze
47
+ LEVELS = %i[debug info warn error fatal unknown].freeze
48
+ ALIAS = {
49
+ flat: :info,
50
+ item: :info,
51
+ ok: :info,
52
+ ko: :error,
53
+ trigger: :info,
54
+ scheduling: :info,
55
+ arrow: :info,
56
+ sending: :info,
57
+ calling: :info,
58
+ receive: :info,
59
+ success: :info,
60
+ skipped: :info
61
+
62
+ }.freeze
63
+ end
64
+
65
+ class Provider
66
+ include FormatsMapping
67
+
68
+ attr_accessor :mode, :emoji, :color
69
+
70
+ @@alias = ALIAS.dup
71
+ @@colors = COLORS.dup
72
+ @@emoji = EMOJI.dup
73
+
74
+ MODE = %i[mono dual log].freeze
75
+
76
+ LEVELS.each do |method|
77
+ define_method(method) do |message, session = nil, source = 'Carioca->Output'|
78
+ display(level: method, message: message, session: session, source: source)
79
+ end
80
+ end
81
+ @@alias.each_key do |method|
82
+ define_method(method) do |message, session = nil, source = 'Carioca->Output'|
83
+ display(level: method, message: message, session: session, source: source)
84
+ end
85
+ end
86
+
87
+ def map_color(color:, analias:)
88
+ raise 'Color must be a Symbol' unless color.instance_of?(Symbol)
89
+ raise "Missing alias : #{analias}" unless LEVELS.include? analias
90
+
91
+ @@alias[analias] = color
92
+ end
93
+
94
+ def map_emoji(emoji:, analias:)
95
+ raise 'Emoji must be a String' unless color.instance_of?(String)
96
+ raise "Missing alias : #{analias}" unless LEVELS.include? analias
97
+
98
+ @@alias[analias] = emoji
99
+ end
100
+
101
+ def add_alias(newalias:, level:)
102
+ raise 'Alias must be a Symbol' unless newalias.instance_of?(Symbol)
103
+ raise "Bad Level : #{level}" unless LEVELS.include? level
104
+
105
+ self.class.define_method(newalias) do |message, session = nil|
106
+ display({ level: newalias, message: message, session: session })
107
+ end
108
+ end
109
+
110
+ # constructor
111
+ def initialize(level: :debug, mode: :mono, emoji: true, colors: true)
112
+ registry = Carioca::Registry.get
113
+ @logger = registry.get_service name: :logger
114
+ @i18n = registry.get_service name: :i18n
115
+ @debug = Carioca::Registry.config.debug?
116
+ self.level = level
117
+ @mode = mode
118
+ @emoji = check_unicode_term ? emoji : false
119
+ @color = colors
120
+ set = []
121
+ set.push mode
122
+ set.push :emoji if @emoji
123
+ set.push :colors if @color
124
+ @logger.debug('Carioca->Output') { @i18n.t('output.load.context', confset: set.to_s) } if @debug
125
+ raise "Unknown output mode : #{@mode}" unless MODE.include? @mode
126
+ end
127
+
128
+ # build a session number
129
+ # @return [String] Session number
130
+ def generate_session
131
+ "#{Time.now.to_i}#{rand(999)}"
132
+ end
133
+
134
+ # getter for the current level
135
+ # @return [Symbol] level
136
+ def level
137
+ @active_levels.first
138
+ end
139
+
140
+ # virtual setter for level, set the current level
141
+ # @raise a badLevel in case of bad level
142
+ # @param [Symbol] level
143
+ def level=(level)
144
+ raise "Bad Level : #{level}" unless LEVELS.include? level
145
+
146
+ @active_levels = LEVELS.dup
147
+ @active_levels.shift(LEVELS.index(level))
148
+ end
149
+
150
+ # check if unicode must be used with term ENV
151
+ # @return [Boolean]
152
+ def check_unicode_term
153
+ return false unless ENV.include? 'TERM'
154
+
155
+ ENV.values_at('LC_ALL', 'LC_CTYPE',
156
+ 'LANG').compact.first.include?('UTF-8') && ENV.values_at('TERM').first.include?('xterm')
157
+ end
158
+
159
+ # abstract method for log wrapper
160
+ # @param [Hash] params
161
+ # @option params [Symbol] :level, a valid level in LEVELS or ALIAS
162
+ # @option params [String] :message text
163
+ # @option params [String] :session an id/timestamp of session
164
+ def display(level:, message:, session:, source:)
165
+ message << " (#{session})" if session
166
+ save = message.dup
167
+ target_level = @@alias.keys.include?(level) ? @@alias[level] : level
168
+ if @active_levels.include? target_level
169
+ if @color
170
+ pastel = ::Pastel.new
171
+ message = pastel.send @@colors[level], message
60
172
  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
-
173
+ if @@emoji.include? level
174
+ pattern = @emoji ? @@emoji[level][:value] : @@emoji[level][:alt]
175
+ pattern = "#{pattern} #{@@emoji[level][:text]}" if @@emoji[level].include?(:text) && !@emoji
176
+ message = "#{pattern} #{message}" unless pattern.empty?
183
177
  end
184
-
185
-
178
+ if @mode == :dual
179
+ save = "#{@@emoji[level][:text]} #{save}" if !LEVELS.include?(level) && (@@emoji[level].include? :text)
180
+ block = proc { save }
181
+ @logger.send target_level, source, &block
182
+ end
183
+ puts message if @mode == :mono or @mode == :dual
184
+ end
186
185
  end
186
+ end
187
187
  end
188
+ end
188
189
  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'