AdvancedCodeGenerator (ACG)
A fluent DSL for generating Ruby classes with stubbed methods for testing and prototyping.
Features
- Fluent DSL: Clean, readable syntax for defining methods
- Full visibility support: Public, private, and protected instance methods
- Class method support: Public and private class methods
- Parameter configuration: Define required, optional, and keyword parameters
- Smart return values: Return specific objects or generate random values
- Zero dependencies: Pure Ruby, no external requirements
Installation
Add this line to your application's Gemfile:
gem 'advanced_code_generator'
And then execute:
bundle install
Or install it yourself:
gem install advanced_code_generator
Usage Examples
Basic Public Method
generator = AdvancedCodeGenerator::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 = AdvancedCodeGenerator::Generator.new do |g|
g.public_method :greet do |m|
m.required :name
m.optional :greeting, default: "Hello"
m.keyword_required :format
m.returns true
end
end
Klass = generator.build
obj = Klass.new
obj.greet("Alice", format: :json) # => true
obj.greet("Bob", "Hi", format: :xml) # => true
Private and Protected Methods
generator = AdvancedCodeGenerator::Generator.new do |g|
g.private_method :secret_calculation do |m|
m.returns 42
end
g.protected_method :internal_logic do |m|
m.returns "protected result"
end
end
Klass = generator.build
obj = Klass.new
obj.send(:secret_calculation) # => 42
# obj.secret_calculation # => NoMethodError
# Protected method access through subclass
Subclass = Class.new(Klass) do
def access_protected
internal_logic
end
end
Subclass.new.access_protected # => "protected result"
Class Methods
generator = AdvancedCodeGenerator::Generator.new do |g|
g.public_class_method :factory do |m|
m.returns "class helper"
end
g.private_class_method :setup do |m|
m.returns "private setup"
end
end
Klass = generator.build
Klass.factory # => "class helper"
Klass.send(:setup) # => "private setup"
Random Value Generation
generator = AdvancedCodeGenerator::Generator.new do |g|
g.public_method :random_int do |m|
m.returns Integer
m.generate true
end
g.public_method :random_string do |m|
m.returns String
m.generate true
end
end
Klass = generator.build
obj = Klass.new
obj.random_int # => 42891 (random integer)
obj.random_string # => "aB3xY9zK2m" (random string)
Testing
Run the test suite:
bundle exec rspec
Development
After checking out the repo, run:
bin/setup
This will install dependencies and start an interactive console.
Available Commands
bin/console- Interactive development consolebin/setup- Install dependencies and build gembundle exec rake- Run tests and linting
Release Process
- Update version in
lib/code_generator/version.rb - Create and push a git tag:
git tag v0.1.0 && git push origin v0.1.0 - GitHub Actions will automatically:
- Build the gem
- Publish to RubyGems.org
- Create a GitHub release
Requirements
- Ruby: >= 2.7.0
- No external dependencies
Contributing
Bug reports and pull requests are welcome! Please follow these guidelines:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -am 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a pull request
Please ensure your code passes all tests and follows the existing style.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting with this project is expected to follow the Code of Conduct.
Note: This gem is designed for testing and prototyping. Generated methods accept any parameters and return configured values, making it perfect for creating test doubles and stubs.