Permalink
Browse files

Moving Strategy class building to it's own module

  • Loading branch information...
1 parent 9fa66ff commit f122544d556d537f9d4b7495a51fa63e6d713623 @roman committed Sep 16, 2009
Showing with 85 additions and 75 deletions.
  1. +4 −3 lib/warden_oauth.rb
  2. +1 −72 lib/warden_oauth/strategy.rb
  3. +80 −0 lib/warden_oauth/strategy_builder.rb
View
@@ -8,9 +8,10 @@ module OAuth
base_path = File.dirname(__FILE__) + "/warden_oauth"
require base_path + "/errors"
- autoload :Utils, base_path + '/utils'
- autoload :Strategy, base_path + '/strategy'
- autoload :Config, base_path + "/config"
+ autoload :Utils, base_path + '/utils'
+ autoload :StrategyBuilder, base_path + '/strategy_builder'
+ autoload :Strategy, base_path + '/strategy'
+ autoload :Config, base_path + "/config"
require base_path + "/manager"
@@ -2,78 +2,7 @@ module Warden
module OAuth
class Strategy < Warden::Strategies::Base
-
- ############################################
- ### section: Setup and Subclass Creation ###
- ############################################
-
- #
- # Manages the creation and registration of the OAuth strategy specified
- # on the keyword
- #
- # @param [Symbol] name of the oauth service
- # @param [Walruz::Config] configuration specified on the declaration of the oauth service
- #
- def self.build(keyword, config)
- strategy_class = self.create_oauth_strategy_class(keyword)
- self.register_oauth_strategy_class(keyword, strategy_class)
- self.set_oauth_service_info(strategy_class, config)
- end
-
- #
- # Creates the OAuth Strategy class from the keyword specified on the declaration of the
- # oauth service. This class will be namespaced inside Warden::OAuth::Strategy
- #
- # @param [Symbol] name of the OAuth service
- # @return [Class] The class representing the Warden strategy
- #
- # @example
- #
- # self.create_oauth_strategy_class(:twitter) #=> Warden::OAuth::Strategy::Twitter
- # # will create a class Warden::OAuth::Strategy::Twitter that extends from
- # # Warden::OAuth::Strategy
- #
- def self.create_oauth_strategy_class(keyword)
- class_name = Warden::OAuth::Utils.camelize(keyword.to_s)
- if self.const_defined?(class_name)
- self.const_get(class_name)
- else
- self.const_set(class_name, Class.new(self))
- end
- end
-
- #
- # Registers the generated OAuth Strategy in the Warden::Strategies collection, the label
- # of the strategy will be the given oauth service name plus an '_oauth' postfix
- #
- # @param [Symbol] name of the OAuth service
- #
- # @example
- # manager.oauth(:twitter) { |twitter| ... } # will register a strategy :twitter_oauth
- #
- def self.register_oauth_strategy_class(keyword, strategy_class)
- keyword_name = "%s_oauth" % keyword.to_s
- if Warden::Strategies[keyword_name.to_sym].nil?
- Warden::Strategies.add(keyword_name.to_sym, strategy_class)
- end
- end
-
- #
- # Defines a CONFIG constant in the generated class that will hold the configuration information
- # (consumer_token, consumer_secret and options) of the oauth service.
- #
- # @param [Class] strategy class that will hold the configuration info
- # @param [Warden::OAuth::Config] configuration info of the oauth service
- #
- def self.set_oauth_service_info(strategy_class, config)
- strategy_class.const_set("CONFIG", config) unless strategy_class.const_defined?("CONFIG")
- end
-
- class << self
- protected :create_oauth_strategy_class,
- :register_oauth_strategy_class,
- :set_oauth_service_info
- end
+ extend StrategyBuilder
######################
### Strategy Logic ###
@@ -0,0 +1,80 @@
+module Warden
+ module OAuth
+
+ #
+ # Handles the creation an registration of OAuth strategies based on configuration parameters
+ # via the Warden::Manager.oauth method
+ #
+ module StrategyBuilder
+ extend self
+
+ #
+ # Manages the creation and registration of the OAuth strategy specified
+ # on the keyword
+ #
+ # @param [Symbol] name of the oauth service
+ # @param [Walruz::Config] configuration specified on the declaration of the oauth service
+ #
+ def build(keyword, config)
+ strategy_class = self.create_oauth_strategy_class(keyword)
+ self.register_oauth_strategy_class(keyword, strategy_class)
+ self.set_oauth_service_info(strategy_class, config)
+ end
+
+ #
+ # Creates the OAuth Strategy class from the keyword specified on the declaration of the
+ # oauth service. This class will be namespaced inside Warden::OAuth::Strategy
+ #
+ # @param [Symbol] name of the OAuth service
+ # @return [Class] The class representing the Warden strategy
+ #
+ # @example
+ #
+ # self.create_oauth_strategy_class(:twitter) #=> Warden::OAuth::Strategy::Twitter
+ # # will create a class Warden::OAuth::Strategy::Twitter that extends from
+ # # Warden::OAuth::Strategy
+ #
+ def create_oauth_strategy_class(keyword)
+ class_name = Warden::OAuth::Utils.camelize(keyword.to_s)
+ if self.const_defined?(class_name)
+ self.const_get(class_name)
+ else
+ self.const_set(class_name, Class.new(self))
+ end
+ end
+
+ #
+ # Registers the generated OAuth Strategy in the Warden::Strategies collection, the label
+ # of the strategy will be the given oauth service name plus an '_oauth' postfix
+ #
+ # @param [Symbol] name of the OAuth service
+ #
+ # @example
+ # manager.oauth(:twitter) { |twitter| ... } # will register a strategy :twitter_oauth
+ #
+ def register_oauth_strategy_class(keyword, strategy_class)
+ keyword_name = "%s_oauth" % keyword.to_s
+ if Warden::Strategies[keyword_name.to_sym].nil?
+ Warden::Strategies.add(keyword_name.to_sym, strategy_class)
+ end
+ end
+
+ #
+ # Defines a CONFIG constant in the generated class that will hold the configuration information
+ # (consumer_token, consumer_secret and options) of the oauth service.
+ #
+ # @param [Class] strategy class that will hold the configuration info
+ # @param [Warden::OAuth::Config] configuration info of the oauth service
+ #
+ def set_oauth_service_info(strategy_class, config)
+ strategy_class.const_set("CONFIG", config) unless strategy_class.const_defined?("CONFIG")
+ end
+
+ protected :create_oauth_strategy_class,
+ :register_oauth_strategy_class,
+ :set_oauth_service_info
+
+ end
+
+ end
+end

0 comments on commit f122544

Please sign in to comment.