1
0

scope_transformer.rb 1014 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. # # override for profile scope which is read only
  12. @access = %w(read) if @term == 'profile'
  13. end
  14. def key
  15. @key ||= [@namespace, @term].compact.join('/')
  16. end
  17. def access
  18. @access.join('/')
  19. end
  20. def merge(other_scope)
  21. clone.merge!(other_scope)
  22. end
  23. def merge!(other_scope)
  24. raise ArgumentError unless other_scope.namespace == namespace && other_scope.term == term
  25. @access.concat(other_scope.instance_variable_get(:@access))
  26. @access.uniq!
  27. @access.sort!
  28. self
  29. end
  30. end
  31. rule(scope: subtree(:scope)) { Scope.new(scope) }
  32. end