Sha256: 1a7094d5911d2999573eda128eaefb53f01c6f4239d941ae8817e83b680b1bf6

Contents?: true

Size: 1.16 KB

Versions: 6

Compression:

Stored size: 1.16 KB

Contents

module Doorkeeper
  module OAuth
    class Scopes
      include Enumerable
      include Comparable

      def self.from_string(string)
        string ||= ''
        new.tap do |scope|
          scope.add(*string.split)
        end
      end

      def self.from_array(array)
        new.tap do |scope|
          scope.add(*array)
        end
      end

      delegate :each, :empty?, to: :@scopes

      def initialize
        @scopes = []
      end

      def exists?(scope)
        @scopes.include? scope.to_s
      end

      def add(*scopes)
        @scopes.push(*scopes.map(&:to_s))
        @scopes.uniq!
      end

      def all
        @scopes
      end

      def to_s
        @scopes.join(' ')
      end

      def has_scopes?(scopes)
        scopes.all? { |s| exists?(s) }
      end

      def +(other)
        if other.is_a? Scopes
          self.class.from_array(all + other.all)
        else
          super(other)
        end
      end

      def <=>(other)
        map(&:to_s).sort <=> other.map(&:to_s).sort
      end

      def &(other)
        other_array = other.present? ? other.all : []
        self.class.from_array(all & other_array)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
doorkeeper-4.2.6 lib/doorkeeper/oauth/scopes.rb
doorkeeper-4.2.5 lib/doorkeeper/oauth/scopes.rb
doorkeeper-4.2.0 lib/doorkeeper/oauth/scopes.rb
doorkeeper-4.1.0 lib/doorkeeper/oauth/scopes.rb
doorkeeper-4.0.0 lib/doorkeeper/oauth/scopes.rb
doorkeeper-4.0.0.rc4 lib/doorkeeper/oauth/scopes.rb