Class: Dry::Ability::RulesBuilder
- Inherits:
-
Object
- Object
- Dry::Ability::RulesBuilder
- Defined in:
- lib/dry/ability/rules_builder.rb
Overview
Creates a container with ability rules and provides DSL to define them.
Instance Method Summary collapse
-
#can(actions, subjects, filter: nil, scope: nil, inverse: false, explicit_scope: true, **constraints, &block) ⇒ Object
Registers rule in the calculated key.
- #cannot(*args, **options, &block) ⇒ Object
-
#map(kind, dict) ⇒ Object
Registers mappings.
-
#map_action(dict) ⇒ Object
Shorthand of
map :action, dict
. -
#map_subject(dict) ⇒ Object
Shorthand of
map :subject, dict
. -
#mixin ⇒ Object
Generates module, which, after being included into a class, registers singleton instance variable
@_container
as reference to the composed container of rules.
Instance Method Details
#can(actions, subjects, filter: nil, scope: nil, inverse: false, explicit_scope: true, **constraints, &block) ⇒ Object
Registers rule in the calculated key
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/dry/ability/rules_builder.rb', line 61 def can(actions, subjects, filter: nil, scope: nil, inverse: false, explicit_scope: true, **constraints, &block) @_container.namespace(:rules) do |_rules| Rule.new(actions, subjects, constraints: constraints, filter: (filter || block&.to_proc), scope: scope, inverse: inverse, explicit_scope: explicit_scope ).register_to(_rules) end end |
#cannot(*args, **options, &block) ⇒ Object
74 75 76 |
# File 'lib/dry/ability/rules_builder.rb', line 74 def cannot(*args, **, &block) can(*args, **, inverse: true, &block) end |
#map(kind, dict) ⇒ Object
Registers mappings
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/dry/ability/rules_builder.rb', line 24 def map(kind, dict) kind = T::ActionOrSubject[kind] dict = T::RulesMapping[dict] @_container.namespace(:mappings) do |_mappings| _mappings.namespace(kind) do |_action_or_subject| dict.each do |mapped, list| list.sort.each do |original| key = _action_or_subject.send(:namespaced, original) pred = Array.wrap(@_container._container.delete(key)&.call) pred << mapped unless pred.include?(mapped) _action_or_subject.register(original, pred) end end end end end |
#map_action(dict) ⇒ Object
Shorthand of map :action, dict
43 44 45 |
# File 'lib/dry/ability/rules_builder.rb', line 43 def map_action(dict) map :action, dict end |
#map_subject(dict) ⇒ Object
Shorthand of map :subject, dict
52 53 54 |
# File 'lib/dry/ability/rules_builder.rb', line 52 def map_subject(dict) map :subject, dict end |
#mixin ⇒ Object
Generates module, which, after being included into a class, registers singleton instance variable @_container
as reference to the composed container of rules.
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/dry/ability/rules_builder.rb', line 80 def mixin @mixin ||= Module.new.tap do |mod| container = @_container.freeze mod.define_singleton_method :included do |base| base.instance_variable_set(:@_container, container) super(base) end mod end end |