Class: Vangrail::Plan

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

Overview

Privileged, lockable collection of grants for one conversation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#auditObject (readonly)

Returns the value of attribute audit.



102
103
104
# File 'lib/vangrail/plan.rb', line 102

def audit
  @audit
end

#idObject (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, **options)
  add(tool, :external, **options)
end

#grant_for(tool) ⇒ Object



133
134
135
# File 'lib/vangrail/plan.rb', line 133

def grant_for(tool)
  grants_for(tool).first
end

#grantsObject



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

Returns:

  • (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, **options)
  add(tool, :read, **options)
end

#to_hObject



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, **options)
  add(tool, :write, **options)
end