Class: DeclarativePolicy::PolicyDsl

Inherits:
Object
  • Object
show all
Defined in:
lib/declarative_policy/policy_dsl.rb

Overview

The return value of a rule { … } declaration. Can call back to register rules with the containing Policy class (context_class here). See Base.rule

Note that the #policy method just performs an #instance_eval, which is useful for multiple #enable or #prevent calls.

Also provides a #method_missing proxy to the context class’s class methods, so that helper methods can be defined and used in a #policy { … } block.

Instance Method Summary collapse

Constructor Details

#initialize(context_class, rule) ⇒ PolicyDsl

Returns a new instance of PolicyDsl.



15
16
17
18
# File 'lib/declarative_policy/policy_dsl.rb', line 15

def initialize(context_class, rule)
  @context_class = context_class
  @rule = rule
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(msg, *args, &block) ⇒ Object



36
37
38
39
40
# File 'lib/declarative_policy/policy_dsl.rb', line 36

def method_missing(msg, *args, &block)
  return super unless @context_class.respond_to?(msg)

  @context_class.__send__(msg, *args, &block) # rubocop: disable GitlabSecurity/PublicSend
end

Instance Method Details

#enable(*abilities) ⇒ Object



24
25
26
# File 'lib/declarative_policy/policy_dsl.rb', line 24

def enable(*abilities)
  @context_class.enable_when(abilities, @rule)
end

#policy(&block) ⇒ Object



20
21
22
# File 'lib/declarative_policy/policy_dsl.rb', line 20

def policy(&block)
  instance_eval(&block)
end

#prevent(*abilities) ⇒ Object



28
29
30
# File 'lib/declarative_policy/policy_dsl.rb', line 28

def prevent(*abilities)
  @context_class.prevent_when(abilities, @rule)
end

#prevent_allObject



32
33
34
# File 'lib/declarative_policy/policy_dsl.rb', line 32

def prevent_all
  @context_class.prevent_all_when(@rule)
end

#respond_to_missing?(msg) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/declarative_policy/policy_dsl.rb', line 42

def respond_to_missing?(msg)
  @context_class.respond_to?(msg) || super
end