Class: SkillBench::Rails::SkillTemplates

Inherits:
Object
  • Object
show all
Defined in:
lib/skill_bench/rails/skill_templates.rb

Overview

Generates Rails-specific skill templates

Class Method Summary collapse

Class Method Details

.active_record_model(name) ⇒ String

Generate an ActiveRecord model template

Parameters:

  • name (String)

    Model name (e.g., 'my_model')

Returns:

  • (String)

    ActiveRecord model class



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/skill_bench/rails/skill_templates.rb', line 83

def self.active_record_model(name)
  class_name = camelize(name)
  <<~RUBY
    # frozen_string_literal: true

    class #{class_name} < ApplicationRecord
      # Validations
      validates :name, presence: true

      # Associations
      # belongs_to :user
      # has_many :items

      # Scopes
      # scope :active, -> { where(active: true) }

      # Instance methods
      # def some_method
      #   ...
      # end

      # Class methods
      # def self.some_class_method
      #   ...
      # end
    end
  RUBY
end

.camelize(name) ⇒ String

Convert a snake_case or kebab-case name to CamelCase.

Replaces ActiveSupport's String#camelize for the scaffold inputs used here: it splits on _ and - separators, upcases the first letter of each segment, and preserves any segment that is already CamelCase.

Examples:

SkillTemplates.camelize('user_creator') # => "UserCreator"
SkillTemplates.camelize('order-service') # => "OrderService"
SkillTemplates.camelize('UserCreator')   # => "UserCreator"

Parameters:

  • name (String)

    snake_case, kebab-case, or already-CamelCase name

Returns:

  • (String)

    CamelCase name



19
20
21
# File 'lib/skill_bench/rails/skill_templates.rb', line 19

def self.camelize(name)
  name.split(/[-_]/).map { |segment| segment.empty? ? segment : segment[0].upcase + segment[1..] }.join
end

.concern(name) ⇒ String

Generate a concern template

Parameters:

  • name (String)

    Concern name (e.g., 'my_concern')

Returns:

  • (String)

    Concern module



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/skill_bench/rails/skill_templates.rb', line 59

def self.concern(name)
  module_name = camelize(name)
  <<~RUBY
    # frozen_string_literal: true

    module #{module_name}
      extend ActiveSupport::Concern

      included do
        # Add class methods, associations, validations here
      end

      class_methods do
        # Add class methods here
      end

      # Add instance methods here
    end
  RUBY
end

.service_object(name) ⇒ String

Generate a service object template

Parameters:

  • name (String)

    Service name (e.g., 'my_service' or 'my-service')

Returns:

  • (String)

    Service object Ruby class



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/skill_bench/rails/skill_templates.rb', line 26

def self.service_object(name)
  class_name = camelize(name)
  <<~RUBY
    # frozen_string_literal: true

    module SkillBench
      module Skills
        class #{class_name}
          # Initialize with required parameters
          # @param args [Hash] Keyword arguments for the service
          def initialize(**args)
            # Set instance variables from args
          end

          # Execute the service
          # @return [Hash] Result with :success and :response keys
          def call
            # Implement service logic here
            { success: true, response: { message: 'Not implemented' } }
          rescue StandardError => e
            Rails.logger.error(e.message)
            Rails.logger.error(e.backtrace.first(5).join("\n"))
            { success: false, response: { error: { message: e.message } } }
          end
        end
      end
    end
  RUBY
end