Class: Shadwire::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/shadwire/installer.rb

Overview

Writes published registry file entries into a consuming Rails app, applying the same safety checks bin/sync_registry enforces. It is non-interactive: the overwrite policy and an optional confirm callable are injected so that commands own the UI and tests stay deterministic.

A file entry is a Hash of the published shape:

{ "target" => "app/.../x.rb", "type" => "component", "content" => "..." }

Instance Method Summary collapse

Constructor Details

#initialize(root, overwrite: :prompt, confirm: nil) ⇒ Installer

Returns a new instance of Installer.

Parameters:

  • root (String, Pathname)

    the consuming app root

  • overwrite (Symbol) (defaults to: :prompt)

    :always, :never, or :prompt

  • confirm (#call, nil) (defaults to: nil)

    called with the target string when :prompt; truthy result overwrites, falsy skips



19
20
21
22
23
# File 'lib/shadwire/installer.rb', line 19

def initialize(root, overwrite: :prompt, confirm: nil)
  @root = Pathname(root).expand_path
  @overwrite = overwrite
  @confirm = confirm
end

Instance Method Details

#install(files) ⇒ Hash

Validates all targets (fail-fast), then writes.

Parameters:

  • files (Array<Hash>)

    flat array of published file entries

Returns:

  • (Hash)

    { written: [target strings], skipped: [target strings] }



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/shadwire/installer.rb', line 28

def install(files)
  planned = validate(files)

  written = []
  skipped = []

  planned.each do |target, content|
    path = @root.join(target)

    if path.exist? && !overwrite?(target)
      skipped << target
      next
    end

    FileUtils.mkdir_p(path.dirname)
    path.write(content)
    written << target
  end

  { written:, skipped: }
end