Skip to content

Instantly share code, notes, and snippets.

@IdahoEv
Created April 17, 2014 22:10
Show Gist options
  • Save IdahoEv/11014423 to your computer and use it in GitHub Desktop.
Save IdahoEv/11014423 to your computer and use it in GitHub Desktop.
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
class Role::Consumer < Role
Role.register 'Consumer', self
end
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