Module: Roles::Generic::ClassMethods

Defined in:
lib/roles_generic/generic.rb

Constant Summary collapse

MAP =
{
  :admin_flag   => "attr_accessor :admin_flag",

  :many_roles   => "attr_accessor :many_roles",
  :one_role     => "attr_accessor :one_role",

  :roles_mask   => "attr_accessor :roles_mask",
  :role_string  => "attr_accessor :role_string",
  :role_strings => "attr_accessor :role_strings",
  :roles_string => "attr_accessor :roles_string",
  
  :embed_many_roles   => "attr_accessor :many_roles",
  :embed_one_role     => "attr_accessor :one_role"        
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#role_groupsObject

Returns the value of attribute role_groups.



29
30
31
# File 'lib/roles_generic/generic.rb', line 29

def role_groups
  @role_groups
end

#valid_role_groupsObject

Returns the value of attribute valid_role_groups.



29
30
31
# File 'lib/roles_generic/generic.rb', line 29

def valid_role_groups
  @valid_role_groups
end

Instance Method Details

#add_role_group(group_hash) ⇒ Object

role_group :admin => :admin, :super_admin

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/roles_generic/generic.rb', line 81

def add_role_group(group_hash)
  raise ArgumentError, '#add_role_group must be passed a Hash where the key is the group name' if !group_hash.kind_of? Hash
  raise ArgumentError, '#add_role_group must be a Hash with a single key value pair' if group_hash.size > 1

  # first key/value pair?
  group = group_hash.keys.first
  raise ArgumentError, "Role group identifier must be a String or Symbol " if !group.kind_of_label?      
  raise ArgumentError, "Role group is not valid, must be one of: #{valid_role_groups}"if !valid_role_group? group

  role_list = group_hash.values.first
  hash = {group.to_sym => [role_list].flat_uniq.to_symbols}

  role_groups.merge!(hash)
end

#add_role_groups(groups_hash) ⇒ Object

Raises:

  • (ArgumentError)


73
74
75
76
77
78
# File 'lib/roles_generic/generic.rb', line 73

def add_role_groups(groups_hash)
  raise ArgumentError, "Role group must be passed a Hash where the key is the group name" if !groups_hash.kind_of? Hash
  groups_hash.each_pair do |key, list|
    add_role_group(key => list)
  end
end

#strategy(name, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/roles_generic/generic.rb', line 53

def strategy name, options = {}
  strategy_name = name.to_sym
  raise ArgumentError, "Unknown role strategy #{strategy_name}" if !MAP.keys.include? strategy_name
  use_roles_strategy strategy_name
  
  if default_options?(options) && MAP[strategy_name]
    instance_eval MAP[strategy_name] 
  end       

  if strategies_with_role_class.include? strategy_name        
    if !options.kind_of? Symbol
      @role_class_name = get_role_class(options)
    else
      @role_class_name = default_role_class if strategies_with_role_class.include? strategy_name
    end
  end
  
  set_role_strategy name, options
end

#valid_role_group?(name) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/roles_generic/generic.rb', line 49

def valid_role_group? name
  valid_role_groups.include? name.to_sym
end

#valid_role_groups_are(*group) ⇒ Object



45
46
47
# File 'lib/roles_generic/generic.rb', line 45

def valid_role_groups_are *group
  valid_role_groups = group        
end