Ruby Model Generator

Command

by davila7 · Added 5mo ago

Claude

Install

npx claude-code-templates@latest --command=cli-tool/templates/ruby/.claude/commands --yes

Add to Claude

claude mcp add ruby-model-generator

About

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

  1. Creates model classes with proper initialization methods
  2. Adds attribute accessors and instance variables
  3. Includes validation methods and error handling
  4. Follows Ruby conventions and best practices
  5. 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

From Our Store

View all →
Claude Code

Claude Code Power User Kit

$39+

Advanced Claude Code skills and configurations

OpenClaw

OpenClaw Business Starter Kit

$59+

Set Up Your AI Assistant in 60 Minutes