Ruby Model Generator
Commandby davila7 · Added 5mo ago
Claude
Install
npx claude-code-templates@latest --command=cli-tool/templates/ruby/.claude/commands --yesAdd to Claude
claude mcp add ruby-model-generatorAbout
Ruby Model Generator
Create Ruby classes and models with proper structure and best practices.
Purpose
This command helps you quickly create Ruby model classes with proper initialization, attributes, and methods following Ruby conventions.
Usage
/model
What this command does
- Creates model classes with proper initialization methods
- Adds attribute accessors and instance variables
- Includes validation methods and error handling
- Follows Ruby conventions and best practices
- Generates related test files when applicable
Example Output
# app/models/user.rb
class User
attr_accessor :name, :email, :age
attr_reader :id, :created_at
def initialize(attributes = {})
@id = attributes[:id] || generate_id
@name = attributes[:name]
@email = attributes[:email]
@age = attributes[:age]
@created_at = Time.now
@errors = []
end
def valid?
@errors.clear
validate_presence_of_name
validate_email_format
validate_age_range
@errors.empty?
end
def errors
@errors.dup
end
def save
return false unless valid?
# Persist logic here
true
end
def to_h
{
id: @id,
name: @name,
email: @email,
age: @age,
created_at: @created_at
}
end
def to_json(*args)
JSON.generate(to_h, *args)
end
private
def validate_presence_of_name
if @name.nil? || @name.strip.empty?
@errors << "Name cannot be blank"
end
end
def validate_email_format
if @email && !@email.match?(/\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i)
@errors << "Email format is invalid"
end
end
def validate_age_range
if @age && (@age < 0 || @age > 150)
@errors << "Age must be between 0 and 150"
end
end
def generate_id
SecureRandom.uuid
end
end
# Example with inheritance
class AdminUser < User
attr_accessor :permissions
def initialize(attributes = {})
Tags
CodeAnalysisaitmplclaude-code-templates