Sha256: 8d10bf0a3dc393f7c277bc6fcefbf1eccbbb01e9366dec81945d58d3f4de585d

Contents?: true

Size: 1.14 KB

Versions: 82

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module ActiveSupport
  module ExecutionContext # :nodoc:
    @after_change_callbacks = []
    class << self
      def after_change(&block)
        @after_change_callbacks << block
      end

      # Updates the execution context. If a block is given, it resets the provided keys to their
      # previous value once the block exits.
      def set(**options)
        options.symbolize_keys!
        keys = options.keys

        store = self.store

        previous_context = keys.zip(store.values_at(*keys)).to_h

        store.merge!(options)
        @after_change_callbacks.each(&:call)

        if block_given?
          begin
            yield
          ensure
            store.merge!(previous_context)
            @after_change_callbacks.each(&:call)
          end
        end
      end

      def []=(key, value)
        store[key.to_sym] = value
        @after_change_callbacks.each(&:call)
      end

      def to_h
        store.dup
      end

      def clear
        store.clear
      end

      private
        def store
          IsolatedExecutionState[:active_support_execution_context] ||= {}
        end
    end
  end
end

Version data entries

82 entries across 78 versions & 11 rubygems

Version Path
activesupport-7.2.1 lib/active_support/execution_context.rb
activesupport-7.2.0 lib/active_support/execution_context.rb
activesupport-7.2.0.rc1 lib/active_support/execution_context.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/execution_context.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/execution_context.rb
activesupport-7.2.0.beta3 lib/active_support/execution_context.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/execution_context.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/execution_context.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/execution_context.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/execution_context.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/execution_context.rb
activesupport-7.2.0.beta2 lib/active_support/execution_context.rb
activesupport-7.1.3.4 lib/active_support/execution_context.rb
activesupport-7.0.8.4 lib/active_support/execution_context.rb
activesupport-7.2.0.beta1 lib/active_support/execution_context.rb
activesupport-7.1.3.2 lib/active_support/execution_context.rb
activesupport-7.1.3.1 lib/active_support/execution_context.rb
activesupport-7.0.8.1 lib/active_support/execution_context.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3/lib/active_support/execution_context.rb
activesupport-7.1.3 lib/active_support/execution_context.rb