Module: RoleStrategy::MongoMapper::OneRole::Implementation

Includes:
Roles::MongoMapper::Strategy::Single
Defined in:
lib/roles_mongo_mapper/strategy/single/one_role.rb

Instance Method Summary collapse

Methods included from Roles::MongoMapper::Strategy::Single

#remove_roles, #roles_list

Methods included from Roles::MongoMapper::Strategy::Shared

#get_role, #get_roles, #set_roles

Instance Method Details

#add_roles(*role_names) ⇒ Object



43
44
45
46
47
# File 'lib/roles_mongo_mapper/strategy/single/one_role.rb', line 43

def add_roles *role_names
  new_roles = select_valid_roles(role_names) 
  the_role = new_roles.first if !new_roles.empty?
  set_role new_role(the_role)
end

#new_role(role) ⇒ Object



56
57
58
# File 'lib/roles_mongo_mapper/strategy/single/one_role.rb', line 56

def new_role role
  role_class.find_role(extract_role role)        
end

#new_roles(*roles) ⇒ Object



60
61
62
# File 'lib/roles_mongo_mapper/strategy/single/one_role.rb', line 60

def new_roles *roles
  new_role roles.flatten.first
end

#present_roles(*roles) ⇒ Object



64
65
66
# File 'lib/roles_mongo_mapper/strategy/single/one_role.rb', line 64

def present_roles *roles
  roles.map{|role| extract_role role}
end

#select_valid_roles(*role_names) ⇒ Object



72
73
74
75
# File 'lib/roles_mongo_mapper/strategy/single/one_role.rb', line 72

def select_valid_roles *role_names
  role_names = role_names.flat_uniq.select{|role| valid_role? role }
  has_role_class? ? role_class.find_roles(role_names).to_a : role_names
end

#set_empty_roleObject



68
69
70
# File 'lib/roles_mongo_mapper/strategy/single/one_role.rb', line 68

def set_empty_role
  self.send("#{role_attribute}=", nil)
end

#set_role(role) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
53
54
# File 'lib/roles_mongo_mapper/strategy/single/one_role.rb', line 49

def set_role role
  raise ArgumentError, "#set_role only takes a single role as argument, not #{role}" if role.kind_of?(Array)
  the_role = new_role(role)
  self.send("#{role_attribute}=", the_role)
  # self.one_role_id = the_role.id
end