Class: Rails::Worktrees::DatabaseConfigUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/worktrees/database_config_updater.rb

Overview

Safely patches common database.yml layouts for worktree suffixes.

Defined Under Namespace

Classes: Result

Constant Summary collapse

SUFFIX_TEMPLATE =
"<%= ENV.fetch('WORKTREE_DATABASE_SUFFIX', '') %>".freeze
SUPPORTED_ENVIRONMENTS =
%w[development test].freeze
DATABASE_LINE_PATTERN =
/\A(\s*database:\s*)(.+?)(\s*(?:#.*)?\n?)\z/
SECTION_PATTERN =
/\A([A-Za-z0-9_]+):(?:\s|$)/

Instance Method Summary collapse

Constructor Details

#initialize(content:) ⇒ DatabaseConfigUpdater

Returns a new instance of DatabaseConfigUpdater.



16
17
18
# File 'lib/rails/worktrees/database_config_updater.rb', line 16

def initialize(content:)
  @content = content
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/rails/worktrees/database_config_updater.rb', line 20

def call
  state = { found: Hash.new(0), patched: 0, unsupported: [], environment: nil }
  lines = @content.lines.map.with_index { |line, index| patch_line(line, index, state) }

  Result.new(
    lines.join,
    state[:patched].positive?,
    build_messages(state[:found], state[:patched], state[:unsupported])
  )
end