Class: Castkit::Generators::Base Abstract

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/generators/base.rb

Overview

This class is abstract.

Abstract base class for all Castkit generators.

Provides standard behavior for generating a component and optional spec file from ERB templates. Subclasses must define a ‘component` (e.g., `:type`, `:contract`) and may override `config` or `default_values`.

Template variables are injected using the ‘config` hash, which includes:

  • ‘:name` – underscored version of the component name

  • ‘:class_name` – PascalCase version of the component name

Direct Known Subclasses

Contract, DataObject, Plugin, Serializer, Type, Validator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.component(value = nil) ⇒ Symbol

Sets or retrieves the component type (e.g., :type, :data_object).

Parameters:

  • value (Symbol, nil) (defaults to: nil)

Returns:

  • (Symbol)


27
28
29
# File 'lib/generators/base.rb', line 27

def component(value = nil)
  value.nil? ? @component : (@component = value)
end

.source_rootString

Returns the root path to look for templates.

Returns:

  • (String)

    the root path to look for templates



32
33
34
# File 'lib/generators/base.rb', line 32

def source_root
  File.expand_path("templates", __dir__)
end

Instance Method Details

#create_componentvoid

This method returns an undefined value.

Creates the main component file using a template.

Template: ‘component.rb.tt` Target: `lib/castkit/#components/#name.rb`



46
47
48
49
50
51
# File 'lib/generators/base.rb', line 46

def create_component
  template(
    "#{self.class.component}.rb.tt",
    "lib/castkit/#{self.class.component}s/#{config[:name]}.rb", **config
  )
end

#create_specvoid

This method returns an undefined value.

Creates the associated spec file, if enabled.

Template: ‘component_spec.rb.tt` Target: `spec/castkit/#components/#name_spec.rb`



59
60
61
62
63
64
65
66
# File 'lib/generators/base.rb', line 59

def create_spec
  return unless options[:spec]

  template(
    "#{self.class.component}_spec.rb.tt",
    "spec/castkit/#{self.class.component}s/#{config[:name]}_spec.rb", **config
  )
end