Class: AdvancedCodeGenerator::Generator
- Inherits:
-
Object
- Object
- AdvancedCodeGenerator::Generator
- 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.
Instance Attribute Summary collapse
-
#class_methods ⇒ Array<AdvancedCodeGenerator::MethodConfig>
readonly
List of class method configurations.
-
#methods ⇒ Array<AdvancedCodeGenerator::MethodConfig>
readonly
List of instance method configurations.
Class Method Summary collapse
-
.new {|generator| ... } ⇒ AdvancedCodeGenerator::Generator
Creates a new Generator instance and evaluates the given block in its context.
Instance Method Summary collapse
-
#build ⇒ Class
Builds and returns a new Ruby class with all configured methods defined.
-
#initialize ⇒ void
constructor
Initializes a new Generator instance with empty method collections.
-
#private_class_method(name) {|method_config| ... } ⇒ void
Defines a private class method on the generated class.
-
#private_method(name) {|method_config| ... } ⇒ void
Defines a private instance method on the generated class.
-
#protected_method(name) {|method_config| ... } ⇒ void
Defines a protected instance method on the generated class.
-
#public_class_method(name) {|method_config| ... } ⇒ void
Defines a public class method on the generated class.
-
#public_method(name) {|method_config| ... } ⇒ void
Defines a public instance method on the generated class.
Constructor Details
#initialize ⇒ void
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_methods ⇒ Array<AdvancedCodeGenerator::MethodConfig> (readonly)
Returns List of class method configurations.
34 35 36 |
# File 'lib/advanced_code_generator/generator.rb', line 34 def class_methods @class_methods end |
#methods ⇒ Array<AdvancedCodeGenerator::MethodConfig> (readonly)
Returns List of instance method configurations.
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.
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
#build ⇒ Class
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.
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.
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.
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.
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.
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.
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 |