Class: CreateRailsApp::CommandBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/create_rails_app/command_builder.rb

Overview

Converts an app name, Rails version, and option hash into a rails new command array.

Examples:

CommandBuilder.new.build(
  app_name: 'myapp', rails_version: '8.1.2',
  options: { api: true, database: 'postgresql' }
)
#=> ['rails', '_8.1.2_', 'new', 'myapp', '--api', '--database=postgresql']

Instance Method Summary collapse

Instance Method Details

#build(app_name:, rails_version: nil, options: {}, minimal: false) ⇒ Array<String>

Builds the rails new command array.

Parameters:

  • app_name (String)
  • rails_version (String, nil) (defaults to: nil)

    exact version for version pinning (nil omits pin)

  • options (Hash{Symbol => Object}) (defaults to: {})
  • minimal (Boolean) (defaults to: false)

    pass --minimal flag

Returns:

  • (Array<String>)


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/create_rails_app/command_builder.rb', line 21

def build(app_name:, rails_version: nil, options: {}, minimal: false)
  command = ['rails']
  command << "_#{rails_version}_" if rails_version
  command.push('new', app_name)
  command << '--minimal' if minimal

  Options::Catalog::ORDER.each do |key|
    next unless options.key?(key)

    append_option!(command, key, options[key])
  end
  command
end