Class: Gruf::Authentication::Strategies
- Inherits:
-
Object
- Object
- Gruf::Authentication::Strategies
- Defined in:
- lib/gruf/authentication/strategies.rb
Overview
Provides a modifiable repository of strategies for authentication
Defined Under Namespace
Classes: StrategyDescendantError
Class Method Summary collapse
-
.[](label) ⇒ Gruf::Authentication::Base
Return a strategy via a hash accessor syntax.
-
.add(name, strategy = nil, &block) ⇒ Class
Add an authentication strategy, either through a class or a block.
-
.any? ⇒ Boolean
Return if there are any loaded strategies.
-
.clear ⇒ Hash
Clear all given strategies.
-
.each ⇒ Object
Iterate over each strategy and yield it to the caller.
-
.to_h ⇒ Hash<Class>
Return the loaded strategies as a hash.
Class Method Details
.[](label) ⇒ Gruf::Authentication::Base
Return a strategy via a hash accessor syntax
56 57 58 |
# File 'lib/gruf/authentication/strategies.rb', line 56 def [](label) _strategies[label.to_sym] end |
.add(name, strategy = nil, &block) ⇒ Class
Add an authentication strategy, either through a class or a block
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/gruf/authentication/strategies.rb', line 37 def add(name, strategy = nil, &block) base = Gruf::Authentication::Base strategy ||= Class.new(base) strategy.class_eval(&block) if block_given? # all strategies require the valid? method raise NoMethodError unless strategy.method_defined?(:valid?) raise StrategyDescendantError, "Strategies must descend from #{base}" unless strategy.ancestors.include?(base) _strategies[name.to_sym] = strategy end |
.any? ⇒ Boolean
Return if there are any loaded strategies
83 84 85 |
# File 'lib/gruf/authentication/strategies.rb', line 83 def any? to_h.keys.count > 0 end |
.clear ⇒ Hash
Clear all given strategies
92 93 94 |
# File 'lib/gruf/authentication/strategies.rb', line 92 def clear @strategies = {} end |
.each ⇒ Object
Iterate over each strategy and yield it to the caller
63 64 65 66 67 |
# File 'lib/gruf/authentication/strategies.rb', line 63 def each _strategies.each do |s| yield s end end |
.to_h ⇒ Hash<Class>
Return the loaded strategies as a hash
74 75 76 |
# File 'lib/gruf/authentication/strategies.rb', line 74 def to_h _strategies end |