Sha256: 492f5b5441f8343445919781ec2cad908adbeb8d4c5c25b1e312f1b01501051a

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module ActionPolicy
  module Ext
    # Adds #_policy_cache_key method to Object,
    # which just call #policy_cache_key or #cache_key
    # or #object_id (if `use_object_id` parameter is set to true).
    #
    # For other core classes returns string representation.
    #
    # Raises ArgumentError otherwise.
    module PolicyCacheKey
      refine Object do
        def _policy_cache_key(use_object_id: false)
          return policy_cache_key if respond_to?(:policy_cache_key)
          return cache_key if respond_to?(:cache_key)

          return object_id if use_object_id == true

          raise ArgumentError, "object is not cacheable"
        end
      end

      refine NilClass do
        def _policy_cache_key(*)
          ""
        end
      end

      refine TrueClass do
        def _policy_cache_key(*)
          "t"
        end
      end

      refine FalseClass do
        def _policy_cache_key(*)
          "f"
        end
      end

      refine String do
        def _policy_cache_key(*)
          self
        end
      end

      refine Symbol do
        def _policy_cache_key(*)
          to_s
        end
      end

      refine Numeric do
        def _policy_cache_key(*)
          to_s
        end
      end

      refine Time do
        def _policy_cache_key(*)
          to_s
        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/ext/policy_cache_key.rb
action_policy-0.1.3 lib/action_policy/ext/policy_cache_key.rb
action_policy-0.1.2 lib/action_policy/ext/policy_cache_key.rb
action_policy-0.1.1 lib/action_policy/ext/policy_cache_key.rb
action_policy-0.1.0 lib/action_policy/ext/policy_cache_key.rb