Class: Vangrail::Plan
- Inherits:
-
Object
- Object
- Vangrail::Plan
- Defined in:
- lib/vangrail/plan.rb
Overview
Privileged, lockable collection of grants for one conversation.
Instance Attribute Summary collapse
-
#audit ⇒ Object
readonly
Returns the value of attribute audit.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
Instance Method Summary collapse
- #external(tool, **options) ⇒ Object
- #grant_for(tool) ⇒ Object
- #grants ⇒ Object
- #grants_for(tool) ⇒ Object
-
#initialize(id: nil, audit: nil) ⇒ Plan
constructor
A new instance of Plan.
- #lock! ⇒ Object
- #locked? ⇒ Boolean
- #read(tool, **options) ⇒ Object
- #to_h ⇒ Object
- #write(tool, **options) ⇒ Object
Constructor Details
#initialize(id: nil, audit: nil) ⇒ Plan
Returns a new instance of Plan.
104 105 106 107 108 109 110 |
# File 'lib/vangrail/plan.rb', line 104 def initialize(id: nil, audit: nil) @id = (id || SecureRandom.uuid).to_s.freeze @audit = audit || AuditLog.new @grants = [] @locked = false @audit.record(:plan_created, plan_id: @id) end |
Instance Attribute Details
#audit ⇒ Object (readonly)
Returns the value of attribute audit.
102 103 104 |
# File 'lib/vangrail/plan.rb', line 102 def audit @audit end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
102 103 104 |
# File 'lib/vangrail/plan.rb', line 102 def id @id end |
Class Method Details
.from_allow(allow, tools:, id: nil, audit: nil) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/vangrail/plan.rb', line 154 def self.from_allow(allow, tools:, id: nil, audit: nil) new(id: id, audit: audit).tap do |plan| allow.each do |tool, origins| next unless tools.readonly?(tool) allowed = Array(origins).map(&:to_sym) arguments = if allowed.empty? {} else { value: allowed.one? ? allowed.first : allowed } end plan.read(tool, arguments: arguments) end end end |
Instance Method Details
#external(tool, **options) ⇒ Object
120 121 122 |
# File 'lib/vangrail/plan.rb', line 120 def external(tool, **) add(tool, :external, **) end |
#grant_for(tool) ⇒ Object
133 134 135 |
# File 'lib/vangrail/plan.rb', line 133 def grant_for(tool) grants_for(tool).first end |
#grants ⇒ Object
124 125 126 |
# File 'lib/vangrail/plan.rb', line 124 def grants @grants.dup.freeze end |
#grants_for(tool) ⇒ Object
128 129 130 131 |
# File 'lib/vangrail/plan.rb', line 128 def grants_for(tool) name = tool.to_sym @grants.select { |grant| grant.tool == name }.freeze end |
#lock! ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/vangrail/plan.rb', line 137 def lock! return self if locked? @grants.freeze @locked = true audit.record(:plan_locked, plan_id: id, grants: @grants.map(&:id)) self end |
#locked? ⇒ Boolean
146 147 148 |
# File 'lib/vangrail/plan.rb', line 146 def locked? @locked end |
#read(tool, **options) ⇒ Object
112 113 114 |
# File 'lib/vangrail/plan.rb', line 112 def read(tool, **) add(tool, :read, **) end |
#to_h ⇒ Object
150 151 152 |
# File 'lib/vangrail/plan.rb', line 150 def to_h { 'id' => id, 'locked' => locked?, 'grants' => @grants.map(&:to_h) } end |
#write(tool, **options) ⇒ Object
116 117 118 |
# File 'lib/vangrail/plan.rb', line 116 def write(tool, **) add(tool, :write, **) end |