Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions lib/stroma/dsl/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ def initialize(matrix)
# - extensions DSL for registering hooks
#
# @return [Module] The generated DSL module
def generate # rubocop:disable Metrics/MethodLength
def generate # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
matrix = @matrix
class_methods = build_class_methods

Module.new do
mod = Module.new do
@stroma_matrix = matrix

class << self
Expand All @@ -81,10 +81,31 @@ def included(base)

const_set(:ClassMethods, class_methods)
end

label_module(mod, "Stroma::DSL(#{matrix.name})")
label_module(class_methods, "Stroma::DSL(#{matrix.name})::ClassMethods")

mod
end

private

# Assigns a descriptive label to an anonymous module for debugging.
# Uses set_temporary_name (Ruby 3.3+) when available.
#
# TODO: Remove the else branch when Ruby 3.2 support is dropped.
# The define_singleton_method fallback is a temporary workaround
# that only affects #inspect and #to_s. Unlike set_temporary_name,
# it does not set #name, so the module remains technically anonymous.
def label_module(mod, label)
if mod.respond_to?(:set_temporary_name)
mod.set_temporary_name(label)
else
mod.define_singleton_method(:inspect) { label }
mod.define_singleton_method(:to_s) { label }
end
end

# Builds the ClassMethods module.
#
# @return [Module] The ClassMethods module
Expand Down
2 changes: 2 additions & 0 deletions sig/lib/stroma/dsl/generator.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module Stroma

private

def label_module: (Module mod, String label) -> void

def build_class_methods: () -> Module
end
end
Expand Down
19 changes: 19 additions & 0 deletions spec/stroma/dsl/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@
it "stores matrix reference" do
expect(dsl_module.stroma_matrix).to eq(matrix)
end

describe "module labeling" do
it "labels DSL module with matrix name", :aggregate_failures do
expect(dsl_module.inspect).to eq("Stroma::DSL(test)")
expect(dsl_module.to_s).to eq("Stroma::DSL(test)")
end

it "labels ClassMethods with matrix name", :aggregate_failures do
class_methods = dsl_module.const_get(:ClassMethods)
expect(class_methods.inspect).to include("Stroma::DSL(test)")
expect(class_methods.inspect).to include("ClassMethods")
end

if Module.new.respond_to?(:set_temporary_name)
it "sets module name via set_temporary_name" do
expect(dsl_module.name).to eq("Stroma::DSL(test)")
end
end
end
end

describe "generated module" do
Expand Down