Skip to content
Kward Search API index

Class: Kward::Tools::WorkspaceTargets

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/tools/workspace_targets.rb

Overview

Resolves a small host-scoped set of workspace roles for local tools. Target names are supplied by trusted runtime orchestration, never by the model, so selecting a target cannot escape into an arbitrary filesystem path.

Constant Summary collapse

ACTIVE =
"active"

Instance Method Summary collapse

Constructor Details

#initialize(active:, targets: {}) ⇒ WorkspaceTargets

Returns a new instance of WorkspaceTargets.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kward/tools/workspace_targets.rb', line 12

def initialize(active:, targets: {})
  @workspaces = { ACTIVE => active }
  targets.each do |name, workspace|
    name = name.to_s
    raise ArgumentError, "Workspace target name is required" if name.empty?
    raise ArgumentError, "Workspace target #{ACTIVE.inspect} is reserved" if name == ACTIVE

    @workspaces[name] = workspace
  end
  @workspaces.freeze
end

Instance Method Details

#properties(properties) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kward/tools/workspace_targets.rb', line 41

def properties(properties)
  return properties if @workspaces.length == 1

  properties.merge(
    target: {
      type: "string",
      enum: @workspaces.keys,
      description: "Workspace role. Defaults to active; origin is the host repository worktree."
    }
  )
end

#target_name(args) ⇒ Object



30
31
32
33
34
35
# File 'lib/kward/tools/workspace_targets.rb', line 30

def target_name(args)
  value = if args.respond_to?(:key?)
            args[:target] || args["target"]
          end
  value.to_s.empty? ? ACTIVE : value.to_s
end

#valid?(args) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/kward/tools/workspace_targets.rb', line 37

def valid?(args)
  @workspaces.key?(target_name(args))
end

#without_target(args) ⇒ Object



53
54
55
# File 'lib/kward/tools/workspace_targets.rb', line 53

def without_target(args)
  args.reject { |key, _value| key.to_s == "target" }
end

#workspace_for(args) ⇒ Object



24
25
26
27
28
# File 'lib/kward/tools/workspace_targets.rb', line 24

def workspace_for(args)
  @workspaces.fetch(target_name(args)) do |name|
    raise ArgumentError, "Unknown workspace target: #{name}"
  end
end