class ObjectFramework::Persistence::Session
Owns an identity map and unit of work for one active transaction. Managed entities stay inside the invocation; return independent values to callers. Application#invoke flushes and closes its session automatically.
Attributes
Public Class Methods
Source
# File lib/object_framework/persistence/session.rb, line 74 def initialize(transaction:, mappings:) @transaction, @mappings = transaction, mappings @identities, @work = IdentityMap.new, UnitOfWork.new(transaction:) @closed, @removed = false, {} end
Public Instance Methods
Source
# File lib/object_framework/persistence/session.rb, line 83 def add(entity) active! mapping = @mappings.fetch(entity.class) unless mapping.identity.reader.call(entity) mapping.identity.writer.call(entity, SecureRandom.uuid) end if @removed.key?([entity.class, mapping.identity.reader.call(entity)]) raise InvalidInput, "Removed entities cannot be registered again" end @identities.add(entity.class, mapping.identity.reader.call(entity), entity) @work.track(entity, mapping:, baseline: nil, state: :new) entity end
Tracks a new mapped entity, assigning a UUID when its identity is nil. Returns the entity. Duplicate identities and previously removed entities cannot be registered as new objects in this session.
Source
# File lib/object_framework/persistence/session.rb, line 142 def close @closed = true @identities.clear @work.clear @removed.clear end
Source
# File lib/object_framework/persistence/session.rb, line 100 def find(type, id, load: []) active! raise NotFound, "Entity was removed" if @removed.key?([type, id]) entity = @identities.find(type, id) unless entity mapping = @mappings.fetch(type) row = transaction.find(mapping.table, id) raise NotFound, "Entity not found" unless row entity = attach(mapping, row) end load_associations(entity, load) entity end
Finds a mapped entity by identity or raises NotFound. Repeated lookups return the same object. load explicitly selects associations, with nested hashes for nested loading; no implicit lazy loading occurs.
Source
# File lib/object_framework/persistence/session.rb, line 137 def flush active! @work.flush end
Writes tracked inserts, changes, and deletions without committing. Managed identities cannot change. Requires an active session.
Source
# File lib/object_framework/persistence/session.rb, line 116 def query(type, query = Query.new, load: []) active! mapping = @mappings.fetch(type) transaction.query(query.for_table(mapping.table)).reject { |row| @removed.key?([type, row[:id]]) }.map do |row| entity = @identities.find(type, row[:id]) || attach(mapping, row) load_associations(entity, load) entity end end
Returns managed entities matching an immutable Query and optional association loading plan. Removed identities are excluded.
Source
# File lib/object_framework/persistence/session.rb, line 127 def remove(entity) active! id = @work.entry(entity).identity @work.remove(entity) @removed[[entity.class, id]] = true @identities.remove(entity.class, id) end
Schedules deletion of a managed entity, or discards a pending insert.