Sha256: c693cea807c1859d2d050825adc29376657e78705d7c110a60ce205d3644fd3d

Contents?: true

Size: 662 Bytes

Versions: 5

Compression:

Stored size: 662 Bytes

Contents

# frozen_string_literal: true

module ActionPolicy
  module Policy
    # Per-policy cache for applied rules.
    #
    # When you call `apply` twice on the same policy and for the same rule,
    # the check (and pre-checks) is only called once.
    module CachedApply
      class << self
        def prepended(base)
          base.prepend InstanceMethods
        end

        alias included prepended
      end

      module InstanceMethods # :nodoc:
        def apply(rule)
          @__rules_cache__ ||= {}
          return @__rules_cache__[rule] if @__rules_cache__.key?(rule)

          @__rules_cache__[rule] = super
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
action_policy-0.1.4 lib/action_policy/policy/cached_apply.rb
action_policy-0.1.3 lib/action_policy/policy/cached_apply.rb
action_policy-0.1.2 lib/action_policy/policy/cached_apply.rb
action_policy-0.1.1 lib/action_policy/policy/cached_apply.rb
action_policy-0.1.0 lib/action_policy/policy/cached_apply.rb