Module: ExplicitActiveRecord::NoDBAccess

Extended by:
T::Sig
Defined in:
lib/explicit_activerecord/no_db_access.rb

Defined Under Namespace

Classes: DbAccessError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.in_a_no_db_access_block?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/explicit_activerecord/no_db_access.rb', line 15

def self.in_a_no_db_access_block?
  (@pure_function_block_counter || 0) > 0
end

.no_db_accessObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/explicit_activerecord/no_db_access.rb', line 20

def self.no_db_access
  unless @subscribed
    ActiveSupport::Notifications.subscribe('sql.active_record') do |_name, _start, _finish, _id, _payload|
      if NoDBAccess.in_a_no_db_access_block?
        @should_raise = true
      end
    end
    @subscribed = true
  end

  @pure_function_block_counter = (@pure_function_block_counter || 0) + 1
  yield

  if @should_raise
    @should_raise = false
    raise DbAccessError.new('Cannot execute sql within a no_db_access block.')
  end
ensure
  @should_raise = false
  @pure_function_block_counter -= 1
end

Instance Method Details

#no_db_access(&block) ⇒ Object



43
44
45
# File 'lib/explicit_activerecord/no_db_access.rb', line 43

def no_db_access(&block)
  NoDBAccess.no_db_access(&block)
end