Class: Railstart::CommandBuilder

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

Overview

Translates configuration and user answers into a rails new command string.

This class provides deterministic, side-effect-free command construction.

Examples:

Build a command from answers

config = { "questions" => [{ "id" => "database", "type" => "select", "rails_flag" => "--database=%{value}" }] }
answers = { "database" => "postgresql" }
Railstart::CommandBuilder.build("blog", config, answers)
# => "rails new blog --database=postgresql"

Class Method Summary collapse

Class Method Details

.add_flags(flags, source, value) ⇒ Object (private)



76
77
78
79
80
81
82
83
# File 'lib/railstart/command_builder.rb', line 76

def add_flags(flags, source, value)
  flag_list = source["rails_flags"] || [source["rails_flag"]].compact

  flag_list.each do |flag|
    interpolated = Config.interpolate_flag(flag, value)
    flags << interpolated
  end
end

.build(app_name, config, answers) ⇒ String

Build a rails new command string using config metadata and answers.

Examples:

Railstart::CommandBuilder.build("todo", config, answers)

Parameters:

  • app_name (String)

    target Rails app name

  • config (Hash)

    merged configuration from Railstart::Config.load

  • answers (Hash)

    user answers keyed by question id

Returns:

  • (String)

    fully assembled CLI command

Raises:



25
26
27
28
# File 'lib/railstart/command_builder.rb', line 25

def build(app_name, config, answers)
  flags = collect_flags(config["questions"], answers)
  "rails new #{app_name} #{flags.join(" ")}".strip
end

.collect_flags(questions, answers) ⇒ Object (private)



32
33
34
35
36
37
38
39
40
41
# File 'lib/railstart/command_builder.rb', line 32

def collect_flags(questions, answers)
  flags = []
  Array(questions).each do |question|
    answer = answers[question["id"]]
    next unless answer

    process_question_flags(flags, question, answer)
  end
  flags
end

.process_multi_select(flags, question, answer) ⇒ Object (private)



68
69
70
71
72
73
74
# File 'lib/railstart/command_builder.rb', line 68

def process_multi_select(flags, question, answer)
  Array(question["choices"]).each do |choice|
    next unless Array(answer).include?(choice["value"])

    add_flags(flags, choice, choice["value"])
  end
end

.process_question_flags(flags, question, answer) ⇒ Object (private)



43
44
45
46
47
48
49
50
51
52
# File 'lib/railstart/command_builder.rb', line 43

def process_question_flags(flags, question, answer)
  case question["type"]
  when "select"
    process_select(flags, question, answer)
  when "yes_no", "input"
    add_flags(flags, question, answer)
  when "multi_select"
    process_multi_select(flags, question, answer)
  end
end

.process_select(flags, question, answer) ⇒ Object (private)



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/railstart/command_builder.rb', line 54

def process_select(flags, question, answer)
  # Check if the selected choice has a choice-level rails_flag
  selected_choice = Array(question["choices"]).find { |choice| choice["value"] == answer }

  if selected_choice && (selected_choice["rails_flag"] || selected_choice["rails_flags"])
    # Use choice-level flag
    add_flags(flags, selected_choice, answer)
  elsif question["rails_flag"] || question["rails_flags"]
    # Fall back to question-level flag
    add_flags(flags, question, answer)
  end
  # If neither exists, no flag is added (e.g., for choices that don't need flags)
end