Class: CommandTower::Configuration::AdminScope::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/command_tower/configuration/admin_scope/config.rb

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



10
11
12
13
# File 'lib/command_tower/configuration/admin_scope/config.rb', line 10

def initialize
  @registrations = {}
  @finalized = false
end

Instance Method Details

#fetch(tool_id) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/command_tower/configuration/admin_scope/config.rb', line 31

def fetch(tool_id)
  normalized = normalize_tool_id!(tool_id)
  registration = @registrations[normalized]
  if registration.nil?
    raise CommandTower::AdminScope::UnregisteredToolError,
      "admin scope registration for #{normalized} is not registered"
  end

  registration
end

#finalize!Object



52
53
54
55
56
# File 'lib/command_tower/configuration/admin_scope/config.rb', line 52

def finalize!
  @registrations.freeze
  @finalized = true
  self
end

#finalized?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/command_tower/configuration/admin_scope/config.rb', line 58

def finalized?
  @finalized
end

#register(tool_id) {|registration| ... } ⇒ Object

Yields:

  • (registration)

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/command_tower/configuration/admin_scope/config.rb', line 15

def register(tool_id, &block)
  raise CommandTower::AdminScope::FrozenRegistryError, "admin scope registry is frozen" if @finalized

  normalized = normalize_tool_id!(tool_id)
  if @registrations.key?(normalized)
    raise CommandTower::AdminScope::DuplicateRegistrationError,
      "admin scope registration for #{normalized} already exists"
  end

  registration = ToolRegistration.new
  yield registration if block_given?
  registration.validate!(tool_id: normalized)
  @registrations[normalized] = registration
  registration
end

#registered?(tool_id) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/command_tower/configuration/admin_scope/config.rb', line 42

def registered?(tool_id)
  @registrations.key?(normalize_tool_id!(tool_id))
rescue CommandTower::AdminWorkspace::InvalidToolNameError
  false
end

#registrationsObject



48
49
50
# File 'lib/command_tower/configuration/admin_scope/config.rb', line 48

def registrations
  @registrations.dup.freeze
end

#reset_registrations!Object



76
77
78
79
80
# File 'lib/command_tower/configuration/admin_scope/config.rb', line 76

def reset_registrations!
  thaw_for_test!
  @registrations.clear
  self
end

#validate_scoped_tools!Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/command_tower/configuration/admin_scope/config.rb', line 62

def validate_scoped_tools!
  CommandTower.config.registry.admin_workspace.definitions.each do |tool_id, definition|
    next unless definition.scope_required?

    unless registered?(tool_id)
      raise CommandTower::AdminScope::MissingRegistrationError,
        "admin workspace tool #{tool_id} requires scope but has no admin_scope registration"
    end

    fetch(tool_id).validate!(tool_id:)
  end
  self
end