Skip to content

Commit af98778

Browse files
authored
Refactor utility methods and optimize registry key handling (#8)
1 parent c6727ea commit af98778

File tree

10 files changed

+69
-26
lines changed

10 files changed

+69
-26
lines changed

lib/stroma.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
require "zeitwerk"
44

5-
require "active_support/all"
6-
75
loader = Zeitwerk::Loader.for_gem
86
loader.ignore("#{__dir__}/stroma/test_kit/rspec")
97
loader.inflector.inflect(

lib/stroma/dsl/generator.rb

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,30 +82,14 @@ def included(base)
8282
const_set(:ClassMethods, class_methods)
8383
end
8484

85-
label_module(mod, "Stroma::DSL(#{matrix.name})")
86-
label_module(class_methods, "Stroma::DSL(#{matrix.name})::ClassMethods")
85+
Utils.name_module(mod, "Stroma::DSL(#{matrix.name})")
86+
Utils.name_module(class_methods, "Stroma::DSL(#{matrix.name})::ClassMethods")
8787

8888
mod
8989
end
9090

9191
private
9292

93-
# Assigns a descriptive label to an anonymous module for debugging.
94-
# Uses set_temporary_name (Ruby 3.3+) when available.
95-
#
96-
# TODO: Remove the else branch when Ruby 3.2 support is dropped.
97-
# The define_singleton_method fallback is a temporary workaround
98-
# that only affects #inspect and #to_s. Unlike set_temporary_name,
99-
# it does not set #name, so the module remains technically anonymous.
100-
def label_module(mod, label)
101-
if mod.respond_to?(:set_temporary_name)
102-
mod.set_temporary_name(label)
103-
else
104-
mod.define_singleton_method(:inspect) { label }
105-
mod.define_singleton_method(:to_s) { label }
106-
end
107-
end
108-
10993
# Builds the ClassMethods module.
11094
#
11195
# @return [Module] The ClassMethods module

lib/stroma/hooks/hook.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module Hooks
4646
# @param extension [Module] Extension module to include
4747
# @raise [Exceptions::InvalidHookType] If type is invalid
4848
def initialize(type:, target_key:, extension:)
49-
if VALID_HOOK_TYPES.exclude?(type)
49+
unless VALID_HOOK_TYPES.include?(type)
5050
raise Exceptions::InvalidHookType,
5151
"Invalid hook type: #{type.inspect}. Valid types: #{VALID_HOOK_TYPES.map(&:inspect).join(', ')}"
5252
end

lib/stroma/registry.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def finalize!
7070
return if @finalized
7171

7272
@entries.freeze
73+
@keys = @entries.map(&:key).freeze
7374
@finalized = true
7475
end
7576

@@ -88,7 +89,7 @@ def entries
8889
# @return [Array<Symbol>] The registry keys
8990
def keys
9091
ensure_finalized!
91-
@entries.map(&:key)
92+
@keys
9293
end
9394

9495
# Checks if a key is registered.
@@ -98,7 +99,7 @@ def keys
9899
# @return [Boolean] true if the key is registered
99100
def key?(key)
100101
ensure_finalized!
101-
@entries.any? { |e| e.key == key.to_sym }
102+
@keys.include?(key.to_sym)
102103
end
103104

104105
private

lib/stroma/utils.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
module Stroma
4+
# Shared utility methods for the Stroma framework.
5+
#
6+
# ## Purpose
7+
#
8+
# Provides common helper methods used across multiple Stroma components.
9+
# All methods are module functions - callable as both module methods
10+
# and instance methods when included.
11+
module Utils
12+
module_function
13+
14+
# Assigns a temporary name to an anonymous module for debugging clarity.
15+
# Uses set_temporary_name (Ruby 3.3+) when available.
16+
#
17+
# TODO: Remove the else branch when Ruby 3.2 support is dropped.
18+
# The define_singleton_method fallback is a temporary workaround
19+
# that only affects #inspect and #to_s. Unlike set_temporary_name,
20+
# it does not set #name, so the module remains technically anonymous.
21+
#
22+
# @param mod [Module] The module to name
23+
# @param name [String] The temporary name
24+
# @return [void]
25+
def name_module(mod, name)
26+
if mod.respond_to?(:set_temporary_name)
27+
mod.set_temporary_name(name)
28+
else
29+
mod.define_singleton_method(:inspect) { name }
30+
mod.define_singleton_method(:to_s) { name }
31+
end
32+
end
33+
end
34+
end

sig/lib/stroma/dsl/generator.rbs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ module Stroma
1111

1212
private
1313

14-
def label_module: (Module mod, String label) -> void
15-
1614
def build_class_methods: () -> Module
1715
end
1816
end

sig/lib/stroma/registry.rbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Stroma
22
class Registry
33
@matrix_name: Symbol
44
@entries: Array[Entry]
5+
@keys: Array[Symbol]
56
@finalized: bool
67

78
attr_reader matrix_name: Symbol

sig/lib/stroma/utils.rbs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Stroma
2+
module Utils
3+
def self.name_module: (Module mod, String name) -> void
4+
5+
private
6+
7+
def name_module: (Module mod, String name) -> void
8+
end
9+
end

spec/stroma/utils_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe Stroma::Utils do
4+
describe ".name_module" do
5+
let(:mod) { Module.new }
6+
let(:name) { "Stroma::Test(example)" }
7+
8+
before { described_class.name_module(mod, name) }
9+
10+
it "sets inspect to the name" do
11+
expect(mod.inspect).to eq(name)
12+
end
13+
14+
it "sets to_s to the name" do
15+
expect(mod.to_s).to eq(name)
16+
end
17+
end
18+
end

spec/stroma/version_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
RSpec.describe Stroma::VERSION do
4-
it { expect(Stroma::VERSION::STRING).to be_present }
4+
it { expect(Stroma::VERSION::STRING).not_to be_nil }
55
end

0 commit comments

Comments
 (0)