Class: Apartment::Privileges::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/apartment/privileges/context.rb

Overview

What a tenant_privilege_policy receives. One instance per phase.

Deliberately not Data.define, though Data is house style for value objects (Migrator::Result, PoolObserver::Sample). This is not a value object: it carries a live connection, which is mutable and meaningless outside the invocation, and Data would give it value equality, hashing and positional decomposition as public semantics. Data also cannot deliver the additive-only promise below — appending a member adds a required positional argument and a required keyword to .new, and Data responds to #deconstruct, so an adopter constructing a context in a unit test or destructuring it positionally would break on a minor release.

Additive-only: new fields arrive as keyword arguments with defaults, and unknown keywords are ignored, so a policy reading attributes off a context Apartment handed it keeps working. Construction is the gem's business.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tenant:, container_name:, connection:, db_role:, phase:) ⇒ Context

Returns a new instance of Context.

Parameters:

  • tenant (String)

    the tenant name as Apartment knows it

  • container_name (String)

    the physical schema or database to address

  • connection (ActiveRecord::ConnectionAdapters::AbstractAdapter)

    valid for this invocation only; do not retain it

  • db_role (String, nil)

    the executing database role, nil where the engine has no role system

  • phase (Symbol)

    :before_schema_load or :after_schema_load



30
31
32
33
34
35
36
37
# File 'lib/apartment/privileges/context.rb', line 30

def initialize(tenant:, container_name:, connection:, db_role:, phase:, **)
  @tenant = tenant
  @container_name = container_name
  @connection = connection
  @db_role = db_role
  @phase = phase
  freeze
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



21
22
23
# File 'lib/apartment/privileges/context.rb', line 21

def connection
  @connection
end

#container_nameObject (readonly)

Returns the value of attribute container_name.



21
22
23
# File 'lib/apartment/privileges/context.rb', line 21

def container_name
  @container_name
end

#db_roleObject (readonly)

Returns the value of attribute db_role.



21
22
23
# File 'lib/apartment/privileges/context.rb', line 21

def db_role
  @db_role
end

#phaseObject (readonly)

Returns the value of attribute phase.



21
22
23
# File 'lib/apartment/privileges/context.rb', line 21

def phase
  @phase
end

#tenantObject (readonly)

Returns the value of attribute tenant.



21
22
23
# File 'lib/apartment/privileges/context.rb', line 21

def tenant
  @tenant
end

Instance Method Details

#after_schema_load?Boolean

Returns:

  • (Boolean)


40
# File 'lib/apartment/privileges/context.rb', line 40

def after_schema_load? = phase == :after_schema_load

#before_schema_load?Boolean

Returns:

  • (Boolean)


39
# File 'lib/apartment/privileges/context.rb', line 39

def before_schema_load? = phase == :before_schema_load

#quoted_containerObject

The value every policy needs. Provided so that copied example code quotes by default rather than interpolating a raw identifier.



44
# File 'lib/apartment/privileges/context.rb', line 44

def quoted_container = connection.quote_table_name(container_name)