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, callable = nil, &block) ⇒ Object

Register a new scoping method for the ‘type`



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

def scope_for(type, name = :default, callable = nil, &block)
  name, callable = prepare_args(name, callable)

  mid = :"__scoping__#{type}__#{name}"
  block = ->(target, **opts) { callable.call(self, target, **opts) } if callable

  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).



147
148
149
# File 'lib/action_policy/policy/scoping.rb', line 147

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

#scope_matchersObject



151
152
153
154
155
156
157
158
159
# File 'lib/action_policy/policy/scoping.rb', line 151

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



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/action_policy/policy/scoping.rb', line 127

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