Skip to content

Commit 3d57f74

Browse files
committed
Add configuration enhancer to iro-runtime DSL
This introduces a `ConfigurationEnhancer` class to allow easier extension and validation of settings within `Iro::Configuration` This Provides a cleaner and reusable API to manage configuration enhancements across plugins.
1 parent 910658b commit 3d57f74

File tree

4 files changed

+60
-10
lines changed

4 files changed

+60
-10
lines changed

iro-core-rb/lib/iro-core.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@
66

77
root_path = File.dirname(__FILE__)
88

9-
Iro.plug('iro-core', root_path) do
9+
Iro.plug 'iro-core', root_path do
1010
loader do
1111
inflect 'lru_store' => 'LRUStore'
1212
end
13+
14+
on_load 'Iro::XYZ' do
15+
enhance_module 'Iro::Reference::White' do
16+
constants.each do |constant_name|
17+
override_constant(constant_name) { |original| Iro::XYZ.from_intermediate(*original) }
18+
end
19+
end
20+
end
1321
end

iro-runtime-rb/lib/iro/runtime/plugin/dsl/behavior.rb

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ def initialize(manager, &block)
1212
end
1313

1414
def enhance_module(module_name, &enhancement)
15-
predicate = ->(_) { Runtime.constant.loaded?(module_name) }
16-
17-
on_any(:constant_loaded, if: predicate) do
15+
on_load(module_name) do
1816
ModuleEnhancer.new(Runtime.constant.resolve(module_name), &enhancement)
1917
end
2018
end
@@ -28,16 +26,31 @@ def on_any(event_type = :ANY, **options, &)
2826
@manager.enqueue(if: predicate, **options.except(:if), &)
2927
end
3028

31-
def on_load(...)
32-
on_any(:constant_loaded, ...)
29+
def on_load(constant_name, **options, &)
30+
predicate = lambda do |data|
31+
Runtime.constant.loaded?(constant_name) &&
32+
(options.key?(:if) ? options[:if].call(data) : true)
33+
end
34+
35+
on_any(if: predicate, **options.except(:if), &)
3336
end
3437

35-
def on_plugin(...)
36-
on_any(:plugin_registered, ...)
38+
def on_plugin(plugin_name, **options, &)
39+
predicate = lambda do |data|
40+
Runtime.plugin.exist?(plugin_name) &&
41+
(options.key?(:if) ? options[:if].call(data) : true)
42+
end
43+
44+
on_any(if: predicate, **options.except(:if), &)
3745
end
3846

39-
def on_unload(...)
40-
on_any(:constant_unloaded, ...)
47+
def on_unload(constant_name, **options, &)
48+
predicate = lambda do |data|
49+
data[:constant]&.name == constant_name &&
50+
(options.key?(:if) ? options[:if].call(data) : true)
51+
end
52+
53+
on_any(:constant_unloaded, if: predicate, **options.except(:if), &)
4154
end
4255
end
4356
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
module Iro
4+
module Runtime
5+
class Plugin
6+
class DSL
7+
class ConfigurationEnhancer
8+
include Behavior
9+
10+
def setting(...)
11+
enhance_module('Iro::Configuration') do
12+
setting(...)
13+
end
14+
end
15+
16+
def validate_setting(...)
17+
enhance_module('Iro::Configuration') do
18+
validates(...)
19+
end
20+
end
21+
end
22+
end
23+
end
24+
end
25+
end

iro-runtime-rb/lib/iro/runtime/plugin/dsl/main.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ class DSL
77
class Main
88
include Behavior
99

10+
def configuration(&)
11+
ConfigurationEnhancer.new(@manager, &)
12+
end
13+
1014
def loader(&)
1115
LoadConfiguration.new(@manager, &)
1216
end

0 commit comments

Comments
 (0)