Module: Roles::Base

Defined in:
lib/roles_generic/base.rb,
lib/roles_generic/roles_base.rb

Defined Under Namespace

Classes: RoleStrategyId

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#orm_nameObject

Returns the value of attribute orm_name.



3
4
5
# File 'lib/roles_generic/base.rb', line 3

def orm_name
  @orm_name
end

#role_strategyObject (readonly)

Returns the value of attribute role_strategy.



5
6
7
# File 'lib/roles_generic/base.rb', line 5

def role_strategy
  @role_strategy
end

Instance Method Details

#include_strategy(orm, strategy_name, options = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/roles_generic/base.rb', line 60

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
    @role_strategy = RoleStrategyId.new strategy_name
    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

#roles(*roles) ⇒ Object



26
27
28
# File 'lib/roles_generic/base.rb', line 26

def roles(*roles)
  strategy_class.valid_roles = Array[*roles].flatten.map { |r| r.to_sym }
end

#set_role_strategy(strategy_name, options = nil) ⇒ Object



30
31
32
# File 'lib/roles_generic/base.rb', line 30

def set_role_strategy strategy_name, options=nil
  include_strategy orm_name, strategy_name, options
end

#valid_role?(role) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/roles_generic/base.rb', line 11

def valid_role? role
  strategy_class.valid_roles.include? role.to_sym
end

#valid_rolesObject



22
23
24
# File 'lib/roles_generic/base.rb', line 22

def valid_roles
  strategy_class.valid_roles
end

#valid_roles?(*role_names) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
# File 'lib/roles_generic/base.rb', line 15

def valid_roles? *role_names
  role_names.each do |role|
    return false if !strategy_class.valid_roles.include? role.to_sym
  end
  true
end

#valid_roles_are(*role_list) ⇒ Object



7
8
9
# File 'lib/roles_generic/base.rb', line 7

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