Class: ActionPolicy::Policy::PreCheck::Check

Inherits:
Object
  • Object
show all
Defined in:
lib/action_policy/policy/pre_check.rb

Overview

Single pre-check instance.

Implements filtering logic.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policy, name, except: nil, only: nil) ⇒ Check

Returns a new instance of Check.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/action_policy/policy/pre_check.rb', line 36

def initialize(policy, name, except: nil, only: nil)
  if !except.nil? && !only.nil?
    raise ArgumentError,
      "Only one of `except` and `only` may be specified for pre-check"
  end

  @policy_class = policy
  @name = name
  @blacklist = Array(except) unless except.nil?
  @whitelist = Array(only) unless only.nil?

  rebuild_filter
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



34
35
36
# File 'lib/action_policy/policy/pre_check.rb', line 34

def name
  @name
end

#policy_classObject (readonly)

Returns the value of attribute policy_class.



34
35
36
# File 'lib/action_policy/policy/pre_check.rb', line 34

def policy_class
  @policy_class
end

Instance Method Details

#applicable?(rule) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/action_policy/policy/pre_check.rb', line 50

def applicable?(rule)
  return true if filter.nil?
  filter.call(rule)
end

#call(policy) ⇒ Object



55
# File 'lib/action_policy/policy/pre_check.rb', line 55

def call(policy) = policy.send(name)

#dupObject

rubocop: enable rubocop: enable



84
85
86
87
88
89
90
91
# File 'lib/action_policy/policy/pre_check.rb', line 84

def dup
  self.class.new(
    policy_class,
    name,
    except: blacklist&.dup,
    only: whitelist&.dup
  )
end

#skip!(except: nil, only: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/action_policy/policy/pre_check.rb', line 57

def skip!(except: nil, only: nil)
  if !except.nil? && !only.nil?
    raise ArgumentError,
      "Only one of `except` and `only` may be specified when skipping pre-check"
  end

  if except.nil? && only.nil?
    raise ArgumentError,
      "At least one of `except` and `only` must be specified when skipping pre-check"
  end

  if except
    @whitelist = Array(except)
    @whitelist -= blacklist if blacklist
    @blacklist = nil
  else
    # only
    @blacklist += Array(only) if blacklist
    @whitelist -= Array(only) if whitelist
    @blacklist = Array(only) if filter.nil?
  end

  rebuild_filter
end