Class: AdvancedCodeGenerator::MethodConfig

Inherits:
Object
  • Object
show all
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.

Examples:

Method configuration usage

config = CodeGenerator::MethodConfig.new(:calculate, :public) do |m|
  m.required :x
  m.optional :y, default: 10
  m.returns 42
end

See Also:

  • CodeGenerator::Generator
  • CodeGenerator::Parameter

Constant Summary collapse

VALID_VISIBILITIES =

Valid visibility options for method definitions.

Returns:

  • (Array<Symbol>)
i[public private protected public_class private_class].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, visibility) {|method_config| ... } ⇒ MethodConfig

Initializes a new MethodConfig instance.

Examples:

config = CodeGenerator::MethodConfig.new(:my_method, :public) do |m|
  m.required :param1
  m.returns "result"
end

Parameters:

  • name (Symbol, String)

    The name of the method to configure

  • visibility (Symbol)

    The visibility level (must be one of VALID_VISIBILITIES)

Yields:

  • (method_config)

    Configuration block for method parameters and settings

Yield Parameters:

  • method_config (CodeGenerator::MethodConfig)

    The current method configuration instance

Raises:

  • (ArgumentError)

    If name is not a Symbol/String or visibility is invalid



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

#nameSymbol (readonly)

Returns The name of the method to be defined.

Returns:

  • (Symbol)

    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

#visibilitySymbol (readonly)

Returns The visibility level (:public, :private, :protected, :public_class, :private_class).

Returns:

  • (Symbol)

    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.

Examples:

m.returns Integer
m.generate true
# Method will return random integers like 42891, not the Integer class

Parameters:

  • value (Boolean) (defaults to: true)

    Whether to enable random generation (defaults to true)

Returns:

  • (Boolean)

    The provided value



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.

Examples:

m.keyword :timeout, default: 30
# Generates: def method_name(timeout: 30)

Parameters:

  • param_name (Symbol)

    The name of the optional keyword parameter

  • default (Object) (defaults to: nil)

    The default value for the parameter (defaults to nil)

Raises:

  • (ArgumentError)

    If param_name is not a Symbol



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.

Examples:

m.keyword_required :format
# Generates: def method_name(format:)

Parameters:

  • param_name (Symbol)

    The name of the required keyword parameter

Raises:

  • (ArgumentError)

    If param_name is not a Symbol



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.

Examples:

m.optional :options, default: {}
# Generates: def method_name(options = {})

Parameters:

  • param_name (Symbol)

    The name of the optional parameter

  • default (Object) (defaults to: nil)

    The default value for the parameter (defaults to nil)

Raises:

  • (ArgumentError)

    If param_name is not a Symbol



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.

Examples:

m.required :user_id
# Generates: def method_name(user_id)

Parameters:

  • param_name (Symbol)

    The name of the required parameter

Raises:

  • (ArgumentError)

    If param_name is not a Symbol



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.

Examples:

m.returns "success"
m.returns Integer  # Will generate random integers if #generate is true

Parameters:

  • value (Object)

    The value to return from the method

Returns:

  • (Object)

    The provided value



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

def returns(value)
  self.return_value = value
end