rggen-core 0.26.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: fe3a65999b49b800216dea110ca47e81c71cf4fe0a65626f50526be374647713
4
- data.tar.gz: 160dbda800f4fef46bbe18ef34810851082d09271eb7754b6b4508eb4f56c8fd
3
+ metadata.gz: 30286c1aefc400e1d6cda52dddc797fca2ad3a35fcf3a43578371a5b9a8fc4b0
4
+ data.tar.gz: e7bb012b70a143c1412eab481febce79e7083ab83c22136f3e52540a41e67c69
5
5
  SHA512:
6
- metadata.gz: eae55af6cdaa6eff67f15ef508dcd32af950d72f18621d0b8769011957c9c6a72c9f3c41557d197a0c1e9fec25ecaff34364f3518cd04c12f22bdb6ee8efa6af
7
- data.tar.gz: 3d030a72ade1067dbb7450ff30ef4cffc3d97430e73a4ea96f9fb310a5cb633e314f1a9b280d4c4e615b1fce2ace5f5456f2881bd76a18fa05cef60420989ada
6
+ metadata.gz: a14bd1ce40c782d0628e55f23a6acffa1578598a421bae4a7127c4464800078ac3181504e0a53b829581cfba4820b56fbc07803cf3fe338097479f4d3aca646a
7
+ data.tar.gz: 524360603dae2839afe73e23a8c2cbedac3e2373e70a24e671d006977caf08dea6a5813d50a99a1724b3ab1a098b38134b0f653cbde2f9c6ee341d12596a73d0
data/README.md CHANGED
@@ -37,7 +37,8 @@ $ gem install rggen-core
37
37
 
38
38
  Feedbacks, bug reports, questions and etc. are wellcome! You can post them by using following ways:
39
39
 
40
- * [GitHub Issue Tracker](https://github.com/rggen/rggen-core/issues)
40
+ * [GitHub Issue Tracker](https://github.com/rggen/rggen/issues)
41
+ * [GitHub Discussions](https://github.com/rggen/rggen/discussions)
41
42
  * [Chat Room](https://gitter.im/rggen/rggen)
42
43
  * [Mailing List](https://groups.google.com/d/forum/rggen)
43
44
  * [Mail](mailto:rggen@googlegroups.com)
@@ -24,11 +24,10 @@ module RgGen
24
24
 
25
25
  [
26
26
  :register_loader, :register_loaders,
27
- :define_value_extractor, :ignore_value, :ignore_values
27
+ :setup_loader, :define_value_extractor
28
28
  ].each do |method_name|
29
29
  define_method(method_name) do |component, *args, &block|
30
- @component_registries[:input][component]
31
- .__send__(__method__, *args, &block)
30
+ @component_registries[:input][component].__send__(__method__, *args, &block)
32
31
  end
33
32
  end
34
33
 
@@ -61,12 +60,8 @@ module RgGen
61
60
  @layers[layer].enable(*args)
62
61
  end
63
62
 
64
- def disable_all
65
- @layers.each_value(&:disable)
66
- end
67
-
68
- def disable(layer, *args)
69
- @layers.key?(layer) && @layers[layer].disable(*args)
63
+ def enable_all
64
+ @layers.each_value(&:enable_all)
70
65
  end
71
66
 
72
67
  def build_factory(type, component)
@@ -94,7 +89,7 @@ module RgGen
94
89
 
95
90
  def_delegator :plugin_manager, :load_plugin
96
91
  def_delegator :plugin_manager, :load_plugins
97
- def_delegator :plugin_manager, :register_plugin
92
+ def_delegator :plugin_manager, :setup_plugin
98
93
 
99
94
  private
100
95
 
@@ -28,68 +28,61 @@ module RgGen
28
28
 
29
29
  def enable(feature_or_list_names, feature_names = nil)
30
30
  if feature_names
31
- @enabled_features[feature_or_list_names]
32
- &.merge!(Array(feature_names))
31
+ list_name = feature_or_list_names
32
+ (@enabled_features[list_name] ||= []).merge!(Array(feature_names))
33
33
  else
34
34
  Array(feature_or_list_names).each do |name|
35
- @enabled_features[name] ||= []
35
+ @enabled_features.key?(name) || (@enabled_features[name] = nil)
36
36
  end
37
37
  end
38
38
  end
39
39
 
40
- def enabled_features(list_name = nil)
41
- if list_name
42
- @enabled_features[list_name] || []
43
- else
44
- @enabled_features.keys
45
- end
46
- end
47
-
48
- def disable(feature_or_list_names = nil, feature_names = nil)
49
- if feature_names
50
- @enabled_features[feature_or_list_names]
51
- &.delete_if { |key, _| Array(feature_names).include?(key) }
52
- elsif feature_or_list_names
53
- Array(feature_or_list_names)
54
- .each(&@enabled_features.method(:delete))
55
- else
56
- @enabled_features.clear
57
- end
40
+ def enable_all
41
+ @enabled_features.clear
58
42
  end
59
43
 
60
44
  def delete(feature_or_list_names = nil, feature_names = nil)
61
45
  if feature_names
62
46
  @feature_entries[feature_or_list_names]&.delete(feature_names)
63
47
  elsif feature_or_list_names
64
- Array(feature_or_list_names)
65
- .each(&@feature_entries.method(:delete))
48
+ Array(feature_or_list_names).each(&@feature_entries.method(:delete))
66
49
  else
67
50
  @feature_entries.clear
68
51
  end
69
52
  end
70
53
 
71
54
  def simple_feature?(feature_name)
72
- enabled_feature?(feature_name, :simple)
55
+ @feature_entries[feature_name]&.match_entry_type?(:simple) || false
73
56
  end
74
57
 
75
58
  def list_feature?(list_name, feature_name = nil)
76
- return false unless enabled_feature?(list_name, :list)
59
+ return false unless @feature_entries[list_name]&.match_entry_type?(:list)
77
60
  return true unless feature_name
78
- enabled_list_item_feature?(list_name, feature_name)
61
+ @feature_entries[list_name].feature?(feature_name)
79
62
  end
80
63
 
81
64
  def feature?(feature_or_list_name, feature_name = nil)
82
65
  if feature_name
83
66
  list_feature?(feature_or_list_name, feature_name)
84
67
  else
85
- simple_feature?(feature_or_list_name) ||
86
- list_feature?(feature_or_list_name)
68
+ simple_feature?(feature_or_list_name) || list_feature?(feature_or_list_name)
69
+ end
70
+ end
71
+
72
+ def enabled_features(list_name = nil)
73
+ if list_name
74
+ enabled_list_features(list_name)
75
+ else
76
+ @enabled_features.empty? && @feature_entries.keys ||
77
+ @enabled_features.keys & @feature_entries.keys
87
78
  end
88
79
  end
89
80
 
90
81
  def build_factories
82
+ target_features =
83
+ (@enabled_features.empty? && @feature_entries || @enabled_features).keys
91
84
  @feature_entries
92
- .slice(*@enabled_features.keys)
85
+ .slice(*target_features)
93
86
  .transform_values(&method(:build_factory))
94
87
  end
95
88
 
@@ -105,16 +98,17 @@ module RgGen
105
98
  @feature_entries[name] = entry
106
99
  end
107
100
 
108
- def enabled_feature?(name, entry_type)
109
- return false unless @feature_entries[name]
110
- &.match_entry_type?(entry_type)
111
- @enabled_features.key?(name)
101
+ def enabled_list_features(list_name)
102
+ return [] unless enabled_list?(list_name)
103
+ features = @feature_entries[list_name].features
104
+ (@enabled_features[list_name] || features) & features
112
105
  end
113
106
 
114
- def enabled_list_item_feature?(list_name, feature_name)
115
- return false unless @feature_entries[list_name]
116
- .feature?(feature_name)
117
- @enabled_features[list_name].include?(feature_name)
107
+ def enabled_list?(list_name)
108
+ return false unless list_feature?(list_name)
109
+ return true if @enabled_features.empty?
110
+ return true if @enabled_features.key?(list_name)
111
+ false
118
112
  end
119
113
 
120
114
  def build_factory(entry)
@@ -6,45 +6,26 @@ module RgGen
6
6
  class InputComponentRegistry < ComponentRegistry
7
7
  def initialize(name, builder)
8
8
  super
9
- @loader_registries = {}
9
+ @loader_registries = Hash.new do |h, k|
10
+ h[k] = LoaderRegistry.new
11
+ end
10
12
  end
11
13
 
12
14
  def register_loader(loader_type, loader)
13
- loader_registry(loader_type).register_loader(loader)
15
+ @loader_registries[loader_type].register_loader(loader)
14
16
  end
15
17
 
16
18
  def register_loaders(loader_type, loaders)
17
- loader_registry(loader_type).register_loaders(loaders)
19
+ @loader_registries[loader_type].register_loaders(loaders)
18
20
  end
19
21
 
20
- def define_value_extractor(loader_type, layers_or_value, value = nil, &body)
21
- layers, value =
22
- if value
23
- [layers_or_value, value]
24
- else
25
- [nil, layers_or_value]
26
- end
27
- loader_registry(loader_type).define_value_extractor(layers, value, &body)
28
- end
29
-
30
- def ignore_value(loader_type, layers_or_value, value = nil)
31
- layers, value =
32
- if value
33
- [layers_or_value, value]
34
- else
35
- [nil, layers_or_value]
36
- end
37
- loader_registry(loader_type).ignore_value(layers, value)
22
+ def setup_loader(loader_type)
23
+ block_given? && yield(@loader_registries[loader_type])
38
24
  end
39
25
 
40
- def ignore_values(loader_type, layers_or_values, values = nil)
41
- layers, values =
42
- if values
43
- [layers_or_values, values]
44
- else
45
- [nil, layers_or_values]
46
- end
47
- loader_registry(loader_type).ignore_values(layers, values)
26
+ def define_value_extractor(loader_type, layers_or_value, value = nil, &body)
27
+ @loader_registries[loader_type]
28
+ .define_value_extractor(layers_or_value, value, &body)
48
29
  end
49
30
 
50
31
  def build_factory
@@ -55,10 +36,6 @@ module RgGen
55
36
 
56
37
  private
57
38
 
58
- def loader_registry(loader_type)
59
- @loader_registries[loader_type] ||= LoaderRegistry.new
60
- end
61
-
62
39
  def build_loaders
63
40
  @loader_registries.values.flat_map(&:create_loaders)
64
41
  end
@@ -90,10 +90,8 @@ module RgGen
90
90
  end
91
91
  end
92
92
 
93
- def disable(feature_or_list_names = nil, feature_names = nil)
94
- @feature_registries.each_value do |registry|
95
- registry.disable(*[feature_or_list_names, feature_names].compact)
96
- end
93
+ def enable_all
94
+ @feature_registries.each_value(&:enable_all)
97
95
  end
98
96
 
99
97
  def delete(feature_or_list_names = nil, feature_names = nil)
@@ -32,9 +32,9 @@ module RgGen
32
32
 
33
33
  alias_method :factory, :define_factory
34
34
 
35
- def build_factory(enabled_features)
35
+ def build_factory(targets)
36
36
  @factory.new(@name) do |f|
37
- f.target_features(target_features(enabled_features))
37
+ f.target_features(target_features(targets))
38
38
  f.target_feature(@default_feature)
39
39
  end
40
40
  end
@@ -77,6 +77,10 @@ module RgGen
77
77
  @features.key?(feature)
78
78
  end
79
79
 
80
+ def features
81
+ @features.keys
82
+ end
83
+
80
84
  private
81
85
 
82
86
  def attach_shared_context(context)
@@ -85,8 +89,8 @@ module RgGen
85
89
  end
86
90
  end
87
91
 
88
- def target_features(enabled_features)
89
- @features.slice(*enabled_features)
92
+ def target_features(targets)
93
+ targets && @features.slice(*targets) || @features
90
94
  end
91
95
  end
92
96
  end
@@ -11,22 +11,25 @@ module RgGen
11
11
  end
12
12
 
13
13
  def register_loader(loader)
14
- register_loaders([loader])
14
+ @loaders << loader
15
15
  end
16
16
 
17
17
  def register_loaders(loaders)
18
18
  @loaders.concat(Array(loaders))
19
19
  end
20
20
 
21
- def define_value_extractor(layers, value, &body)
21
+ def define_value_extractor(layers_or_value, value = nil, &body)
22
+ value, layers = [value, layers_or_value].compact
22
23
  @extractors << create_extractor(layers, value, &body)
23
24
  end
24
25
 
25
- def ignore_value(layers, value)
26
+ def ignore_value(layers_or_value, value = nil)
27
+ value, layers = [value, layers_or_value].compact
26
28
  ignore_values(layers, [value])
27
29
  end
28
30
 
29
- def ignore_values(layers, values)
31
+ def ignore_values(layers_or_values, values = nil)
32
+ values, layers = [values, layers_or_values].compact
30
33
  [layers].flatten.each do |layer|
31
34
  (@ignore_values[layer] ||= []).concat(Array(values))
32
35
  end
@@ -3,81 +3,65 @@
3
3
  module RgGen
4
4
  module Core
5
5
  module Builder
6
- class PluginRegistry
7
- def initialize(plugin_module, &block)
8
- @plugin_module = plugin_module
9
- @block = block
10
- end
11
-
12
- def default_setup(builder)
13
- @plugin_module.plugin_spec.activate(builder)
14
- end
15
-
16
- def optional_setup(builder)
17
- @block && @plugin_module.instance_exec(builder, &@block)
18
- end
19
-
20
- def version_info
21
- @plugin_module.plugin_spec.version_info
22
- end
23
- end
6
+ DEFAULT_PLUGSINS = 'rggen/default'
24
7
 
25
8
  class PluginInfo
26
- attr_reader :name
27
- attr_reader :setup_path
28
- attr_reader :gem_name
9
+ attr_reader :path
10
+ attr_reader :gemname
29
11
  attr_reader :version
30
12
 
31
- def parse(setup_path_or_name, version)
32
- setup_path_or_name = setup_path_or_name.to_s.strip
33
- @name, @setup_path, @gem_name, @version =
34
- if setup_file_directly_given?(setup_path_or_name)
35
- [nil, setup_path_or_name, *find_plugin_gem(setup_path_or_name)]
13
+ def self.parse(path_or_name, version)
14
+ info = new
15
+ info.parse(path_or_name.to_s.strip, version)
16
+ info
17
+ end
18
+
19
+ def parse(path_or_name, version)
20
+ @name, @path, @gemname, @version =
21
+ if plugin_path?(path_or_name)
22
+ [nil, path_or_name, *find_gemspec_by_path(path_or_name)]
36
23
  else
37
24
  [
38
- setup_path_or_name, get_setup_path(setup_path_or_name),
39
- extract_gem_name(setup_path_or_name), version
25
+ path_or_name, get_plugin_path(path_or_name),
26
+ get_gemname(path_or_name), version
40
27
  ]
41
28
  end
42
- self
43
29
  end
44
30
 
45
31
  def to_s
46
- if name && version
47
- "#{name} (#{version})"
48
- elsif name
49
- name
32
+ if @name && @version
33
+ "#{@name} (#{@version})"
34
+ elsif @name
35
+ @name
50
36
  else
51
- setup_path
37
+ @path
52
38
  end
53
39
  end
54
40
 
55
41
  private
56
42
 
57
- def setup_file_directly_given?(setup_path_or_name)
58
- File.ext(setup_path_or_name) == 'rb' ||
59
- File.basename(setup_path_or_name, '.*') == 'setup'
43
+ def plugin_path?(path_or_name)
44
+ path_or_name == DEFAULT_PLUGSINS || File.ext(path_or_name) == 'rb'
60
45
  end
61
46
 
62
- def find_plugin_gem(setup_path)
63
- gemspec =
64
- Gem::Specification
65
- .each.find { |spec| match_gemspec?(spec, setup_path) }
66
- gemspec && [gemspec.name, gemspec.version]
47
+ def find_gemspec_by_path(path)
48
+ Gem::Specification
49
+ .each.find { |spec| match_gemspec_path?(spec, path) }
50
+ .then { |spec| spec && [spec.name, spec.version] }
67
51
  end
68
52
 
69
- def match_gemspec?(gemspec, setup_path)
70
- gemspec.full_require_paths.any?(&setup_path.method(:start_with?))
53
+ def match_gemspec_path?(gemspec, path)
54
+ gemspec.full_require_paths.any?(&path.method(:start_with?))
71
55
  end
72
56
 
73
- def extract_gem_name(plugin_name)
74
- plugin_name.split('/', 2).first
57
+ def get_plugin_path(name)
58
+ base_name, sub_name = name.split('/', 2)
59
+ base_name = base_name.sub(/^rggen[-_]/, '').tr('-', '_')
60
+ File.join(*['rggen', base_name, sub_name].compact)
75
61
  end
76
62
 
77
- def get_setup_path(plugin_name)
78
- base, sub_directory = plugin_name.split('/', 2)
79
- base = base.sub(/^rggen[-_]/, '').tr('-', '_')
80
- File.join(*['rggen', base, sub_directory, 'setup'].compact)
63
+ def get_gemname(name)
64
+ name.split('/', 2).first
81
65
  end
82
66
  end
83
67
 
@@ -87,9 +71,9 @@ module RgGen
87
71
  @plugins = []
88
72
  end
89
73
 
90
- def load_plugin(setup_path_or_name, version = nil)
91
- info = PluginInfo.new.parse(setup_path_or_name, version)
92
- read_setup_file(info)
74
+ def load_plugin(path_or_name, version = nil)
75
+ info = PluginInfo.parse(path_or_name, version)
76
+ read_plugin_file(info)
93
77
  end
94
78
 
95
79
  def load_plugins(plugins, no_default_plugins, activation = true)
@@ -99,17 +83,19 @@ module RgGen
99
83
  activation && activate_plugins
100
84
  end
101
85
 
102
- def register_plugin(plugin_module, &block)
103
- plugin?(plugin_module) ||
104
- (raise Core::PluginError.new('no plugin spec is given'))
105
- @plugins << PluginRegistry.new(plugin_module, &block)
86
+ def setup_plugin(plugin_name, &block)
87
+ @plugins << PluginSpec.new(plugin_name, &block)
88
+ end
89
+
90
+ def activate_plugins
91
+ do_normal_activation
92
+ do_addtional_activation
106
93
  end
107
94
 
108
- def activate_plugins(**options)
109
- options[:no_default_setup] ||
110
- @plugins.each { |plugin| plugin.default_setup(@builder) }
111
- options[:no_optional_setup] ||
112
- @plugins.each { |plugin| plugin.optional_setup(@builder) }
95
+ def activate_plugin_by_name(plugin_name)
96
+ target_plugin = @plugins.find { |plugin| plugin.name == plugin_name }
97
+ target_plugin&.activate(@builder)
98
+ target_plugin&.activate_additionally(@builder)
113
99
  end
114
100
 
115
101
  def version_info
@@ -118,9 +104,9 @@ module RgGen
118
104
 
119
105
  private
120
106
 
121
- def read_setup_file(info)
122
- info.gem_name && gem(info.gem_name, info.version)
123
- require info.setup_path
107
+ def read_plugin_file(info)
108
+ info.gemname && gem(info.gemname, info.version)
109
+ require info.path
124
110
  rescue ::LoadError
125
111
  raise Core::PluginError.new("cannot load such plugin: #{info}")
126
112
  end
@@ -133,8 +119,6 @@ module RgGen
133
119
  ]
134
120
  end
135
121
 
136
- DEFAULT_PLUGSINS = 'rggen/setup'
137
-
138
122
  def default_plugins(no_default_plugins)
139
123
  load_default_plugins?(no_default_plugins) && DEFAULT_PLUGSINS || nil
140
124
  end
@@ -153,8 +137,12 @@ module RgGen
153
137
  &.map { |entry| entry.split(',', 2) }
154
138
  end
155
139
 
156
- def plugin?(plugin_module)
157
- plugin_module.respond_to?(:plugin_spec) && plugin_module.plugin_spec
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) }
158
146
  end
159
147
  end
160
148
  end
@@ -4,14 +4,16 @@ module RgGen
4
4
  module Core
5
5
  module Builder
6
6
  class PluginSpec
7
- def initialize(name, plugin_module)
7
+ def initialize(name)
8
8
  @name = name
9
- @plugin_module = plugin_module
10
9
  @component_registrations = []
11
10
  @loader_registrations = []
12
11
  @files = []
12
+ block_given? && yield(self)
13
13
  end
14
14
 
15
+ attr_reader :name
16
+
15
17
  def version(value = nil)
16
18
  @version = value if value
17
19
  version_value
@@ -25,13 +27,8 @@ module RgGen
25
27
  @component_registrations << [component, layers, body]
26
28
  end
27
29
 
28
- def register_loader(component, loader_type, loader)
29
- @loader_registrations << [component, loader_type, loader]
30
- end
31
-
32
- def register_loaders(component, loader_type, loaders)
33
- Array(loaders)
34
- .each { |loader| register_loader(component, loader_type, loader) }
30
+ def setup_loader(component, loader_type, &body)
31
+ @loader_registrations << [component, loader_type, body]
35
32
  end
36
33
 
37
34
  def register_files(files)
@@ -41,23 +38,26 @@ module RgGen
41
38
 
42
39
  alias_method :files, :register_files
43
40
 
41
+ def addtional_setup(&body)
42
+ @addtional_setup = body
43
+ end
44
+
44
45
  def activate(builder)
45
46
  activate_components(builder)
46
47
  activate_loaders(builder)
47
48
  load_files
48
49
  end
49
50
 
51
+ def activate_additionally(builder)
52
+ @addtional_setup&.call(builder)
53
+ end
54
+
50
55
  private
51
56
 
52
57
  DEFAULT_VERSION = '0.0.0'
53
58
 
54
59
  def version_value
55
- @version || const_version || DEFAULT_VERSION
56
- end
57
-
58
- def const_version
59
- @plugin_module.const_defined?(:VERSION) &&
60
- @plugin_module.const_get(:VERSION)
60
+ @version || DEFAULT_VERSION
61
61
  end
62
62
 
63
63
  def activate_components(builder)
@@ -69,8 +69,10 @@ module RgGen
69
69
  end
70
70
 
71
71
  def activate_loaders(builder)
72
- @loader_registrations.each do |component, loader_type, loader|
73
- builder.register_loader(component, loader_type, loader)
72
+ @loader_registrations.each do |component, loader_type, body|
73
+ builder
74
+ .input_component_registry(component)
75
+ .setup_loader(loader_type, &body)
74
76
  end
75
77
  end
76
78
 
@@ -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)
@@ -11,17 +11,17 @@ module RgGen
11
11
  :input_component_registry,
12
12
  :output_component_registry,
13
13
  :register_loader,
14
+ :setup_loader,
15
+ :load_plugin,
14
16
  :define_loader,
15
17
  :define_simple_feature,
16
18
  :define_list_feature,
17
19
  :define_list_item_feature,
18
20
  :define_value_extractor,
19
21
  :enable,
20
- :disable_all,
21
- :disable,
22
+ :enable_all,
22
23
  :delete,
23
- :register_plugin,
24
- :load_plugin
24
+ :setup_plugin
25
25
  ].each do |method_name|
26
26
  def_delegator :'RgGen.builder', method_name
27
27
  end
@@ -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.26.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
 
@@ -105,7 +108,6 @@ require_relative 'core/builder/plugin_manager'
105
108
  require_relative 'core/builder/builder'
106
109
  require_relative 'core/builder'
107
110
 
108
- require_relative 'core/plugin'
109
111
  require_relative 'core/printers'
110
112
  require_relative 'core/options'
111
113
  require_relative 'core/dsl'
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.26.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-03-25 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
@@ -165,7 +166,6 @@ files:
165
166
  - lib/rggen/core/output_base/raise_error.rb
166
167
  - lib/rggen/core/output_base/source_file_component_factory.rb
167
168
  - lib/rggen/core/output_base/template_engine.rb
168
- - lib/rggen/core/plugin.rb
169
169
  - lib/rggen/core/printers.rb
170
170
  - lib/rggen/core/register_map.rb
171
171
  - lib/rggen/core/register_map/component.rb
@@ -188,12 +188,13 @@ files:
188
188
  - lib/rggen/core/utility/code_utility/structure_definition.rb
189
189
  - lib/rggen/core/utility/error_utility.rb
190
190
  - lib/rggen/core/utility/regexp_patterns.rb
191
+ - lib/rggen/core/utility/type_checker.rb
191
192
  - lib/rggen/core/version.rb
192
193
  homepage: https://github.com/rggen/rggen-core
193
194
  licenses:
194
195
  - MIT
195
196
  metadata:
196
- bug_tracker_uri: https://github.com/rggen/rggen-core/issues
197
+ bug_tracker_uri: https://github.com/rggen/rggen/issues
197
198
  mailing_list_uri: https://groups.google.com/d/forum/rggen
198
199
  rubygems_mfa_required: 'true'
199
200
  source_code_uri: https://github.com/rggen/rggen-core
@@ -213,8 +214,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
213
214
  - !ruby/object:Gem::Version
214
215
  version: '0'
215
216
  requirements: []
216
- rubygems_version: 3.3.3
217
+ rubygems_version: 3.3.7
217
218
  signing_key:
218
219
  specification_version: 4
219
- summary: rggen-core-0.26.0
220
+ summary: rggen-core-0.28.0
220
221
  test_files: []
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RgGen
4
- module Core
5
- module Plugin
6
- attr_reader :plugin_spec
7
-
8
- private
9
-
10
- def setup_plugin(name)
11
- @plugin_spec = Builder::PluginSpec.new(name, self)
12
- block_given? && yield(@plugin_spec)
13
- end
14
- end
15
- end
16
- end