Module: ActionPolicy::Policy::Scoping::ClassMethods

Defined in:
lib/action_policy/policy/scoping.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#scope_for(type, name = :default, &block) ⇒ Object

Register a new scoping method for the ‘type`



116
117
118
119
120
121
122
# File 'lib/action_policy/policy/scoping.rb', line 116

def scope_for(type, name = :default, &block)
  mid = :"__scoping__#{type}__#{name}"

  define_method(mid, &block)

  scoping_handlers[type][name] = mid
end

#scope_matcher(type, class_or_proc) ⇒ Object

Define scope type matcher.

Scope matcher is an object that implements ‘#===` (_case equality_) or a Proc.

When no type is provided when applying a scope we try to infer a type from the target object by calling matchers one by one until we find a matching type (i.e. there is a matcher which returns ‘true` when applying it to the target).



144
145
146
# File 'lib/action_policy/policy/scoping.rb', line 144

def scope_matcher(type, class_or_proc)
  scope_matchers << [type, class_or_proc]
end

#scope_matchersObject



148
149
150
151
152
153
154
155
156
# File 'lib/action_policy/policy/scoping.rb', line 148

def scope_matchers
  return @scope_matchers if instance_variable_defined?(:@scope_matchers)

  @scope_matchers = if superclass.respond_to?(:scope_matchers)
    superclass.scope_matchers.dup
  else
    []
  end
end

#scoping_handlersObject



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/action_policy/policy/scoping.rb', line 124

def scoping_handlers
  return @scoping_handlers if instance_variable_defined?(:@scoping_handlers)

  @scoping_handlers =
    Hash.new { |h, k| h[k] = {} }.tap do |handlers|
      if superclass.respond_to?(:scoping_handlers)
        superclass.scoping_handlers.each do |k, v|
          handlers[k] = v.dup
        end
      end
    end
end