Class: Vangrail::Grant

Inherits:
Object
  • Object
show all
Defined in:
lib/vangrail/plan.rb

Overview

One scoped authority in a Plan.

Constant Summary collapse

EFFECTS =
%i[read write external].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tool:, effect:, arguments: {}, sinks: nil, uses: nil, after: nil, integrity: nil, confirm: false, transaction: false, conversation_id: nil, id: nil) ⇒ Grant

Returns a new instance of Grant.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vangrail/plan.rb', line 16

def initialize(tool:, effect:, arguments: {}, sinks: nil, uses: nil, after: nil,
               integrity: nil, confirm: false, transaction: false,
               conversation_id: nil, id: nil)
  @tool = tool.to_sym
  @effect = effect.to_sym
  raise ArgumentError, "effect must be one of #{EFFECTS.join(', ')}" unless EFFECTS.include?(@effect)
  if !uses.nil? && (!uses.is_a?(Integer) || !uses.positive?)
    raise ArgumentError, 'uses must be a positive integer or nil'
  end

  @id = (id || SecureRandom.uuid).to_s.freeze
  @arguments = immutable_hash(arguments)
  @sinks = normalize_optional(sinks)
  @uses = uses
  @after = normalize(after)
  @integrity = normalize_optional(integrity)
  @confirmation_required = confirm.equal?(true)
  @transaction_required = transaction.equal?(true)
  @conversation_id = conversation_id&.to_s&.freeze
  freeze
end

Instance Attribute Details

#afterObject (readonly)

Returns the value of attribute after.



13
14
15
# File 'lib/vangrail/plan.rb', line 13

def after
  @after
end

#argumentsObject (readonly)

Returns the value of attribute arguments.



13
14
15
# File 'lib/vangrail/plan.rb', line 13

def arguments
  @arguments
end

#conversation_idObject (readonly)

Returns the value of attribute conversation_id.



13
14
15
# File 'lib/vangrail/plan.rb', line 13

def conversation_id
  @conversation_id
end

#effectObject (readonly)

Returns the value of attribute effect.



13
14
15
# File 'lib/vangrail/plan.rb', line 13

def effect
  @effect
end

#idObject (readonly)

Returns the value of attribute id.



13
14
15
# File 'lib/vangrail/plan.rb', line 13

def id
  @id
end

#integrityObject (readonly)

Returns the value of attribute integrity.



13
14
15
# File 'lib/vangrail/plan.rb', line 13

def integrity
  @integrity
end

#sinksObject (readonly)

Returns the value of attribute sinks.



13
14
15
# File 'lib/vangrail/plan.rb', line 13

def sinks
  @sinks
end

#toolObject (readonly)

Returns the value of attribute tool.



13
14
15
# File 'lib/vangrail/plan.rb', line 13

def tool
  @tool
end

#usesObject (readonly)

Returns the value of attribute uses.



13
14
15
# File 'lib/vangrail/plan.rb', line 13

def uses
  @uses
end

Instance Method Details

#confirmation_required?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/vangrail/plan.rb', line 38

def confirmation_required?
  @confirmation_required
end

#to_hObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vangrail/plan.rb', line 46

def to_h
  {
    'id' => id,
    'tool' => tool.to_s,
    'effect' => effect.to_s,
    'arguments' => arguments.transform_values { |constraint| describe(constraint) },
    'sinks' => sinks&.map(&:to_s),
    'uses' => uses,
    'after' => after.map(&:to_s),
    'integrity' => integrity&.map(&:to_s),
    'confirm' => confirmation_required?,
    'transaction' => transaction_required?,
    'conversation_id' => conversation_id,
  }.compact
end

#transaction_required?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/vangrail/plan.rb', line 42

def transaction_required?
  @transaction_required
end