Class: Spree::Authentication::StrategyRegistry
- Inherits:
-
Object
- Object
- Spree::Authentication::StrategyRegistry
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- app/models/spree/authentication/strategy_registry.rb
Overview
Keyed registry of authentication strategy classes for the Store and Admin APIs.
Strategies are dispatched by the ‘provider` value the client sends to the auth endpoint, so the registry is a key → class map. The `:email` key is reserved for the built-in Spree::Authentication::Strategies::EmailPasswordStrategy; integrators can override it by adding a different class under the same key.
Instance Method Summary collapse
-
#[](key) ⇒ Class?
Look up a registered strategy class.
-
#add(key, strategy_class) ⇒ Class
Register a strategy class under the given provider key.
-
#initialize(strategies = {}) ⇒ StrategyRegistry
constructor
A new instance of StrategyRegistry.
-
#key?(key) ⇒ Boolean
Whether a strategy is registered under the given provider key.
-
#remove(key) ⇒ Class?
Unregister a strategy.
-
#to_h ⇒ Hash{Symbol => Class}
A shallow copy of the underlying map.
Constructor Details
#initialize(strategies = {}) ⇒ StrategyRegistry
Returns a new instance of StrategyRegistry.
26 27 28 29 |
# File 'app/models/spree/authentication/strategy_registry.rb', line 26 def initialize(strategies = {}) @strategies = {} strategies.each { |key, klass| add(key, klass) } end |
Instance Method Details
#[](key) ⇒ Class?
Look up a registered strategy class.
54 55 56 |
# File 'app/models/spree/authentication/strategy_registry.rb', line 54 def [](key) @strategies[key.to_sym] end |
#add(key, strategy_class) ⇒ Class
Register a strategy class under the given provider key. Overwrites any existing entry for that key.
38 39 40 |
# File 'app/models/spree/authentication/strategy_registry.rb', line 38 def add(key, strategy_class) @strategies[key.to_sym] = strategy_class end |
#key?(key) ⇒ Boolean
Whether a strategy is registered under the given provider key.
62 63 64 |
# File 'app/models/spree/authentication/strategy_registry.rb', line 62 def key?(key) @strategies.key?(key.to_sym) end |
#remove(key) ⇒ Class?
Unregister a strategy. Idempotent — returns ‘nil` if the key is not present.
46 47 48 |
# File 'app/models/spree/authentication/strategy_registry.rb', line 46 def remove(key) @strategies.delete(key.to_sym) end |
#to_h ⇒ Hash{Symbol => Class}
Returns a shallow copy of the underlying map.
67 68 69 |
# File 'app/models/spree/authentication/strategy_registry.rb', line 67 def to_h @strategies.dup end |