Class: AdvancedCodeGenerator::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/advanced_code_generator/generator.rb

Overview

Generates Ruby classes with stubbed methods using a fluent DSL.

This class provides a domain-specific language (DSL) for defining methods with various visibility levels, parameters, and return values. It’s primarily designed for testing and prototyping scenarios where you need to create mock objects or stub classes quickly.

Examples:

Basic usage

generator = CodeGenerator::Generator.new do |g|
  g.public_method :hello do |m|
    m.returns "world"
  end
end
Klass = generator.build
obj = Klass.new
obj.hello # => "world"

Method with parameters

generator = CodeGenerator::Generator.new do |g|
  g.public_method :greet do |m|
    m.required :name
    m.optional :greeting, default: "Hello"
    m.returns true
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializevoid

Initializes a new Generator instance with empty method collections.

This constructor is typically called internally by new and should not be called directly in most cases.



42
43
44
45
# File 'lib/advanced_code_generator/generator.rb', line 42

def initialize
  @methods = []
  @class_methods = []
end

Instance Attribute Details

#class_methodsArray<AdvancedCodeGenerator::MethodConfig> (readonly)

Returns List of class method configurations.

Returns:



34
35
36
# File 'lib/advanced_code_generator/generator.rb', line 34

def class_methods
  @class_methods
end

#methodsArray<AdvancedCodeGenerator::MethodConfig> (readonly)

Returns List of instance method configurations.

Returns:



31
32
33
# File 'lib/advanced_code_generator/generator.rb', line 31

def methods
  @methods
end

Class Method Details

.new {|generator| ... } ⇒ AdvancedCodeGenerator::Generator

Creates a new Generator instance and evaluates the given block in its context.

This is the primary entry point for using the DSL. The block parameter provides access to the generator’s DSL methods for defining methods.

Examples:

generator = CodeGenerator::Generator.new do |g|
  g.public_method :test_method do |m|
    m.returns "test"
  end
end

Yields:

  • (generator)

    The generator instance for DSL configuration

Yield Parameters:

  • generator (CodeGenerator::Generator)

    The current generator instance

Returns:



62
63
64
65
66
67
# File 'lib/advanced_code_generator/generator.rb', line 62

def self.new(&block)
  generator = allocate
  generator.__send__(:initialize)
  generator.instance_eval(&block) if block
  generator
end

Instance Method Details

#buildClass

Builds and returns a new Ruby class with all configured methods defined.

This method creates an anonymous class and defines all the methods that were configured through the DSL. The returned class can be instantiated and used like any other Ruby class.

Examples:

generator = CodeGenerator::Generator.new do |g|
  g.public_method :hello do |m|
    m.returns "world"
  end
end
Klass = generator.build
obj = Klass.new
puts obj.hello # => "world"

Returns:

  • (Class)

    A new class with all configured methods



86
87
88
89
90
91
# File 'lib/advanced_code_generator/generator.rb', line 86

def build
  klass = Class.new
  define_instance_methods(klass)
  define_class_methods(klass)
  klass
end

#private_class_method(name) {|method_config| ... } ⇒ void

This method returns an undefined value.

Defines a private class method on the generated class.

Private class methods can only be called within the class context using Object#send on the class itself.

Examples:

g.private_class_method :internal_setup do |m|
  m.returns "private setup"
end

Parameters:

  • name (Symbol, String)

    The name of the method to define

Yields:

  • (method_config)

    Configuration block for the method

Yield Parameters:

  • method_config (CodeGenerator::MethodConfig)

    Method configuration object



181
182
183
184
# File 'lib/advanced_code_generator/generator.rb', line 181

def private_class_method(name, &block)
  method_config = MethodConfig.new(name, :private_class, &block)
  @class_methods << method_config
end

#private_method(name) {|method_config| ... } ⇒ void

This method returns an undefined value.

Defines a private instance method on the generated class.

Private methods can only be called within the class or its instances using Object#send or from within other instance methods.

Examples:

g.private_method :internal_calculation do |m|
  m.returns "private result"
end

Parameters:

  • name (Symbol, String)

    The name of the method to define

Yields:

  • (method_config)

    Configuration block for the method

Yield Parameters:

  • method_config (CodeGenerator::MethodConfig)

    Method configuration object



125
126
127
128
# File 'lib/advanced_code_generator/generator.rb', line 125

def private_method(name, &block)
  method_config = MethodConfig.new(name, :private, &block)
  @methods << method_config
end

#protected_method(name) {|method_config| ... } ⇒ void

This method returns an undefined value.

Defines a protected instance method on the generated class.

Protected methods can be called by instances of the same class or its subclasses, but not from outside the inheritance hierarchy.

Examples:

g.protected_method :shared_logic do |m|
  m.returns "protected result"
end

Parameters:

  • name (Symbol, String)

    The name of the method to define

Yields:

  • (method_config)

    Configuration block for the method

Yield Parameters:

  • method_config (CodeGenerator::MethodConfig)

    Method configuration object



144
145
146
147
# File 'lib/advanced_code_generator/generator.rb', line 144

def protected_method(name, &block)
  method_config = MethodConfig.new(name, :protected, &block)
  @methods << method_config
end

#public_class_method(name) {|method_config| ... } ⇒ void

This method returns an undefined value.

Defines a public class method on the generated class.

Class methods are called on the class itself rather than instances.

Examples:

g.public_class_method :factory do |m|
  m.returns "class helper"
end

Parameters:

  • name (Symbol, String)

    The name of the method to define

Yields:

  • (method_config)

    Configuration block for the method

Yield Parameters:

  • method_config (CodeGenerator::MethodConfig)

    Method configuration object



162
163
164
165
# File 'lib/advanced_code_generator/generator.rb', line 162

def public_class_method(name, &block)
  method_config = MethodConfig.new(name, :public_class, &block)
  @class_methods << method_config
end

#public_method(name) {|method_config| ... } ⇒ void

This method returns an undefined value.

Defines a public instance method on the generated class.

Examples:

g.public_method :calculate do |m|
  m.required :x
  m.optional :y, default: 10
  m.returns 42
end

Parameters:

  • name (Symbol, String)

    The name of the method to define

Yields:

  • (method_config)

    Configuration block for the method

Yield Parameters:

  • method_config (CodeGenerator::MethodConfig)

    Method configuration object



106
107
108
109
# File 'lib/advanced_code_generator/generator.rb', line 106

def public_method(name, &block)
  method_config = MethodConfig.new(name, :public, &block)
  @methods << method_config
end