Class: Antigravity::Policy::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/antigravity/policy.rb

Overview


Rule — a single allow/deny/confirm entry in a policy.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, tool_name = nil, condition: nil, handler: nil) ⇒ Rule

Returns a new instance of Rule.



41
42
43
44
45
46
# File 'lib/antigravity/policy.rb', line 41

def initialize(action, tool_name = nil, condition: nil, handler: nil)
  @action = action
  @tool_name = tool_name
  @condition = condition
  @handler = handler
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



39
40
41
# File 'lib/antigravity/policy.rb', line 39

def action
  @action
end

#conditionObject (readonly)

Returns the value of attribute condition.



39
40
41
# File 'lib/antigravity/policy.rb', line 39

def condition
  @condition
end

#handlerObject (readonly)

Returns the value of attribute handler.



39
40
41
# File 'lib/antigravity/policy.rb', line 39

def handler
  @handler
end

#tool_nameObject (readonly)

Returns the value of attribute tool_name.



39
40
41
# File 'lib/antigravity/policy.rb', line 39

def tool_name
  @tool_name
end

Instance Method Details

#matches?(tool, args) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/antigravity/policy.rb', line 48

def matches?(tool, args)
  return false if @tool_name && @tool_name.to_sym != tool.to_sym
  return false if @condition && !@condition.call(name: tool, args: args)
  true
end

#precedenceObject

Precedence order (higher = wins):

  1. Tool specificity: Specific tool > Wildcard (nil)
  2. Condition specificity: Has predicate > No predicate
  3. Action restrictiveness: Deny > Confirm > Allow


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/antigravity/policy.rb', line 58

def precedence
  specificity = @tool_name ? 1 : 0
  condition_score = @condition ? 1 : 0
  action_score = case @action
                 when :deny then 3
                 when :confirm then 2
                 when :allow then 1
                 else 0
                 end
  [specificity, condition_score, action_score]
end