rggen-core 0.29.0 → 0.30.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 18a9718f8b5e18dc5fff8f27e29df34c94017f3964cd63d2f6fa09eb5627a7de
4
- data.tar.gz: 21993e58645d31298ec8a8044f1eb05d436a2a05b10215b8f93f5e91443daf03
3
+ metadata.gz: 977e436971681e3425cdb967c6a86d7debeb89fac6ab33db6e7ab1d831102b8f
4
+ data.tar.gz: 9d49db2290dfdc7b5c7dfc4a0369a2428ed9606f6487cad4c956c7144d46319f
5
5
  SHA512:
6
- metadata.gz: 92e3f06ffd5182911e805c145b08ebea4e74e2e07e618eb7311be527b2abc8e2fdc41b50f82e1565d38798b109d12d325ce15da3262b4ac9aabcad0df077b824
7
- data.tar.gz: fc15825417a32f84cddcc5b776b24a51c2f2bc495e82001ca3912a4e7f992e5bfa0b366a19d6ae37f080d3bad9258213a8701c9b9c5b8bff1332f0835fde4377
6
+ metadata.gz: 999522157b4878363182b31f22a3a12c73c6908409b043c972df3885e9908df66b7093b27bfbe90da30b5dfcebffdcbcdc2ccb1b11da87ebed244f071c991134
7
+ data.tar.gz: 61149e6066502b04f73f525304745b7e555355c141944fb3e03a1043a6ab6761cd0637ffa90ca22c7d3e330d65020831031f525fa4a189b54c9834b78eccc551
@@ -30,12 +30,11 @@ module RgGen
30
30
  entry.define_feature(feature_name, context, &body)
31
31
  end
32
32
 
33
- def enable(feature_or_list_names, feature_names = nil)
34
- if feature_names
35
- list_name = feature_or_list_names
33
+ def enable(list_name = nil, feature_names)
34
+ if list_name
36
35
  (@enabled_features[list_name] ||= []).merge!(Array(feature_names))
37
36
  else
38
- Array(feature_or_list_names).each do |name|
37
+ Array(feature_names).each do |name|
39
38
  @enabled_features.key?(name) || (@enabled_features[name] = nil)
40
39
  end
41
40
  end
@@ -45,21 +44,23 @@ module RgGen
45
44
  @enabled_features.clear
46
45
  end
47
46
 
48
- def delete(feature_or_list_names = nil, feature_names = nil)
49
- if feature_names
50
- @feature_entries[feature_or_list_names]&.delete(feature_names)
51
- elsif feature_or_list_names
52
- Array(feature_or_list_names).each(&@feature_entries.method(:delete))
47
+ def delete(list_name = nil, feature_names)
48
+ if list_name
49
+ @feature_entries[list_name]&.delete(feature_names)
53
50
  else
54
- @feature_entries.clear
51
+ Array(feature_names).each(&@feature_entries.method(:delete))
55
52
  end
56
53
  end
57
54
 
58
- def feature?(feature_or_list_name, feature_name = nil)
59
- if feature_name
60
- list_feature?(feature_or_list_name, feature_name)
55
+ def delete_all
56
+ @feature_entries.clear
57
+ end
58
+
59
+ def feature?(list_name = nil, feature_name)
60
+ if list_name
61
+ list_feature?(list_name, feature_name)
61
62
  else
62
- @feature_entries.key?(feature_or_list_name)
63
+ @feature_entries.key?(feature_name)
63
64
  end
64
65
  end
65
66
 
@@ -23,9 +23,9 @@ module RgGen
23
23
  block_given? && yield(@loader_registries[loader_type])
24
24
  end
25
25
 
26
- def define_value_extractor(loader_type, layers_or_value, value = nil, &body)
26
+ def define_value_extractor(loader_type, layers = nil, value, &body)
27
27
  @loader_registries[loader_type]
28
- .define_value_extractor(layers_or_value, value, &body)
28
+ .define_value_extractor(layers, value, &body)
29
29
  end
30
30
 
31
31
  def build_factory
@@ -94,9 +94,9 @@ module RgGen
94
94
  end
95
95
  end
96
96
 
97
- def enable(feature_or_list_names, feature_names = nil)
97
+ def enable(...)
98
98
  @feature_registries.each_value do |registry|
99
- registry.enable(feature_or_list_names, feature_names)
99
+ registry.enable(...)
100
100
  end
101
101
  end
102
102
 
@@ -104,12 +104,16 @@ module RgGen
104
104
  @feature_registries.each_value(&:enable_all)
105
105
  end
106
106
 
107
- def delete(feature_or_list_names = nil, feature_names = nil)
107
+ def delete(...)
108
108
  @feature_registries.each_value do |registry|
109
- registry.delete(*[feature_or_list_names, feature_names].compact)
109
+ registry.delete(...)
110
110
  end
111
111
  end
112
112
 
113
+ def delete_all
114
+ @feature_registries.each_value(&:delete_all)
115
+ end
116
+
113
117
  private
114
118
 
115
119
  def define_proxy_call(name)
@@ -18,18 +18,15 @@ module RgGen
18
18
  @loaders.concat(Array(loaders))
19
19
  end
20
20
 
21
- def define_value_extractor(layers_or_value, value = nil, &body)
22
- value, layers = [value, layers_or_value].compact
21
+ def define_value_extractor(layers = nil, value, &body)
23
22
  @extractors << create_extractor(layers, value, &body)
24
23
  end
25
24
 
26
- def ignore_value(layers_or_value, value = nil)
27
- value, layers = [value, layers_or_value].compact
25
+ def ignore_value(layers = nil, value)
28
26
  ignore_values(layers, [value])
29
27
  end
30
28
 
31
- def ignore_values(layers_or_values, values = nil)
32
- values, layers = [values, layers_or_values].compact
29
+ def ignore_values(layers = nil, values)
33
30
  [layers].flatten.each do |layer|
34
31
  (@ignore_values[layer] ||= []).concat(Array(values))
35
32
  end
@@ -7,16 +7,13 @@ module RgGen
7
7
  end
8
8
 
9
9
  module RaiseError
10
+ include InputBase::RaiseError
11
+
10
12
  private
11
13
 
12
14
  def error_exception
13
15
  ConfigurationError
14
16
  end
15
-
16
- def error(message, input_value = nil)
17
- position = input_value.position if input_value.respond_to?(:position)
18
- raise ConfigurationError.new(message, position || @position)
19
- end
20
17
  end
21
18
  end
22
19
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module Core
5
+ module InputBase
6
+ module ConversionUtility
7
+ private
8
+
9
+ def to_int(value, position = nil)
10
+ v, pos =
11
+ if value.is_a?(InputValue)
12
+ [value.value, value.position]
13
+ else
14
+ [value, position]
15
+ end
16
+ Integer(v)
17
+ rescue ArgumentError, TypeError
18
+ message = yield(v)
19
+ error message, pos
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module Core
5
+ module InputBase
6
+ ApproximatelyErrorPosition = Struct.new(:position) do
7
+ def self.create(position)
8
+ position && new(position)
9
+ end
10
+
11
+ def to_s
12
+ "#{position} (approximately)"
13
+ end
14
+ end
15
+
16
+ module RaiseError
17
+ private
18
+
19
+ def error(message, position = nil)
20
+ pos = extract_error_position(position)
21
+ raise error_exception.new(message, pos)
22
+ end
23
+
24
+ def extract_error_position(position)
25
+ pos =
26
+ if position.respond_to?(:position)
27
+ position.position
28
+ else
29
+ position
30
+ end
31
+ pos ||
32
+ if respond_to?(:error_position)
33
+ error_position
34
+ else
35
+ @position
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -6,6 +6,7 @@ module RgGen
6
6
  class Feature < Base::Feature
7
7
  include Utility::RegexpPatterns
8
8
  include Utility::TypeChecker
9
+ include ConversionUtility
9
10
 
10
11
  class << self
11
12
  def property(name, **options, &body)
@@ -29,15 +30,13 @@ module RgGen
29
30
  end
30
31
 
31
32
  def build(&block)
32
- @builders ||= []
33
- @builders << block
33
+ (@builders ||= []) << block
34
34
  end
35
35
 
36
36
  attr_reader :builders
37
37
 
38
38
  def post_build(&block)
39
- @post_builders ||= []
40
- @post_builders << block
39
+ (@post_builders ||= []) << block
41
40
  end
42
41
 
43
42
  attr_reader :post_builders
@@ -64,8 +63,7 @@ module RgGen
64
63
  attr_reader :verifiers
65
64
 
66
65
  def printable(name, &body)
67
- @printables ||= {}
68
- @printables[name] = body
66
+ (@printables ||= {})[name] = body
69
67
  end
70
68
 
71
69
  attr_reader :printables
@@ -119,11 +117,22 @@ module RgGen
119
117
  end
120
118
 
121
119
  def inspect
122
- printable_values =
123
- printables&.map { |name, value| "#{name}: #{value.inspect}" }
120
+ printable_values = printables&.map { |name, value| "#{name}: #{value.inspect}" }
124
121
  (printable_values && "#{super}[#{printable_values.join(', ')}]") || super
125
122
  end
126
123
 
124
+ attr_reader :position
125
+
126
+ def error_position
127
+ if position
128
+ position
129
+ else
130
+ approximate_position =
131
+ component.features.map(&:position).find(&:itself)
132
+ ApproximatelyErrorPosition.create(approximate_position)
133
+ end
134
+ end
135
+
127
136
  private
128
137
 
129
138
  def do_build(args)
@@ -137,16 +146,12 @@ module RgGen
137
146
  self.class.builders.each { |builder| instance_exec(*args, &builder) }
138
147
  end
139
148
 
140
- attr_reader :position
141
-
142
149
  def match_automatically?
143
- matcher = self.class.input_matcher
144
- matcher&.match_automatically?
150
+ self.class.input_matcher&.match_automatically?
145
151
  end
146
152
 
147
153
  def match_pattern(rhs)
148
- matcher = self.class.input_matcher
149
- @match_data, @match_index = matcher&.match(rhs)
154
+ @match_data, @match_index = self.class.input_matcher&.match(rhs)
150
155
  end
151
156
 
152
157
  attr_reader :match_data
@@ -161,8 +166,7 @@ module RgGen
161
166
  end
162
167
 
163
168
  def do_verify(scope)
164
- self.class.verifiers&.[](scope)
165
- &.each { |verifier| verifier.verify(self) }
169
+ self.class.verifiers&.[](scope)&.each { |verifier| verifier.verify(self) }
166
170
  (@verified ||= {})[scope] = true
167
171
  end
168
172
 
@@ -18,8 +18,8 @@ module RgGen
18
18
  @default_value
19
19
  end
20
20
 
21
- def value_format(format = nil)
22
- @value_format = format if format
21
+ def value_format(format = nil, **options)
22
+ @value_format = [format, options] if format
23
23
  @value_format
24
24
  end
25
25
  end
@@ -51,29 +51,31 @@ module RgGen
51
51
  end
52
52
 
53
53
  def process_active_input_value(input_value)
54
- parseed_value, options =
54
+ parsed_value, options =
55
55
  if self.class.value_format
56
56
  parse_input_value(input_value, self.class.value_format)
57
57
  end
58
- override_input_value(input_value, parseed_value, options) || input_value
58
+ override_input_value(input_value, parsed_value, options) || input_value
59
59
  end
60
60
 
61
61
  VALUE_PARSERS = {
62
- value_with_options: ValueWithOptionsParser,
62
+ option_array: OptionArrayParser,
63
+ option_hash: OptionHashParser,
63
64
  hash_list: HashListParser
64
65
  }.freeze
65
66
 
66
67
  def parse_input_value(input_value, value_format)
67
- VALUE_PARSERS[value_format].new(error_exception).parse(input_value)
68
+ format, options = value_format
69
+ VALUE_PARSERS[format].new(error_exception, **options).parse(input_value)
68
70
  end
69
71
 
70
- def override_input_value(input_value, parseed_value, options)
71
- (convert_value(input_value, parseed_value) || parseed_value)
72
+ def override_input_value(input_value, parsed_value, options)
73
+ (convert_value(input_value, parsed_value) || parsed_value)
72
74
  &.then { |v| InputValue.new(v, options, input_value.position) }
73
75
  end
74
76
 
75
- def convert_value(input_value, parseed_value)
76
- value = parseed_value || input_value.value
77
+ def convert_value(input_value, parsed_value)
78
+ value = parsed_value || input_value.value
77
79
  if empty_value?(value)
78
80
  evaluate_defalt_value(input_value.position)
79
81
  else
@@ -21,8 +21,8 @@ module RgGen
21
21
  assign_value(symbolized_name, value, position)
22
22
  end
23
23
 
24
- def []=(value_name, position_or_value, value = nil)
25
- value(value_name, value || position_or_value, position_or_value)
24
+ def []=(value_name, position = nil, value)
25
+ value(value_name, value, position)
26
26
  end
27
27
 
28
28
  def [](value_name)
@@ -6,14 +6,10 @@ module RgGen
6
6
  class InputValue < ::SimpleDelegator
7
7
  NoValue = Object.new
8
8
 
9
- def initialize(value, options_or_position, position = NoValue)
9
+ def initialize(value, options = NoValue, position)
10
10
  super((value.is_a?(String) && value.strip) || value)
11
- @options, @position =
12
- if position.equal?(NoValue)
13
- [NoValue, options_or_position]
14
- else
15
- [options_or_position, position]
16
- end
11
+ @options = options
12
+ @position = position
17
13
  end
18
14
 
19
15
  alias_method :value, :__getobj__
@@ -6,7 +6,7 @@ module RgGen
6
6
  class InputValueParser
7
7
  include Utility::TypeChecker
8
8
 
9
- def initialize(exception)
9
+ def initialize(exception, **_option)
10
10
  @exception = exception
11
11
  end
12
12
 
@@ -16,8 +16,13 @@ module RgGen
16
16
  string&.split(separator, limit)&.map(&:strip)
17
17
  end
18
18
 
19
- def error(message, input_value = nil)
20
- position = input_value.position if input_value.respond_to?(:position)
19
+ def error(message, position_or_input_value = nil)
20
+ position =
21
+ if position_or_input_value.respond_to?(:position)
22
+ position_or_input_value.position
23
+ else
24
+ position_or_input_value
25
+ end
21
26
  raise @exception.new(message, position)
22
27
  end
23
28
  end
@@ -3,7 +3,7 @@
3
3
  module RgGen
4
4
  module Core
5
5
  module InputBase
6
- class ValueWithOptionsParser < InputValueParser
6
+ class OptionArrayParser < InputValueParser
7
7
  def parse(input_value)
8
8
  value, options =
9
9
  if string?(input_value)
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module Core
5
+ module InputBase
6
+ class OptionHashParser < InputValueParser
7
+ def initialize(exception, allowed_options: nil, multiple_values: false)
8
+ super
9
+ @allowed_options = allowed_options
10
+ @multiple_values = multiple_values
11
+ end
12
+
13
+ def parse(input_value)
14
+ values, options = parse_input_value(input_value)
15
+ check_result(values, options, input_value)
16
+ pack_result(values, options)
17
+ end
18
+
19
+ private
20
+
21
+ def parse_input_value(input_value)
22
+ values, options =
23
+ if string?(input_value)
24
+ parse_string_value(input_value)
25
+ elsif array?(input_value)
26
+ parse_array_value(input_value)
27
+ elsif hash?(input_value)
28
+ [nil, input_value]
29
+ elsif !input_value.empty_value?
30
+ [[input_value.value]]
31
+ end
32
+ [values || [], symbolize_keys(options) || {}]
33
+ end
34
+
35
+ def parse_string_value(input_value)
36
+ value_string, option_string = split_string(input_value, ':', 2)
37
+ values = split_string(value_string, /[,\n]/, 0)
38
+ options = parse_option_string(option_string, input_value.position)
39
+ [values, options]
40
+ end
41
+
42
+ def parse_option_string(option_string, position)
43
+ split_string(option_string, /[,\n]/, 0)
44
+ &.to_h { |option| split_string(option, ':', 2) }
45
+ rescue ArgumentError, TypeError
46
+ error "invalid options are given: #{option_string.inspect}", position
47
+ end
48
+
49
+ def parse_array_value(input_value)
50
+ input_value.each_with_object([[], {}]) do |item, (values, options)|
51
+ if value_item?(item, values)
52
+ values << item
53
+ else
54
+ update_option_hash(options, item, input_value.position)
55
+ end
56
+ end
57
+ end
58
+
59
+ def value_item?(item, values)
60
+ !hash?(item) && (@multiple_values || values.empty?)
61
+ end
62
+
63
+ def update_option_hash(option_hash, item, position)
64
+ option_hash.update(item)
65
+ rescue ArgumentError, TypeError
66
+ error "invalid option is given: #{item.inspect}", position
67
+ end
68
+
69
+ def symbolize_keys(options)
70
+ options&.transform_keys { |k| string?(k) && k.to_sym || k }
71
+ end
72
+
73
+ def check_result(values, options, input_value)
74
+ no_values?(values, options) &&
75
+ (error "no input values are given: #{input_value.inspect}", input_value)
76
+ illegal_value_size?(values) &&
77
+ (error "multiple input values are given: #{values}", input_value)
78
+ check_option(options, input_value.position)
79
+ end
80
+
81
+ def no_values?(values, options)
82
+ values.empty? && !options.empty?
83
+ end
84
+
85
+ def illegal_value_size?(values)
86
+ !@multiple_values && values.size > 1
87
+ end
88
+
89
+ def check_option(options, position)
90
+ return if @allowed_options.nil? || @allowed_options.empty?
91
+ return if options.nil? || options.empty?
92
+
93
+ unknown_options = options.keys - @allowed_options
94
+ unknown_options.empty? ||
95
+ (error "unknown options are given: #{unknown_options}", position)
96
+ end
97
+
98
+ def pack_result(values, options)
99
+ [@multiple_values && values || values.first, options]
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -7,16 +7,13 @@ module RgGen
7
7
  end
8
8
 
9
9
  module RaiseError
10
+ include InputBase::RaiseError
11
+
10
12
  private
11
13
 
12
14
  def error_exception
13
15
  RegisterMapError
14
16
  end
15
-
16
- def error(message, input_value = nil)
17
- position = input_value.position if input_value.respond_to?(:position)
18
- raise RegisterMapError.new(message, position || @position)
19
- end
20
17
  end
21
18
  end
22
19
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module RgGen
4
4
  module Core
5
- VERSION = '0.29.0'
5
+ VERSION = '0.30.0'
6
6
  MAJOR, MINOR, PATCH = VERSION.split('.').map(&:to_i)
7
7
  end
8
8
  end
data/lib/rggen/core.rb CHANGED
@@ -42,6 +42,8 @@ require_relative 'core/base/feature_layer_extension'
42
42
  require_relative 'core/input_base/input_value'
43
43
  require_relative 'core/input_base/input_data'
44
44
  require_relative 'core/input_base/input_value_extractor'
45
+ require_relative 'core/input_base/error'
46
+ require_relative 'core/input_base/conversion_utility'
45
47
  require_relative 'core/input_base/loader'
46
48
  require_relative 'core/input_base/json_loader'
47
49
  require_relative 'core/input_base/toml_loader'
@@ -53,7 +55,8 @@ require_relative 'core/input_base/verifier'
53
55
  require_relative 'core/input_base/property'
54
56
  require_relative 'core/input_base/feature'
55
57
  require_relative 'core/input_base/input_vaue_parser'
56
- require_relative 'core/input_base/value_with_options_parser'
58
+ require_relative 'core/input_base/option_array_parser'
59
+ require_relative 'core/input_base/option_hash_parser'
57
60
  require_relative 'core/input_base/hash_list_parser'
58
61
  require_relative 'core/input_base/feature_factory'
59
62
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rggen-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.29.0
4
+ version: 0.30.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taichi Ishitani
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-01-02 00:00:00.000000000 Z
11
+ date: 2023-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docile
@@ -144,6 +144,8 @@ files:
144
144
  - lib/rggen/core/generator.rb
145
145
  - lib/rggen/core/input_base/component.rb
146
146
  - lib/rggen/core/input_base/component_factory.rb
147
+ - lib/rggen/core/input_base/conversion_utility.rb
148
+ - lib/rggen/core/input_base/error.rb
147
149
  - lib/rggen/core/input_base/feature.rb
148
150
  - lib/rggen/core/input_base/feature_factory.rb
149
151
  - lib/rggen/core/input_base/hash_list_parser.rb
@@ -154,9 +156,10 @@ files:
154
156
  - lib/rggen/core/input_base/input_vaue_parser.rb
155
157
  - lib/rggen/core/input_base/json_loader.rb
156
158
  - lib/rggen/core/input_base/loader.rb
159
+ - lib/rggen/core/input_base/option_array_parser.rb
160
+ - lib/rggen/core/input_base/option_hash_parser.rb
157
161
  - lib/rggen/core/input_base/property.rb
158
162
  - lib/rggen/core/input_base/toml_loader.rb
159
- - lib/rggen/core/input_base/value_with_options_parser.rb
160
163
  - lib/rggen/core/input_base/verifier.rb
161
164
  - lib/rggen/core/input_base/yaml_loader.rb
162
165
  - lib/rggen/core/options.rb
@@ -219,8 +222,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
222
  - !ruby/object:Gem::Version
220
223
  version: '0'
221
224
  requirements: []
222
- rubygems_version: 3.4.1
225
+ rubygems_version: 3.4.10
223
226
  signing_key:
224
227
  specification_version: 4
225
- summary: rggen-core-0.29.0
228
+ summary: rggen-core-0.30.0
226
229
  test_files: []