Class: Mistri::Workspace::ActiveRecord
- Inherits:
-
Object
- Object
- Mistri::Workspace::ActiveRecord
- Defined in:
- lib/mistri/workspace/active_record.rb
Overview
Documents in the host's own database, through a model class the host supplies, optionally scoped (per session, per tenant). Not auto-required; load it with require "mistri/workspace/active_record".
The model needs a path column and a content column, unique on path within the scope. A migration to copy:
create_table :mistri_documents do |t|
t.string :session_id, null: false
t.string :path, null: false, limit: 512
t.text :content, size: :medium, null: false
t.
end
add_index :mistri_documents, [:session_id, :path], unique: true
Instance Method Summary collapse
- #delete(path) ⇒ Object
-
#initialize(model, scope: {}) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
- #list(prefix = nil) ⇒ Object
- #read(path) ⇒ Object
- #write(path, content) ⇒ Object
Constructor Details
#initialize(model, scope: {}) ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
20 21 22 23 |
# File 'lib/mistri/workspace/active_record.rb', line 20 def initialize(model, scope: {}) @model = model @scope = scope end |
Instance Method Details
#delete(path) ⇒ Object
36 37 38 39 |
# File 'lib/mistri/workspace/active_record.rb', line 36 def delete(path) @model.where(**@scope, path: path.to_s).delete_all nil end |
#list(prefix = nil) ⇒ Object
41 42 43 44 |
# File 'lib/mistri/workspace/active_record.rb', line 41 def list(prefix = nil) paths = @model.where(**@scope).pluck(:path).sort prefix ? paths.select { |p| p.start_with?(prefix.to_s) } : paths end |
#read(path) ⇒ Object
25 26 27 |
# File 'lib/mistri/workspace/active_record.rb', line 25 def read(path) @model.where(**@scope, path: path.to_s).pick(:content) end |
#write(path, content) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/mistri/workspace/active_record.rb', line 29 def write(path, content) record = @model.find_or_initialize_by(**@scope, path: path.to_s) record.content = content.to_s record.save! nil end |