module Casbin::Util
Constants
- EVAL_REG
Public Class Methods
removes any duplicated elements in a string array.
# File lib/casbin-ruby/util.rb, line 31 def array_remove_duplicates(arr) arr.uniq end
gets a printable string for a string array.
# File lib/casbin-ruby/util.rb, line 36 def array_to_string(arr) arr.join(', ') end
Escapes the dots in the assertion, because the expression evaluation doesn't support such variable names. Also it replaces attributes with hash syntax (`r.obj.Owner` -> `r_obj`, `r.obj.Owner.Position` -> `r_obj['Position']`), because Keisan functions work in both regular `f(x)` and postfix `x.f()` notation, where for example `a.f(b,c)` is translated internally to `f(a,b,c)` - github.com/project-eutopia/keisan#specifying-functions For now we replace attributes for the request elements like `r.sub`, `r.obj`, `r.act` casbin.org/docs/en/abac#how-to-use-abac We support Unicode in attributes for the compatibility with Golang - golang.org/ref/spec#Identifiers
# File lib/casbin-ruby/util.rb, line 21 def escape_assertion(string) string.gsub(/\br\.(\w+)((?:\.[[:alpha:]_][[:alnum:]_]*)+)/) do |_| param = Regexp.last_match(1) attrs = Regexp.last_match(2)[1..-1]&.split('.')&.map { |attr| "['#{attr}']" } attrs = attrs&.join || '' "r_#{param}#{attrs}" end.gsub('r.', 'r_').gsub('p.', 'p_') end
For now, it does not used. Returns the parameters of function eval.
# File lib/casbin-ruby/util.rb, line 65 def get_eval_value(string) string.scan(EVAL_REG).flatten end
determine whether matcher contains function eval
# File lib/casbin-ruby/util.rb, line 46 def has_eval(string) EVAL_REG.match?(string) end
joins a string and a slice into a new slice.
# File lib/casbin-ruby/util.rb, line 70 def join_slice(a, *b) Array(a).concat b end
gets a printable string for variable number of parameters.
# File lib/casbin-ruby/util.rb, line 41 def params_to_string(*params) params.join(', ') end
removes the comments starting with # in the text.
# File lib/casbin-ruby/util.rb, line 9 def remove_comments(string) string.split('#').first.strip end
replace all occurrences of function eval with rules
# File lib/casbin-ruby/util.rb, line 51 def replace_eval(expr, rules) i = -1 expr.gsub EVAL_REG do |_| i += 1 if rules.is_a? Hash "(#{escape_assertion rules[Regexp.last_match 1]})" else "(#{escape_assertion rules[i]})" end end end
returns the elements in `a` that aren't in `b`.
# File lib/casbin-ruby/util.rb, line 75 def set_subtract(a, b) a - b end