Created
May 29, 2012 15:44
-
-
Save tal/2829150 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Intended Behavior: | |
ExternalContactList.type_to_class('facebook') # => ExternalContactList::Facebook | |
UserIdentifier.type_to_class('facebook') # => UserIdentifier::Facebook | |
UserIdentifier::Facebook.type_to_class('facebook') # => UserIdentifier::Facebook | |
# Actual Behavior | |
ExternalContactList.type_to_class('facebook') # => UserIdentifier::Facebook | |
UserIdentifier.type_to_class('facebook') # => UserIdentifier::Facebook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module HasProviderSubclass | |
module ClassMethods | |
def type_to_class_string type | |
@@type_to_class_string[type] | |
end | |
def type_to_class type | |
@@type_to_class[type] | |
end | |
private | |
def __setup_provider_variables | |
@@type_to_class_string = Hash.new { |h, type| h[type] = "#{self.to_s}::#{type.capitalize}" } | |
@@type_to_class = Hash.new { |h, cs| h[cs] = type_to_class_string(cs).constantize } | |
end | |
end | |
module InstanceMethods | |
end | |
def self.included(receiver) | |
receiver.extend ClassMethods | |
receiver.send :include, InstanceMethods | |
receiver.send :__setup_provider_variables | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserIdentifier | |
include HasProviderSubclass | |
class Facebook < UserIdentifier | |
end | |
end | |
class ExternalContactList | |
include HasProviderSubclass | |
class Facebook < ExternalContactList | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment