debian-mirror-gitlab/lib/declarative_policy/rule_dsl.rb

48 lines
946 B
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-05-09 12:01:36 +05:30
module DeclarativePolicy
# The DSL evaluation context inside rule { ... } blocks.
# Responsible for creating and combining Rule objects.
#
# See Base.rule
class RuleDsl
def initialize(context_class)
@context_class = context_class
end
def can?(ability)
Rule::Ability.new(ability)
end
def all?(*rules)
Rule::And.make(rules)
end
def any?(*rules)
Rule::Or.make(rules)
end
def none?(*rules)
~Rule::Or.new(rules)
end
def cond(condition)
Rule::Condition.new(condition)
end
def delegate(delegate_name, condition)
Rule::DelegatedCondition.new(delegate_name, condition)
end
2018-11-18 11:00:15 +05:30
def method_missing(msg, *args)
return super unless args.empty? && !block_given?
2018-05-09 12:01:36 +05:30
2018-11-18 11:00:15 +05:30
if @context_class.delegations.key?(msg)
DelegateDsl.new(self, msg)
2018-05-09 12:01:36 +05:30
else
2018-11-18 11:00:15 +05:30
cond(msg.to_sym)
2018-05-09 12:01:36 +05:30
end
end
end
end