Class: Charming::Generators::ModelGenerator

Inherits:
AppFileGenerator show all
Defined in:
lib/charming/generators/model_generator.rb

Overview

ModelGenerator implements ‘charming generate model NAME [name:type …]`. Writes an ActiveRecord model class, a `Create<Table>` migration (with one column per supplied field), and a baseline spec. Requires the app to have been generated with `–database sqlite3`.

Defined Under Namespace

Classes: Field

Constant Summary collapse

VALID_TYPES =

The set of ActiveRecord column types accepted on the command line.

%w[string text integer float decimal boolean date datetime time].freeze

Instance Method Summary collapse

Constructor Details

#initialize(name, args, out:, destination:, force: false) ⇒ ModelGenerator

name is the resource name. args is the list of ‘name:type` field specifications.



17
18
19
20
# File 'lib/charming/generators/model_generator.rb', line 17

def initialize(name, args, out:, destination:, force: false)
  super
  @fields = args.map { |arg| parse_field(arg) }
end

Instance Method Details

#generateObject

Validates that the app is database-configured, then writes the model, migration, and spec files.

Raises:



24
25
26
27
28
29
30
# File 'lib/charming/generators/model_generator.rb', line 24

def generate
  raise Error, "Database support is not configured. Generate the app with --database sqlite3 first." unless database_configured?

  create_file(model_path, model)
  create_file(migration_path, migration)
  create_file(spec_path, spec)
end