Skip to content

Instantly share code, notes, and snippets.

@tal
Created May 29, 2012 15:44
Show Gist options
  • Save tal/2829150 to your computer and use it in GitHub Desktop.
Save tal/2829150 to your computer and use it in GitHub Desktop.
# 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
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
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