You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
911 B

  1. # frozen_string_literal: true
  2. class ScopeTransformer < Parslet::Transform
  3. class Scope
  4. DEFAULT_TERM = 'all'
  5. DEFAULT_ACCESS = %w(read write).freeze
  6. attr_reader :namespace, :term
  7. def initialize(scope)
  8. @namespace = scope[:namespace]&.to_s
  9. @access = scope[:access] ? [scope[:access].to_s] : DEFAULT_ACCESS.dup
  10. @term = scope[:term]&.to_s || DEFAULT_TERM
  11. end
  12. def key
  13. @key ||= [@namespace, @term].compact.join('/')
  14. end
  15. def access
  16. @access.join('/')
  17. end
  18. def merge(other_scope)
  19. clone.merge!(other_scope)
  20. end
  21. def merge!(other_scope)
  22. raise ArgumentError unless other_scope.namespace == namespace && other_scope.term == term
  23. @access.concat(other_scope.instance_variable_get('@access'))
  24. @access.uniq!
  25. @access.sort!
  26. self
  27. end
  28. end
  29. rule(scope: subtree(:scope)) { Scope.new(scope) }
  30. end