1
0

scope_transformer.rb 910 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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