dim-toolkit 2.1.1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,126 +1,126 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'file_helper'
4
-
5
- module Dim
6
- module Helpers
7
- module AttributeHelper
8
- include FileHelper
9
-
10
- CHECK_SINGLE_ENUM = :check_single_enum
11
- CHECK_MULTI_ENUM = :check_multi_enum
12
- FORMAT_STYLES = {
13
- 'list' => 'list',
14
- 'multi' => 'multi',
15
- 'single' => 'single',
16
- 'split' => 'split',
17
- 'text' => 'text'
18
- }.freeze
19
- @filepath = ''
20
-
21
- def resolve_attributes(folder:, filename:)
22
- @filepath = "#{folder}/#{filename}"
23
- attributes = open_yml_file(folder, filename, allow_empty_file: true)
24
- unless attributes
25
- puts "Warning: empty file detected; skipped loading of #{filename}"
26
- return
27
- end
28
-
29
- check_for_default_attributes(attributes, filename)
30
-
31
- attributes.each do |attribute, attr_config|
32
- attr_config.transform_keys!(&:to_sym)
33
-
34
- change_type_to_format_style(attr_config)
35
- validate_format_style(attribute, attr_config)
36
- add_check_value(attr_config)
37
- validate_default(attribute, attr_config)
38
- validate_allowed(attribute, attr_config)
39
- validate_allowed_for_enum(attribute, attr_config)
40
- validate_default_for_enum(attribute, attr_config)
41
-
42
- symbolize_values(attr_config)
43
- end
44
-
45
- attributes
46
- end
47
-
48
- def check_for_default_attributes(attributes, filename)
49
- common_values = Requirement::SYNTAX.keys & attributes.keys
50
- return if common_values.empty?
51
-
52
- Dim::ExitHelper.exit(
53
- code: 1,
54
- filename: @filepath,
55
- msg: 'Defining standard attributes as a custom attributes is not allowed; ' \
56
- "#{common_values.join(',')} in #{filename}"
57
- )
58
- end
59
-
60
- # TODO: change "format_style" to "type" in requirements syntax and then remove this conversion
61
- def change_type_to_format_style(config)
62
- config[:format_style] = config.delete(:type)
63
- end
64
-
65
- def validate_format_style(attribute, config)
66
- return if FORMAT_STYLES.values.include?(config[:format_style])
67
-
68
- exit_with_error(config: 'type', config_value: config[:format_style], attribute: attribute)
69
- end
70
-
71
- def add_check_value(config)
72
- config[:check] = CHECK_SINGLE_ENUM if config[:format_style] == FORMAT_STYLES['single']
73
- config[:check] = CHECK_MULTI_ENUM if config[:format_style] == FORMAT_STYLES['multi']
74
- end
75
-
76
- def validate_default(attribute, config)
77
- return unless config[:default] == 'auto'
78
-
79
- exit_with_error(config: 'default', config_value: config[:default], attribute: attribute)
80
- end
81
-
82
- def validate_allowed(attribute, config)
83
- return if config[:allowed].nil? || config[:allowed].is_a?(Array)
84
-
85
- exit_with_error(config: 'allowed', config_value: config[:allowed], attribute: attribute)
86
- end
87
-
88
- def validate_allowed_for_enum(attribute, config)
89
- return unless FORMAT_STYLES.fetch_values('single', 'multi').include?(config[:format_style])
90
-
91
- return if config[:allowed].is_a?(Array) && config[:allowed].map { |val| val.is_a?(String) }.all?
92
-
93
- Dim::ExitHelper.exit(
94
- code: 1,
95
- filename: @filepath,
96
- msg: "Allowed value must be list of strings; invalid allowed value for #{attribute}"
97
- )
98
- end
99
-
100
- def validate_default_for_enum(attribute, config)
101
- return unless FORMAT_STYLES.fetch_values('single', 'multi').include?(config[:format_style])
102
-
103
- return if config[:allowed].include?(config[:default])
104
-
105
- Dim::ExitHelper.exit(
106
- code: 1,
107
- filename: @filepath,
108
- msg: "default value for #{attribute} must be from allowed list of #{config[:allowed]}"
109
- )
110
- end
111
-
112
- def symbolize_values(config)
113
- config[:format_style] = config[:format_style].to_sym if config[:format_style]
114
- config[:format_shift] = config[:format_shift].to_i
115
- config[:default] = '' unless config[:default]
116
- end
117
-
118
- private
119
-
120
- def exit_with_error(config:, config_value:, attribute:)
121
- msg = "Invalid value \"#{config_value}\" for #{config} detected for attribute #{attribute}"
122
- Dim::ExitHelper.exit(code: 1, filename: @filepath, msg: msg)
123
- end
124
- end
125
- end
126
- end
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'file_helper'
4
+
5
+ module Dim
6
+ module Helpers
7
+ module AttributeHelper
8
+ include FileHelper
9
+
10
+ CHECK_SINGLE_ENUM = :check_single_enum
11
+ CHECK_MULTI_ENUM = :check_multi_enum
12
+ FORMAT_STYLES = {
13
+ 'list' => 'list',
14
+ 'multi' => 'multi',
15
+ 'single' => 'single',
16
+ 'split' => 'split',
17
+ 'text' => 'text'
18
+ }.freeze
19
+ @filepath = ''
20
+
21
+ def resolve_attributes(folder:, filename:)
22
+ @filepath = "#{folder}/#{filename}"
23
+ attributes = open_yml_file(folder, filename, allow_empty_file: true)
24
+ unless attributes
25
+ puts "Warning: empty file detected; skipped loading of #{filename}"
26
+ return
27
+ end
28
+
29
+ check_for_default_attributes(attributes, filename)
30
+
31
+ attributes.each do |attribute, attr_config|
32
+ attr_config.transform_keys!(&:to_sym)
33
+
34
+ change_type_to_format_style(attr_config)
35
+ validate_format_style(attribute, attr_config)
36
+ add_check_value(attr_config)
37
+ validate_default(attribute, attr_config)
38
+ validate_allowed(attribute, attr_config)
39
+ validate_allowed_for_enum(attribute, attr_config)
40
+ validate_default_for_enum(attribute, attr_config)
41
+
42
+ symbolize_values(attr_config)
43
+ end
44
+
45
+ attributes
46
+ end
47
+
48
+ def check_for_default_attributes(attributes, filename)
49
+ common_values = Requirement::SYNTAX.keys & attributes.keys
50
+ return if common_values.empty?
51
+
52
+ Dim::ExitHelper.exit(
53
+ code: 1,
54
+ filename: @filepath,
55
+ msg: 'Defining standard attributes as a custom attributes is not allowed; ' \
56
+ "#{common_values.join(',')} in #{filename}"
57
+ )
58
+ end
59
+
60
+ # TODO: change "format_style" to "type" in requirements syntax and then remove this conversion
61
+ def change_type_to_format_style(config)
62
+ config[:format_style] = config.delete(:type)
63
+ end
64
+
65
+ def validate_format_style(attribute, config)
66
+ return if FORMAT_STYLES.values.include?(config[:format_style])
67
+
68
+ exit_with_error(config: 'type', config_value: config[:format_style], attribute: attribute)
69
+ end
70
+
71
+ def add_check_value(config)
72
+ config[:check] = CHECK_SINGLE_ENUM if config[:format_style] == FORMAT_STYLES['single']
73
+ config[:check] = CHECK_MULTI_ENUM if config[:format_style] == FORMAT_STYLES['multi']
74
+ end
75
+
76
+ def validate_default(attribute, config)
77
+ return unless config[:default] == 'auto'
78
+
79
+ exit_with_error(config: 'default', config_value: config[:default], attribute: attribute)
80
+ end
81
+
82
+ def validate_allowed(attribute, config)
83
+ return if config[:allowed].nil? || config[:allowed].is_a?(Array)
84
+
85
+ exit_with_error(config: 'allowed', config_value: config[:allowed], attribute: attribute)
86
+ end
87
+
88
+ def validate_allowed_for_enum(attribute, config)
89
+ return unless FORMAT_STYLES.fetch_values('single', 'multi').include?(config[:format_style])
90
+
91
+ return if config[:allowed].is_a?(Array) && config[:allowed].map { |val| val.is_a?(String) }.all?
92
+
93
+ Dim::ExitHelper.exit(
94
+ code: 1,
95
+ filename: @filepath,
96
+ msg: "Allowed value must be list of strings; invalid allowed value for #{attribute}"
97
+ )
98
+ end
99
+
100
+ def validate_default_for_enum(attribute, config)
101
+ return unless FORMAT_STYLES.fetch_values('single', 'multi').include?(config[:format_style])
102
+
103
+ return if config[:allowed].include?(config[:default])
104
+
105
+ Dim::ExitHelper.exit(
106
+ code: 1,
107
+ filename: @filepath,
108
+ msg: "default value for #{attribute} must be from allowed list of #{config[:allowed]}"
109
+ )
110
+ end
111
+
112
+ def symbolize_values(config)
113
+ config[:format_style] = config[:format_style].to_sym if config[:format_style]
114
+ config[:format_shift] = config[:format_shift].to_i
115
+ config[:default] = '' unless config[:default]
116
+ end
117
+
118
+ private
119
+
120
+ def exit_with_error(config:, config_value:, attribute:)
121
+ msg = "Invalid value \"#{config_value}\" for #{config} detected for attribute #{attribute}"
122
+ Dim::ExitHelper.exit(code: 1, filename: @filepath, msg: msg)
123
+ end
124
+ end
125
+ end
126
+ end
@@ -1,25 +1,25 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../exit_helper'
4
-
5
- module Dim::Helpers
6
- module FileHelper
7
- def open_yml_file(folder, filename, allow_empty_file: false)
8
- file_path = Pathname.new(File.join(folder, filename)).cleanpath.to_s
9
- binary_data = File.binread(file_path).chomp
10
- begin
11
- data = YAML.parse(
12
- binary_data.encode('utf-8', invalid: :replace, undef: :replace, replace: '?'),
13
- filename: file_path
14
- )
15
- rescue Psych::SyntaxError => e
16
- Dim::ExitHelper.exit(code: 1, filename: filename, msg: e.message)
17
- end
18
-
19
- Dim::ExitHelper.exit(code: 1, filename: filename, msg: 'not a valid yaml file') unless data || allow_empty_file
20
- return unless data
21
-
22
- data.to_ruby
23
- end
24
- end
25
- end
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../exit_helper'
4
+
5
+ module Dim::Helpers
6
+ module FileHelper
7
+ def open_yml_file(folder, filename, allow_empty_file: false)
8
+ file_path = Pathname.new(File.join(folder, filename)).cleanpath.to_s
9
+ binary_data = File.binread(file_path).chomp
10
+ begin
11
+ data = YAML.parse(
12
+ binary_data.encode('utf-8', invalid: :replace, undef: :replace, replace: '?'),
13
+ filename: file_path
14
+ )
15
+ rescue Psych::SyntaxError => e
16
+ Dim::ExitHelper.exit(code: 1, filename: filename, msg: e.message)
17
+ end
18
+
19
+ Dim::ExitHelper.exit(code: 1, filename: filename, msg: 'not a valid yaml file') unless data || allow_empty_file
20
+ return unless data
21
+
22
+ data.to_ruby
23
+ end
24
+ end
25
+ end