strategies.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # encoding: utf-8
  2. module Warden
  3. module Strategies
  4. class << self
  5. # Add a strategy and store it in a hash.
  6. def add(label, strategy = nil, &block)
  7. strategy ||= Class.new(Warden::Strategies::Base)
  8. strategy.class_eval(&block) if block_given?
  9. unless strategy.method_defined?(:authenticate!)
  10. raise NoMethodError, "authenticate! is not declared in the #{label.inspect} strategy"
  11. end
  12. unless strategy.ancestors.include?(Warden::Strategies::Base)
  13. raise "#{label.inspect} is not a #{base}"
  14. end
  15. _strategies[label] = strategy
  16. end
  17. # Update a previously given strategy.
  18. def update(label, &block)
  19. strategy = _strategies[label]
  20. raise "Unknown strategy #{label.inspect}" unless strategy
  21. add(label, strategy, &block)
  22. end
  23. # Provides access to strategies by label
  24. # :api: public
  25. def [](label)
  26. _strategies[label]
  27. end
  28. # Clears all declared.
  29. # :api: public
  30. def clear!
  31. _strategies.clear
  32. end
  33. # :api: private
  34. def _strategies
  35. @strategies ||= {}
  36. end
  37. end # << self
  38. end # Strategies
  39. end # Warden