Class: Dry::Ability::Container

Inherits:
Container
  • Object
show all
Defined in:
lib/dry/ability/container.rb

Constant Summary collapse

MAPPING_NSFN =
{
  :subject => F[:string_tpl, "mappings.subject.%s"].to_proc,
  :action  => F[:string_tpl, "mappings.action.%s"].to_proc
}
RULES_NSFN =
F[:string_tpl, "rules.%s.%s"].to_proc

Instance Method Summary collapse

Instance Method Details

#key_candidates(action, subject) ⇒ Object



33
34
35
36
# File 'lib/dry/ability/container.rb', line 33

def key_candidates(action, subject)
  subject, action = mappings(:subject, subject), mappings(:action, action)
  subject.product(action).map!(&RULES_NSFN)
end

#mappings(kind, key) ⇒ Object



38
39
40
# File 'lib/dry/ability/container.rb', line 38

def mappings(kind, key)
  F.collect_mappings(key, self, MAPPING_NSFN[kind], &:to_s)
end

#resolve_with_mappings(action, subject) {|exception| ... } ⇒ Object

Yield Parameters:

  • exception

    Yields block with an instance of RuleNotDefault exception class



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dry/ability/container.rb', line 17

def resolve_with_mappings(action, subject)
  candidates = key_candidates(action, subject)
  result = []
  candidates.each do |key|
    next unless key?(key)
    result << resolve(key)
  end
  if result.blank?
    exception = RuleNotDefined.new(action: action, subject: subject, candidates: candidates)
    raise exception unless block_given?
    yield(exception)
  else
    result
  end
end