Class: Reeve::Authorization::Declaration

Inherits:
Object
  • Object
show all
Defined in:
lib/reeve/authorization/declaration.rb

Overview

What guard_with records for one tool class: which policy governs it, which action to check, and which of its arguments never reach the ledger in the clear.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tool_class:, policy:, action:, redacted_arguments: []) ⇒ Declaration

Returns a new instance of Declaration.



10
11
12
13
14
15
# File 'lib/reeve/authorization/declaration.rb', line 10

def initialize(tool_class:, policy:, action:, redacted_arguments: [])
  @tool_class = tool_class
  @policy = policy
  @action = action.to_sym
  @redacted_arguments = redacted_arguments.map(&:to_sym).freeze
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



8
9
10
# File 'lib/reeve/authorization/declaration.rb', line 8

def action
  @action
end

#policyObject (readonly)

Returns the value of attribute policy.



8
9
10
# File 'lib/reeve/authorization/declaration.rb', line 8

def policy
  @policy
end

#redacted_argumentsObject (readonly)

Returns the value of attribute redacted_arguments.



8
9
10
# File 'lib/reeve/authorization/declaration.rb', line 8

def redacted_arguments
  @redacted_arguments
end

#tool_classObject (readonly)

Returns the value of attribute tool_class.



8
9
10
# File 'lib/reeve/authorization/declaration.rb', line 8

def tool_class
  @tool_class
end

Instance Method Details

#for_subclass(subclass) ⇒ Object



32
33
34
35
36
37
# File 'lib/reeve/authorization/declaration.rb', line 32

def for_subclass(subclass)
  self.class.new(
    tool_class: subclass, policy: policy, action: action,
    redacted_arguments: redacted_arguments
  )
end

#policy_nameObject



39
40
41
# File 'lib/reeve/authorization/declaration.rb', line 39

def policy_name
  policy.respond_to?(:name) && policy.name ? policy.name : policy.class.name
end

#tool_nameObject

Resolved on first use rather than at construction: guard_with runs inside the class body, and a class defined as Class.new { ... } has no name until the constant it is assigned to exists.



20
21
22
# File 'lib/reeve/authorization/declaration.rb', line 20

def tool_name
  @tool_name ||= derive_tool_name
end

#with(policy: self.policy, action: self.action, redacted_arguments: self.redacted_arguments) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/reeve/authorization/declaration.rb', line 24

def with(policy: self.policy, action: self.action,
         redacted_arguments: self.redacted_arguments)
  self.class.new(
    tool_class: tool_class, policy: policy, action: action,
    redacted_arguments: redacted_arguments
  )
end