Sha256: fc41e24ad3aa381a51d9de9414e23c5c32041624d9fb930b3d231075c5069af3

Contents?: true

Size: 1.23 KB

Versions: 5

Compression:

Stored size: 1.23 KB

Contents

module Roles
  module Base
    attr_accessor :orm_name

    def valid_roles_are(*role_list)
      strategy_class.valid_roles = role_list.to_symbols
    end

    def valid_roles
      strategy_class.valid_roles
    end

    def roles(*roles)
      strategy_class.valid_roles = Array[*roles].flatten.map { |r| r.to_sym }
    end
    
    def role_strategy strategy_name, options=nil
      include_strategy orm_name, strategy_name, options
    end  
         
    def include_strategy orm, strategy_name, options=nil 
      begin     
        module_name = "RoleStrategy::#{orm_name.to_s.camelize}::#{strategy_name.to_s.camelize}"
        constant = module_name.constantize

        strategy_class_method = %Q{
          def strategy_class
            #{constant} 
          end
        }
        
        class_eval do
          eval strategy_class_method
        end

        instance_eval do        
          eval strategy_class_method
          include constant
        end        
      rescue
        raise "No Role strategy module for ORM #{orm.to_s.camelize} for the strategy #{strategy_name}. Module #{module_name} has not been registered"
      end
      constant.apply_options(options) if constant.respond_to? :apply_options
    end    
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
roles_generic-0.2.7 lib/roles_generic/base.rb
roles_generic-0.2.6 lib/roles_generic/base.rb
roles_generic-0.2.5 lib/roles_generic/base.rb
roles_generic-0.2.4 lib/roles_generic/base.rb
roles_generic-0.2.3 lib/roles_generic/base.rb