rggen-core 0.27.0 → 0.28.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 87457243bd057e719ca35386df5b23f6c0cc72a86029e93998b51d8436f0b184
4
- data.tar.gz: 96a4fd969fd9399e3a1bffdbc9319315bcd9a4df1f45827eb31517eb942bb489
3
+ metadata.gz: 30286c1aefc400e1d6cda52dddc797fca2ad3a35fcf3a43578371a5b9a8fc4b0
4
+ data.tar.gz: e7bb012b70a143c1412eab481febce79e7083ab83c22136f3e52540a41e67c69
5
5
  SHA512:
6
- metadata.gz: ab5f4642ecd2d47c88dfffe5432733c7c91320dace927c84d537cdc12be2e54ab70cf9e4ee32f47bd03f7ec044090f13e1b4a4efab6fa1f2549f6e99d3f05d5d
7
- data.tar.gz: 1faf35891545834a2ba9df954b15d818c65ea6910859c4987c8e587d495dcfd167fbbb0a5a9206d060fca16c817a004474d721298ac0f6fa9eecbef2dccb04dc
6
+ metadata.gz: a14bd1ce40c782d0628e55f23a6acffa1578598a421bae4a7127c4464800078ac3181504e0a53b829581cfba4820b56fbc07803cf3fe338097479f4d3aca646a
7
+ data.tar.gz: 524360603dae2839afe73e23a8c2cbedac3e2373e70a24e671d006977caf08dea6a5813d50a99a1724b3ab1a098b38134b0f653cbde2f9c6ee341d12596a73d0
@@ -47,7 +47,7 @@ module RgGen
47
47
  def find_gemspec_by_path(path)
48
48
  Gem::Specification
49
49
  .each.find { |spec| match_gemspec_path?(spec, path) }
50
- .yield_self { |spec| spec && [spec.name, spec.version] }
50
+ .then { |spec| spec && [spec.name, spec.version] }
51
51
  end
52
52
 
53
53
  def match_gemspec_path?(gemspec, path)
@@ -88,8 +88,8 @@ module RgGen
88
88
  end
89
89
 
90
90
  def activate_plugins
91
- @plugins.each { |plugin| plugin.activate(@builder) }
92
- @plugins.each { |plugin| plugin.activate_additionally(@builder) }
91
+ do_normal_activation
92
+ do_addtional_activation
93
93
  end
94
94
 
95
95
  def activate_plugin_by_name(plugin_name)
@@ -136,6 +136,14 @@ module RgGen
136
136
  &.reject(&:blank?)
137
137
  &.map { |entry| entry.split(',', 2) }
138
138
  end
139
+
140
+ def do_normal_activation
141
+ @plugins.each { |plugin| plugin.activate(@builder) }
142
+ end
143
+
144
+ def do_addtional_activation
145
+ @plugins.each { |plugin| plugin.activate_additionally(@builder) }
146
+ end
139
147
  end
140
148
  end
141
149
  end
@@ -7,7 +7,8 @@ module RgGen
7
7
  end
8
8
 
9
9
  module RaiseError
10
- def error(message, position = nil)
10
+ def error(message, input_value = nil)
11
+ position = input_value.position if input_value.respond_to?(:position)
11
12
  raise ConfigurationError.new(message, position || @position)
12
13
  end
13
14
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kernel
4
+ alias_method :__orignal_Integer, :Integer
5
+
6
+ def Integer(arg, base = 0, exception: true)
7
+ arg = arg.__getobj__ if arg.is_a?(::Delegator)
8
+ __orignal_Integer(arg, base, exception: exception)
9
+ end
10
+
11
+ module_function :__orignal_Integer
12
+ module_function :Integer
13
+ end
@@ -4,8 +4,8 @@ class Object
4
4
  def export_instance_variable(variable, to)
5
5
  instance_variable_defined?(variable) &&
6
6
  instance_variable_get(variable)
7
- .yield_self { |v| block_given? ? yield(v) : v }
8
- .yield_self { |v| to.instance_variable_set(variable, v) }
7
+ .then { |v| block_given? ? yield(v) : v }
8
+ .then { |v| to.instance_variable_set(variable, v) }
9
9
  end
10
10
 
11
11
  def singleton_exec(*args, &block)
@@ -5,6 +5,7 @@ module RgGen
5
5
  module InputBase
6
6
  class Feature < Base::Feature
7
7
  include Utility::RegexpPatterns
8
+ include Utility::TypeChecker
8
9
 
9
10
  class << self
10
11
  def property(name, **options, &body)
@@ -50,8 +51,7 @@ module RgGen
50
51
  end
51
52
 
52
53
  def input_pattern(pattern_or_patterns, **options, &converter)
53
- @input_matcher =
54
- InputMatcher.new(pattern_or_patterns, **options, &converter)
54
+ @input_matcher = InputMatcher.new(pattern_or_patterns, **options, &converter)
55
55
  end
56
56
 
57
57
  attr_reader :input_matcher
@@ -88,9 +88,8 @@ module RgGen
88
88
  end
89
89
 
90
90
  def export_verifiers(subclass)
91
- subclass.instance_variable_set(
92
- :@verifiers, @verifiers.transform_values(&:dup)
93
- )
91
+ subclass
92
+ .instance_variable_set(:@verifiers, @verifiers.transform_values(&:dup))
94
93
  end
95
94
  end
96
95
 
@@ -104,9 +103,7 @@ module RgGen
104
103
  end
105
104
 
106
105
  def post_build
107
- self.class.post_builders&.each do |post_builder|
108
- instance_exec(&post_builder)
109
- end
106
+ self.class.post_builders&.each { |block| instance_exec(&block) }
110
107
  end
111
108
 
112
109
  def verify(scope)
@@ -114,9 +111,7 @@ module RgGen
114
111
  end
115
112
 
116
113
  def printables
117
- helper
118
- .printables
119
- &.map { |name, body| [name, printable(name, &body)] }
114
+ helper.printables&.map { |name, body| [name, printable(name, &body)] }
120
115
  end
121
116
 
122
117
  def printable?
@@ -133,10 +128,13 @@ module RgGen
133
128
 
134
129
  def do_build(args)
135
130
  @position = args.last.position
136
- value = args.last.value
137
- match_automatically? && match_pattern(value)
138
- Array(self.class.builders)
139
- .each { |builder| instance_exec(*args[0..-2], value, &builder) }
131
+ match_automatically? && match_pattern(args.last)
132
+ execute_build_blocks(args)
133
+ end
134
+
135
+ def execute_build_blocks(args)
136
+ args = [*args, args.last.options] if args.last.with_options?
137
+ self.class.builders.each { |builder| instance_exec(*args, &builder) }
140
138
  end
141
139
 
142
140
  attr_reader :position
@@ -4,6 +4,8 @@ module RgGen
4
4
  module Core
5
5
  module InputBase
6
6
  class FeatureFactory < Base::FeatureFactory
7
+ include Utility::TypeChecker
8
+
7
9
  class << self
8
10
  def convert_value(&block)
9
11
  @value_converter = block
@@ -15,10 +17,18 @@ module RgGen
15
17
  @default_value = block if block_given?
16
18
  @default_value
17
19
  end
20
+
21
+ def allow_options
22
+ @allow_options = true
23
+ end
24
+
25
+ def allow_options?
26
+ @allow_options || false
27
+ end
18
28
  end
19
29
 
20
30
  def create(component, *args)
21
- input_value = preprocess(args.last)
31
+ input_value = process_input_value(args.last)
22
32
  create_feature(component, *args[0..-2], input_value) do |feature|
23
33
  build_feature(feature, input_value)
24
34
  feature.verify(:feature)
@@ -35,20 +45,60 @@ module RgGen
35
45
 
36
46
  private
37
47
 
38
- def preprocess(input_value)
39
- converted_value =
40
- active_feature_factory? && convert_value(input_value)
41
- converted_value || input_value
48
+ def process_input_value(input_value)
49
+ if passive_feature_factory?
50
+ input_value
51
+ elsif self.class.allow_options?
52
+ process_input_value_with_options(input_value)
53
+ else
54
+ process_input_value_without_options(input_value)
55
+ end
42
56
  end
43
57
 
44
- def convert_value(input_value)
45
- new_value =
46
- if input_value.empty_value?
47
- evaluate_defalt_value(input_value.position)
58
+ def process_input_value_with_options(input_value)
59
+ value, options =
60
+ if string?(input_value)
61
+ parse_string_value(input_value)
48
62
  else
49
- convert(input_value.value, input_value.position)
63
+ Array(input_value).then { |values| [values.first, values[1..]] }
50
64
  end
51
- new_value && InputValue.new(new_value, input_value.position)
65
+ value = convert_value(value, input_value.position) || value
66
+ InputValue.new(value, options || [], input_value.position)
67
+ end
68
+
69
+ def parse_string_value(input_value)
70
+ value, options = split_string(input_value, ':', 2)
71
+ [value, parse_option_string(options)]
72
+ end
73
+
74
+ def parse_option_string(option_string)
75
+ split_string(option_string, /[,\n]/, 0)&.map do |option|
76
+ name, value = split_string(option, ':', 2)
77
+ value && [name, value] || name
78
+ end
79
+ end
80
+
81
+ def split_string(string, separator, limit)
82
+ string&.split(separator, limit)&.map(&:strip)
83
+ end
84
+
85
+ def process_input_value_without_options(input_value)
86
+ value = convert_value(input_value.value, input_value.position)
87
+ value && InputValue.new(value, input_value.position) || input_value
88
+ end
89
+
90
+ def convert_value(value, position)
91
+ if empty_value?(value)
92
+ evaluate_defalt_value(position)
93
+ else
94
+ convert(value, position)
95
+ end
96
+ end
97
+
98
+ def empty_value?(value)
99
+ return true if value.nil?
100
+ return value.empty? if value.respond_to?(:empty?)
101
+ false
52
102
  end
53
103
 
54
104
  def evaluate_defalt_value(position)
@@ -13,8 +13,8 @@ module RgGen
13
13
  def match(rhs)
14
14
  rhs
15
15
  .to_s
16
- .yield_self { |s| ignore_blanks? && delete_blanks(s) || s }
17
- .yield_self(&method(:match_patterns))
16
+ .then { |s| ignore_blanks? && delete_blanks(s) || s }
17
+ .then(&method(:match_patterns))
18
18
  end
19
19
 
20
20
  def match_automatically?
@@ -3,21 +3,43 @@
3
3
  module RgGen
4
4
  module Core
5
5
  module InputBase
6
- class InputValue
7
- def initialize(value, position)
8
- @value = (value.is_a?(String) && value.strip) || value
9
- @position = position
6
+ class InputValue < ::SimpleDelegator
7
+ NoValue = Object.new
8
+
9
+ def initialize(value, options_or_position, position = NoValue)
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
10
17
  end
11
18
 
12
- attr_accessor :value
19
+ alias_method :value, :__getobj__
20
+
13
21
  attr_reader :position
22
+ attr_reader :options
23
+
24
+ def ==(other)
25
+ __getobj__ == other ||
26
+ other.is_a?(InputValue) && __getobj__ == other.__getobj__
27
+ end
28
+
29
+ def match_class?(klass)
30
+ __getobj__.is_a?(klass)
31
+ end
14
32
 
15
33
  def empty_value?
16
- return true if @value.nil?
17
- return true if @value.respond_to?(:empty?) && @value.empty?
34
+ return true if value.nil?
35
+ return true if value.respond_to?(:empty?) && value.empty?
18
36
  false
19
37
  end
20
38
 
39
+ def with_options?
40
+ !@options.equal?(NoValue)
41
+ end
42
+
21
43
  def available?
22
44
  true
23
45
  end
@@ -80,8 +80,8 @@ module RgGen
80
80
 
81
81
  def set_initial_value(feature, varible_name)
82
82
  @options[:initial]
83
- .yield_self { |v| evaluate_default_initial_value(feature, v) }
84
- .yield_self { |v| feature.instance_variable_set(varible_name, v) }
83
+ .then { |v| evaluate_default_initial_value(feature, v) }
84
+ .then { |v| feature.instance_variable_set(varible_name, v) }
85
85
  end
86
86
 
87
87
  def evaluate_default_initial_value(feature, value)
@@ -4,15 +4,111 @@ module RgGen
4
4
  module Core
5
5
  module InputBase
6
6
  module YAMLLoader
7
+ Position = Struct.new(:file, :line, :column) do
8
+ def to_s
9
+ "file #{file} line #{line} column #{column}"
10
+ end
11
+ end
12
+
13
+ module PsychExtension
14
+ refine ::Psych::Nodes::Node do
15
+ attr_accessor :filename
16
+
17
+ attr_writer :mapping_key
18
+
19
+ def mapping_key?
20
+ @mapping_key || false
21
+ end
22
+ end
23
+ end
24
+
25
+ using PsychExtension
26
+
27
+ class TreeBuilder < ::Psych::TreeBuilder
28
+ def initialize(filename)
29
+ super()
30
+ @filename = filename
31
+ end
32
+
33
+ def set_start_location(node)
34
+ super
35
+ node.filename = @filename
36
+ end
37
+
38
+ def scalar(value, anchor, tag, plain, quated, style)
39
+ node = super
40
+ node.mapping_key = mapping_key?
41
+ node
42
+ end
43
+
44
+ private
45
+
46
+ def mapping_key?
47
+ @last.mapping? && @last.children.size.odd?
48
+ end
49
+ end
50
+
51
+ class Visitor < ::Psych::Visitors::ToRuby
52
+ if ::Psych::VERSION >= '3.2.0'
53
+ def initialize(scalar_scanner, class_loader)
54
+ super(scalar_scanner, class_loader, symbolize_names: true)
55
+ end
56
+ end
57
+
58
+ def register(node, object)
59
+ obj =
60
+ if override_object?(node)
61
+ file = node.filename
62
+ line = node.start_line + 1
63
+ column = node.start_column + 1
64
+ position = Position.new(file, line, column)
65
+ InputValue.new(object, position)
66
+ else
67
+ object
68
+ end
69
+ super(node, obj)
70
+ end
71
+
72
+ private
73
+
74
+ def override_object?(node)
75
+ node.mapping? || node.sequence? || (node.scalar? && !node.mapping_key?)
76
+ end
77
+ end
78
+
7
79
  private
8
80
 
9
81
  def load_yaml(file)
10
- yaml = File.binread(file)
11
- YAML.safe_load(
12
- yaml,
13
- permitted_classes: [Symbol], aliases: true,
14
- filename: file, symbolize_names: true
15
- )
82
+ parse_yaml(File.binread(file), file)
83
+ .then { |result| to_ruby(result) }
84
+ .then { |result| symbolize_names(result) }
85
+ end
86
+
87
+ def parse_yaml(yaml, file)
88
+ parser = ::Psych::Parser.new(TreeBuilder.new(file))
89
+ parser.parse(yaml, file)
90
+ parser.handler.root.children.first
91
+ end
92
+
93
+ def to_ruby(result)
94
+ cl = ::Psych::ClassLoader::Restricted.new(['Symbol'], [])
95
+ ss = ::Psych::ScalarScanner.new(cl)
96
+ Visitor.new(ss, cl).accept(result)
97
+ end
98
+
99
+ def symbolize_names(result)
100
+ return result if ::Psych::VERSION >= '3.2.0'
101
+
102
+ if result.match_class?(Hash)
103
+ keys = result.keys
104
+ keys.each do |key|
105
+ result[key.to_sym] = symbolize_names(result.delete(key))
106
+ end
107
+ elsif result.match_class?(Array)
108
+ result.map! { |value| symbolize_names(value) }
109
+ end
110
+
111
+ result
16
112
  end
17
113
  end
18
114
  end
@@ -119,7 +119,7 @@ module RgGen
119
119
  Options.add_option(:configuration) do |option|
120
120
  option.short_option '-c'
121
121
  option.long_option '--configuration FILE'
122
- option.default { ENV['RGGEN_DEFAULT_CONFIGURATION_FILE'] }
122
+ option.default { ENV.fetch('RGGEN_DEFAULT_CONFIGURATION_FILE', nil) }
123
123
  option.description 'Specify a configuration file'
124
124
  end
125
125
 
@@ -7,7 +7,8 @@ module RgGen
7
7
  end
8
8
 
9
9
  module RaiseError
10
- def error(message, position = nil)
10
+ def error(message, input_value = nil)
11
+ position = input_value.position if input_value.respond_to?(:position)
11
12
  raise RegisterMapError.new(message, position || @position)
12
13
  end
13
14
  end
@@ -4,6 +4,8 @@ module RgGen
4
4
  module Core
5
5
  module RegisterMap
6
6
  module HashLoader
7
+ include Utility::TypeChecker
8
+
7
9
  private
8
10
 
9
11
  SUB_LAYER_KEYS = {
@@ -21,7 +23,7 @@ module RgGen
21
23
  }.freeze
22
24
 
23
25
  def format_layer_data(read_data, layer, file)
24
- if read_data.is_a?(Array)
26
+ if array?(read_data)
25
27
  format_array_layer_data(read_data, layer, file)
26
28
  else
27
29
  fomrat_hash_layer_data(read_data, layer, file)
@@ -39,7 +41,7 @@ module RgGen
39
41
  end
40
42
 
41
43
  def format_sub_layer_data(read_data, layer, file)
42
- if read_data.is_a?(Array)
44
+ if array?(read_data)
43
45
  format_array_sub_layer_data(read_data, layer, file)
44
46
  else
45
47
  format_hash_sub_layer_data(read_data, layer, file)
@@ -32,6 +32,18 @@ module RgGen
32
32
  def integer
33
33
  INTEGER_PATTERN
34
34
  end
35
+
36
+ TRUTHY_PATTERN = /true|on|yes/i.freeze
37
+
38
+ def truthy_pattern
39
+ TRUTHY_PATTERN
40
+ end
41
+
42
+ FALSEY_PATTERN = /false|off|no/i.freeze
43
+
44
+ def falsey_pattern
45
+ FALSEY_PATTERN
46
+ end
35
47
  end
36
48
  end
37
49
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module Core
5
+ module Utility
6
+ module TypeChecker
7
+ [String, Symbol, Integer, Array, Hash].each do |klass|
8
+ module_eval(<<~DEFINE_METHOD, __FILE__, __LINE__ + 1)
9
+ # module_function def string?(value)
10
+ # return value.match_class?(String) if value.respond_to?(:match_class?)
11
+ # value.is_a?(String)
12
+ # end
13
+ module_function def #{klass.to_s.downcase}?(value)
14
+ return value.match_class?(#{klass}) if value.respond_to?(:match_class?)
15
+ value.is_a?(#{klass})
16
+ end
17
+ DEFINE_METHOD
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module RgGen
4
4
  module Core
5
- VERSION = '0.27.0'
5
+ VERSION = '0.28.0'
6
6
  MAJOR, MINOR, PATCH = VERSION.split('.').map(&:to_i)
7
7
  end
8
8
  end
data/lib/rggen/core.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'delegate'
3
4
  require 'docile'
4
5
  require 'erubi'
5
6
  require 'fileutils'
@@ -14,6 +15,7 @@ require 'yaml'
14
15
  require_relative 'core/version'
15
16
 
16
17
  require_relative 'core/facets'
18
+ require_relative 'core/core_extensions/kernel'
17
19
  require_relative 'core/core_extensions/object'
18
20
 
19
21
  require_relative 'core/utility/attribute_setter'
@@ -24,6 +26,7 @@ require_relative 'core/utility/code_utility/structure_definition'
24
26
  require_relative 'core/utility/code_utility'
25
27
  require_relative 'core/utility/error_utility'
26
28
  require_relative 'core/utility/regexp_patterns'
29
+ require_relative 'core/utility/type_checker'
27
30
 
28
31
  require_relative 'core/exceptions'
29
32
 
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.27.0
4
+ version: 0.28.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: 2022-07-05 00:00:00.000000000 Z
11
+ date: 2022-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docile
@@ -134,6 +134,7 @@ files:
134
134
  - lib/rggen/core/configuration/ruby_loader.rb
135
135
  - lib/rggen/core/configuration/toml_loader.rb
136
136
  - lib/rggen/core/configuration/yaml_loader.rb
137
+ - lib/rggen/core/core_extensions/kernel.rb
137
138
  - lib/rggen/core/core_extensions/object.rb
138
139
  - lib/rggen/core/dsl.rb
139
140
  - lib/rggen/core/exceptions.rb
@@ -187,6 +188,7 @@ files:
187
188
  - lib/rggen/core/utility/code_utility/structure_definition.rb
188
189
  - lib/rggen/core/utility/error_utility.rb
189
190
  - lib/rggen/core/utility/regexp_patterns.rb
191
+ - lib/rggen/core/utility/type_checker.rb
190
192
  - lib/rggen/core/version.rb
191
193
  homepage: https://github.com/rggen/rggen-core
192
194
  licenses:
@@ -212,8 +214,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
212
214
  - !ruby/object:Gem::Version
213
215
  version: '0'
214
216
  requirements: []
215
- rubygems_version: 3.3.3
217
+ rubygems_version: 3.3.7
216
218
  signing_key:
217
219
  specification_version: 4
218
- summary: rggen-core-0.27.0
220
+ summary: rggen-core-0.28.0
219
221
  test_files: []