Module: Hadley::Authz::StrategyBuilder
- Included in:
- Strategy
- Defined in:
- lib/hadley/authz/strategy_builder.rb
Overview
This mixin module provides helpful utilties for generating new authorization strategies based on configuration provided when the strategy is being registered with Warden.
Instance Method Summary collapse
-
#build(name, config) ⇒ Object
Builds a new authorization strategy class based on the provided configuration.
-
#create_strategy(name) ⇒ Class
protected
Creates the strategy class based on the provided name.
-
#register_strategy(name, strategy) ⇒ Object
protected
Registers the new authorization strategy with Warden under the specified name prefixed by ‘afid_’.
-
#set_config(strategy, config) ⇒ Object
protected
Binds the configuration information to the newly created strategy class.
Instance Method Details
#build(name, config) ⇒ Object
Builds a new authorization strategy class based on the provided configuration.
9 10 11 12 13 |
# File 'lib/hadley/authz/strategy_builder.rb', line 9 def build(name, config) strategy = self.create_strategy(name) self.register_strategy(name, strategy) self.set_config(strategy, config) end |
#create_strategy(name) ⇒ Class (protected)
Creates the strategy class based on the provided name. The class will be namespaced under the class that these methods are mixed into.
23 24 25 26 27 28 29 30 |
# File 'lib/hadley/authz/strategy_builder.rb', line 23 def create_strategy(name) class_name = Hadley::Utils.camelize(name.to_s) if self.const_defined?(class_name) self.const_get(class_name) else self.const_set(class_name, Class.new(self)) end end |
#register_strategy(name, strategy) ⇒ Object (protected)
Registers the new authorization strategy with Warden under the specified name prefixed by ‘afid_’.
36 37 38 39 40 41 |
# File 'lib/hadley/authz/strategy_builder.rb', line 36 def register_strategy(name, strategy) full_name = "afid_#{name}".to_sym if Warden::Strategies[full_name].nil? Warden::Strategies.add(full_name, strategy) end end |
#set_config(strategy, config) ⇒ Object (protected)
Binds the configuration information to the newly created strategy class.
47 48 49 |
# File 'lib/hadley/authz/strategy_builder.rb', line 47 def set_config(strategy, config) strategy.const_set("CONFIG", config) unless strategy.const_defined?("CONFIG") end |