Class: AdvancedCodeGenerator::MethodConfig
- Inherits:
-
Object
- Object
- AdvancedCodeGenerator::MethodConfig
- Defined in:
- lib/advanced_code_generator/method_config.rb
Overview
Represents the configuration for a single method definition.
This class encapsulates all the metadata needed to define a method, including its name, visibility, parameters, return value, and generation settings. It’s used internally by CodeGenerator::Generator to store method configuration data collected through the DSL.
Constant Summary collapse
- VALID_VISIBILITIES =
Valid visibility options for method definitions.
i[public private protected public_class private_class].freeze
Instance Attribute Summary collapse
-
#name ⇒ Symbol
readonly
The name of the method to be defined.
-
#visibility ⇒ Symbol
readonly
The visibility level (:public, :private, :protected, :public_class, :private_class).
Instance Method Summary collapse
-
#generate(value: true) ⇒ Boolean
Enables random value generation for class return types.
-
#initialize(name, visibility) {|method_config| ... } ⇒ MethodConfig
constructor
Initializes a new MethodConfig instance.
-
#keyword(param_name, default: nil) ⇒ void
Adds an optional keyword parameter to the method.
-
#keyword_required(param_name) ⇒ void
Adds a required keyword parameter to the method.
-
#optional(param_name, default: nil) ⇒ void
Adds an optional positional parameter to the method.
-
#required(param_name) ⇒ void
Adds a required positional parameter to the method.
-
#returns(value) ⇒ Object
Sets the return value for the method.
Constructor Details
#initialize(name, visibility) {|method_config| ... } ⇒ MethodConfig
Initializes a new MethodConfig instance.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/advanced_code_generator/method_config.rb', line 54 def initialize(name, visibility) raise ArgumentError, 'Method name must be a Symbol or String' unless name.is_a?(Symbol) || name.is_a?(String) raise ArgumentError, "Invalid visibility: #{visibility}" unless VALID_VISIBILITIES.include?(visibility) @name = name.to_sym @visibility = visibility @parameters = [] @return_value = nil @generate_random = false yield self if block_given? end |
Instance Attribute Details
#name ⇒ Symbol (readonly)
Returns The name of the method to be defined.
22 23 24 |
# File 'lib/advanced_code_generator/method_config.rb', line 22 def name @name end |
#visibility ⇒ Symbol (readonly)
Returns The visibility level (:public, :private, :protected, :public_class, :private_class).
25 26 27 |
# File 'lib/advanced_code_generator/method_config.rb', line 25 def visibility @visibility end |
Instance Method Details
#generate(value: true) ⇒ Boolean
Enables random value generation for class return types.
When enabled and the return value is a Class (like Integer, String, Symbol), the method will return random instances of that class instead of the class itself.
160 161 162 |
# File 'lib/advanced_code_generator/method_config.rb', line 160 def generate(value: true) self.generate_random = value end |
#keyword(param_name, default: nil) ⇒ void
This method returns an undefined value.
Adds an optional keyword parameter to the method.
Optional keyword parameters have a default value and can be omitted when calling the method.
128 129 130 131 |
# File 'lib/advanced_code_generator/method_config.rb', line 128 def keyword(param_name, default: nil) validate_param_name(param_name) parameters << Parameter.new(:keyword, param_name, default: default) end |
#keyword_required(param_name) ⇒ void
This method returns an undefined value.
Adds a required keyword parameter to the method.
Required keyword parameters must be provided as named arguments when calling the method.
111 112 113 114 |
# File 'lib/advanced_code_generator/method_config.rb', line 111 def keyword_required(param_name) validate_param_name(param_name) parameters << Parameter.new(:keyword_required, param_name) end |
#optional(param_name, default: nil) ⇒ void
This method returns an undefined value.
Adds an optional positional parameter to the method.
Optional parameters have a default value and can be omitted when calling the method.
95 96 97 98 |
# File 'lib/advanced_code_generator/method_config.rb', line 95 def optional(param_name, default: nil) validate_param_name(param_name) parameters << Parameter.new(:optional, param_name, default: default) end |
#required(param_name) ⇒ void
This method returns an undefined value.
Adds a required positional parameter to the method.
Required parameters must be provided when calling the method.
78 79 80 81 |
# File 'lib/advanced_code_generator/method_config.rb', line 78 def required(param_name) validate_param_name(param_name) parameters << Parameter.new(:required, param_name) end |
#returns(value) ⇒ Object
Sets the return value for the method.
The method will return this value when called. If combined with #generate, and the return value is a Class, it will generate random instances of that class.
144 145 146 |
# File 'lib/advanced_code_generator/method_config.rb', line 144 def returns(value) self.return_value = value end |