Class: RosettAi::Composition::ScopeResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/rosett_ai/composition/scope_resolver.rb

Overview

Resolves the scope hierarchy for behaviour files. Scopes are ordered: global > organisation > project > local. Later scopes override earlier ones at equal priority.

Constant Summary collapse

SCOPE_ORDER =

Ordered scope names from lowest to highest precedence.

['global', 'organisation', 'project', 'local'].freeze
SCOPE_WEIGHTS =

Numeric weight for each scope (higher = takes precedence).

SCOPE_ORDER.each_with_index.to_h { |scope, i| [scope, i] }.freeze

Instance Method Summary collapse

Instance Method Details

#overrides?(scope_a, scope_b) ⇒ Boolean

Compares two scopes. Returns true if scope_a overrides scope_b.

Parameters:

  • scope_a (String)

    candidate overriding scope

  • scope_b (String)

    candidate overridden scope

Returns:

  • (Boolean)


50
51
52
# File 'lib/rosett_ai/composition/scope_resolver.rb', line 50

def overrides?(scope_a, scope_b)
  weight(scope_a) > weight(scope_b)
end

#resolve(file_path, project_root: nil) ⇒ String

Resolves the scope for a given behaviour file path.

Parameters:

  • file_path (String, Pathname)

    path to the behaviour file

  • project_root (Pathname, nil) (defaults to: nil)

    detected project root

Returns:

  • (String)

    one of SCOPE_ORDER values



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rosett_ai/composition/scope_resolver.rb', line 23

def resolve(file_path, project_root: nil)
  path = file_path.to_s

  if project_root && path.start_with?(project_root.join('.rosett-ai', 'conf', 'behaviour').to_s)
    'project'
  elsif path.include?('/organisation/') || path.include?('/org/')
    'organisation'
  elsif path.include?('/local/')
    'local'
  else
    'global'
  end
end

#weight(scope) ⇒ Integer

Returns the numeric weight for a scope name.

Parameters:

  • scope (String)

    scope name

Returns:

  • (Integer)

    weight (higher = overrides lower)



41
42
43
# File 'lib/rosett_ai/composition/scope_resolver.rb', line 41

def weight(scope)
  SCOPE_WEIGHTS.fetch(scope, 0)
end