Class: Gruf::Authentication::Strategies

Inherits:
Object
  • Object
show all
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

Class Method Details

.[](label) ⇒ Gruf::Authentication::Base

Return a strategy via a hash accessor syntax

Parameters:

  • label (Symbol)

    The name of the strategy

Returns:



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

Parameters:

  • name (String)

    The vanity name of the strategy being loaded

  • strategy (Class|NilClass) (defaults to: nil)

    (Optional) The class that represents the strategy

  • &block (Proc)

    If given, will attempt to build a strategy class from the base class with this block

Returns:

  • (Class)

    The loaded strategy

Raises:

  • (NoMethodError)


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

Returns:

  • (Boolean)

    True if there are loaded strategies



83
84
85
# File 'lib/gruf/authentication/strategies.rb', line 83

def any?
  to_h.keys.count > 0
end

.clearHash

Clear all given strategies

Returns:

  • (Hash)

    The newly empty hash



92
93
94
# File 'lib/gruf/authentication/strategies.rb', line 92

def clear
  @strategies = {}
end

.eachObject

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_hHash<Class>

Return the loaded strategies as a hash

Returns:

  • (Hash<Class>)

    A name/strategy pair of loaded strategies



74
75
76
# File 'lib/gruf/authentication/strategies.rb', line 74

def to_h
  _strategies
end