Class: Charming::DatabaseInstaller

Inherits:
Object
  • Object
show all
Defined in:
lib/charming/database_installer.rb

Overview

DatabaseInstaller implements ‘charming db:install sqlite3`. It adds database support to an existing Charming app by creating `config/database.rb`, `app/models/application_record.rb`, `db/migrate/`, and `db/seeds.rb`, and patching the gemspec and root loader to include the new dependencies and the `app/models` autoload directory.

Instance Method Summary collapse

Constructor Details

#initialize(database, out:, destination:) ⇒ DatabaseInstaller

database is the adapter name (only “sqlite3” is currently supported). out is the status-output stream. destination is the app root.



13
14
15
16
17
18
# File 'lib/charming/database_installer.rb', line 13

def initialize(database, out:, destination:)
  @database = database
  @out = out
  @destination = destination
  @app_name = Generators::Name.new(app_name_from_gemspec)
end

Instance Method Details

#installObject

Performs the install: writes the database config, application record, migrate directory, seeds file, and patches the gemspec + root loader. Idempotent: existing files are reported with “exist <path>” instead of being overwritten.

Raises:



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/charming/database_installer.rb', line 23

def install
  raise Generators::Error, "Unsupported database: #{database.inspect}" unless database == "sqlite3"

  create_file("config/database.rb", database_config)
  create_file("app/models/application_record.rb", application_record)
  create_file("db/migrate/.keep", "")
  create_file("db/seeds.rb", %(# frozen_string_literal: true
))
  update_gemspec
  update_root_file
end