Sha256: 12c2c197125b3007f49548b093a7dbe76016938866e1363e359ea54a8a6ce43c

Contents?: true

Size: 936 Bytes

Versions: 5

Compression:

Stored size: 936 Bytes

Contents

# frozen_string_literal: true

module ActionPolicy # :nodoc:
  module Policy
    module Rails
      # Add ActiveSupport::Notifications support.
      #
      # Fires `action_policy.apply_rule` event on every `#apply` call.
      # Fires `action_policy.init` event on every policy initialization.
      module Instrumentation
        INIT_EVENT_NAME = "action_policy.init"
        APPLY_EVENT_NAME = "action_policy.apply_rule"

        def initialize(*)
          event = {policy: self.class.name}
          ActiveSupport::Notifications.instrument(INIT_EVENT_NAME, event) { super }
        end

        def apply(rule)
          event = {policy: self.class.name, rule: rule.to_s}
          ActiveSupport::Notifications.instrument(APPLY_EVENT_NAME, event) do
            res = super
            event[:cached] = result.cached?
            event[:value] = result.value
            res
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
action_policy-0.4.4 lib/action_policy/rails/policy/instrumentation.rb
action_policy-0.4.3 lib/action_policy/rails/policy/instrumentation.rb
action_policy-0.4.2 lib/action_policy/rails/policy/instrumentation.rb
action_policy-0.4.1 lib/action_policy/rails/policy/instrumentation.rb
action_policy-0.4.0 lib/action_policy/rails/policy/instrumentation.rb