Module: Engram::Rails::HasMemory

Defined in:
lib/engram/rails/has_memory.rb

Overview

Class-level macro added to ActiveRecord models.

class User < ApplicationRecord
  has_memory                      # scope => "user:<id>"
end

class Account < ApplicationRecord
  has_memory scope: ->{ "team:#{team_id}" }
end

‘user.memory` returns an Engram::Memory bound to that owner.

Instance Method Summary collapse

Instance Method Details

#has_memory(scope: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/engram/rails/has_memory.rb', line 17

def has_memory(scope: nil)
  scope_proc = scope

  define_method(:memory) do
    key =
      if scope_proc
        instance_exec(&scope_proc)
      else
        "#{self.class.name.underscore}:#{id}"
      end
    Engram::Memory.new(scope: key)
  end
end