Class: PadlockAuth::Config::Scopes
- Inherits:
-
Object
- Object
- PadlockAuth::Config::Scopes
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/padlock_auth/config/scopes.rb
Overview
Represents a collection of scopes.
Class Method Summary collapse
-
.from_array(*array) ⇒ PadlockAuth::Config::Scopes
Create a new Scopes instance from an array.
-
.from_string(string) ⇒ PadlockAuth::Config::Scopes
Create a new Scopes instance from a string.
Instance Method Summary collapse
-
#&(other) ⇒ Object
Returns a scopes array with elements contained in both collections.
-
#+(other) ⇒ Object
Adds two collections of scopes together.
-
#<=>(other) ⇒ Object
Compares two collections of scopes.
-
#add(*scopes) ⇒ Object
Add a scope to the collection.
-
#all ⇒ Array<String>
Returns all scopes in the collection.
-
#exists?(scope) ⇒ Boolean
Check if a scope exists in the collection.
-
#initialize ⇒ Scopes
constructor
Initialize a new Scopes instance.
-
#scopes?(scopes) ⇒ Boolean
(also: #has_scopes?)
Returns true if all scopes exist in the collection.
-
#to_s ⇒ String
Returns all scopes as a string.
Constructor Details
#initialize ⇒ Scopes
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.
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.
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.
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.
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.
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 |
#all ⇒ Array<String>
Returns all scopes in the collection.
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.
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.
80 81 82 |
# File 'lib/padlock_auth/config/scopes.rb', line 80 def scopes?(scopes) scopes.all? { |scope| exists?(scope) } end |
#to_s ⇒ String
Returns all scopes as a string.
71 72 73 |
# File 'lib/padlock_auth/config/scopes.rb', line 71 def to_s @scopes.join(" ") end |