Module: RoleStrategy::Mongoid::EmbedOneRole::Implementation
- Includes:
- Mongoid::Roles::Strategy::Single
- Defined in:
- lib/roles_mongoid/strategy/single/embed_one_role.rb
Instance Method Summary collapse
- #exchange_roles(*role_names) ⇒ Object
- #new_role(role) ⇒ Object
- #new_roles(*roles) ⇒ Object
- #present_roles(*roles) ⇒ Object
- #select_valid_roles(*role_names) ⇒ Object
- #set_empty_role ⇒ Object
- #set_role(role) ⇒ Object
- #set_roles(*roles) ⇒ Object
Instance Method Details
#exchange_roles(*role_names) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/roles_mongoid/strategy/single/embed_one_role.rb', line 49 def exchange_roles *role_names = last_option role_names raise ArgumentError, "Must take an options hash as last argument with a :with option signifying which role(s) to replace with" if ! || !.kind_of?(Hash) common = role_names.to_symbols & roles_list if !common.empty? with_roles = [:with] set_role with_roles end end |
#new_role(role) ⇒ Object
59 60 61 |
# File 'lib/roles_mongoid/strategy/single/embed_one_role.rb', line 59 def new_role role role_class.find_role(extract_role role) end |
#new_roles(*roles) ⇒ Object
63 64 65 |
# File 'lib/roles_mongoid/strategy/single/embed_one_role.rb', line 63 def new_roles *roles new_role roles.flatten.first end |
#present_roles(*roles) ⇒ Object
67 68 69 |
# File 'lib/roles_mongoid/strategy/single/embed_one_role.rb', line 67 def present_roles *roles roles.map{|role| extract_role role} end |
#select_valid_roles(*role_names) ⇒ Object
75 76 77 |
# File 'lib/roles_mongoid/strategy/single/embed_one_role.rb', line 75 def select_valid_roles *role_names role_names = role_names.flat_uniq.select{|role| valid_role? role } end |
#set_empty_role ⇒ Object
71 72 73 |
# File 'lib/roles_mongoid/strategy/single/embed_one_role.rb', line 71 def set_empty_role self.send("#{role_attribute}=", nil) end |
#set_role(role) ⇒ Object
39 40 41 42 |
# File 'lib/roles_mongoid/strategy/single/embed_one_role.rb', line 39 def set_role role raise ArgumentError, "#set_role only takes a single role as argument, not #{role}" if role.kind_of?(Array) self.send("create_#{role_attribute}", :name => role) end |
#set_roles(*roles) ⇒ Object
44 45 46 47 |
# File 'lib/roles_mongoid/strategy/single/embed_one_role.rb', line 44 def set_roles *roles roles = roles.flat_uniq self.send("create_#{role_attribute}", :name => roles.first) end |