Class: PadlockAuth::Config::Scopes

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/padlock_auth/config/scopes.rb

Overview

Represents a collection of scopes.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScopes

Initialize a new Scopes instance.



38
39
40
# File 'lib/padlock_auth/config/scopes.rb', line 38

def initialize
  @scopes = []
end

Class Method Details

.from_array(*array) ⇒ PadlockAuth::Config::Scopes

Create a new Scopes instance from an array.

Parameters:

  • array (Array<String>)

    An array of scopes

Returns:



29
30
31
32
33
# File 'lib/padlock_auth/config/scopes.rb', line 29

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

.from_string(string) ⇒ PadlockAuth::Config::Scopes

Create a new Scopes instance from a string.

Parameters:

  • string (String)

    A space-separated string of scopes

Returns:



16
17
18
19
20
21
# File 'lib/padlock_auth/config/scopes.rb', line 16

def self.from_string(string)
  string ||= ""
  new.tap do |scope|
    scope.add(*string.split(/\s+/))
  end
end

Instance Method Details

#&(other) ⇒ Object

Returns a scopes array with elements contained in both collections.

Parameters:



108
109
110
# File 'lib/padlock_auth/config/scopes.rb', line 108

def &(other)
  self.class.from_array(all & to_array(other))
end

#+(other) ⇒ Object

Adds two collections of scopes together.



88
89
90
# File 'lib/padlock_auth/config/scopes.rb', line 88

def +(other)
  self.class.from_array(all + to_array(other))
end

#<=>(other) ⇒ Object

Compares two collections of scopes.

Parameters:



96
97
98
99
100
101
102
# File 'lib/padlock_auth/config/scopes.rb', line 96

def <=>(other)
  if other.respond_to?(:map)
    map(&:to_s).sort <=> other.map(&:to_s).sort
  else
    super
  end
end

#add(*scopes) ⇒ Object

Add a scope to the collection.

Parameters:

  • scopes (Array<String>)

    The scopes to add



56
57
58
59
# File 'lib/padlock_auth/config/scopes.rb', line 56

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

#allArray<String>

Returns all scopes in the collection.

Returns:

  • (Array<String>)

    All scopes



64
65
66
# File 'lib/padlock_auth/config/scopes.rb', line 64

def all
  @scopes
end

#exists?(scope) ⇒ Boolean

Check if a scope exists in the collection.

Parameters:

  • scope (String)

    The scope to check

Returns:

  • (Boolean)

    True if the scope exists



48
49
50
# File 'lib/padlock_auth/config/scopes.rb', line 48

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

#scopes?(scopes) ⇒ Boolean Also known as: has_scopes?

Returns true if all scopes exist in the collection.

Parameters:

  • scopes (Array<String>)

    The scopes to check

Returns:

  • (Boolean)

    True if all scopes exist



80
81
82
# File 'lib/padlock_auth/config/scopes.rb', line 80

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

#to_sString

Returns all scopes as a string.

Returns:

  • (String)

    All scopes as a space-joined string



71
72
73
# File 'lib/padlock_auth/config/scopes.rb', line 71

def to_s
  @scopes.join(" ")
end