Module: Roles::DataMapper::Strategy::Multi

Included in:
RoleStrategy::DataMapper::ManyRoles::Implementation, RoleStrategy::DataMapper::RolesMask::Implementation
Defined in:
lib/roles_data_mapper/strategy/multi.rb

Instance Method Summary collapse

Instance Method Details

#add_roles(*roles) ⇒ Object



13
14
15
16
17
18
# File 'lib/roles_data_mapper/strategy/multi.rb', line 13

def add_roles *roles
  new_roles = select_valid_roles(roles)
  if !new_roles.empty?
    self.roles = self.roles + new_roles
  end
end

#remove_roles(*role_names) ⇒ Object

should remove the current single role (set = nil) only if it is contained in the list of roles to be removed



22
23
24
25
26
27
28
# File 'lib/roles_data_mapper/strategy/multi.rb', line 22

def remove_roles *role_names
  role_names = role_names.flat_uniq
  set_empty_roles and return if roles_diff(role_names).empty?
  roles_to_remove = select_valid_roles(role_names)
  set_roles roles_diff(role_names)
  true
end

#rolesObject

query assigned roles



31
32
33
34
35
# File 'lib/roles_data_mapper/strategy/multi.rb', line 31

def roles
  get_roles.map do |role|
    role.respond_to?(:sym) ? role.to_sym : role
  end
end

#roles=(*role_names) ⇒ Object

assign multiple roles



7
8
9
10
11
# File 'lib/roles_data_mapper/strategy/multi.rb', line 7

def roles=(*role_names)
  extracted_roles = extract_roles(role_names)
  return nil if extracted_roles.empty?
  set_roles(select_valid_roles extracted_roles)
end

#roles_listObject



37
38
39
40
41
# File 'lib/roles_data_mapper/strategy/multi.rb', line 37

def roles_list
  my_roles = [roles].flat_uniq
  return [] if my_roles.empty?
  has_role_class? ? my_roles.map{|r| r.name.to_sym } : my_roles          
end