Created
April 17, 2014 22:10
-
-
Save IdahoEv/11014423 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
module ClassRegistry | |
module ClassMethods | |
def registry | |
@registry ||= {} | |
end | |
def register(role_name, klass=self) | |
registry[role_name] = klass | |
end | |
def registry_key | |
matches = registry.select{ |key, val| val = self.class}.keys | |
end | |
def users | |
User.where(:role_name => registry_key) | |
end | |
end | |
def self.included(base) | |
base.extend(ClassMethods) | |
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 Role::Consumer < Role | |
Role.register 'Consumer', self | |
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
require 'class_registry' | |
class Role | |
include ClassRegistry | |
def self.for(user) | |
registry[user.role_name].new.tap do |role| | |
role.user = user | |
end | |
end | |
def role_name | |
user.role_name | |
end | |
attr_accessor :user | |
Dir[File.dirname(__FILE__) + '/role/*.rb'].each { |file| require file } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment