Module: RoleModel::ClassMethods

Defined in:
lib/role_model/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#inherited(subclass) ⇒ Object

:nodoc:



3
4
5
6
7
8
9
# File 'lib/role_model/class_methods.rb', line 3

def inherited(subclass) # :nodoc:
  ::RoleModel::INHERITABLE_CLASS_ATTRIBUTES.each do |attribute|
    instance_var = "@#{attribute}"
    subclass.instance_variable_set(instance_var, instance_variable_get(instance_var))
  end
  super
end

#mask_for(*roles) ⇒ Object



21
22
23
24
25
# File 'lib/role_model/class_methods.rb', line 21

def mask_for(*roles)
  sanitized_roles = roles.map { |role| Array(role) }.flatten.map(&:to_sym)

  (valid_roles & sanitized_roles).inject(0) { |sum, role| sum + 2**valid_roles.index(role) }
end

#roles_attribute(name) ⇒ Object

set the bitmask attribute role assignments will be stored in



12
13
14
# File 'lib/role_model/class_methods.rb', line 12

def roles_attribute(name)
  self.roles_attribute = name
end

#roles_attribute=(name) ⇒ Object

alternative method signature: set the bitmask attribute role assignments will be stored in



17
18
19
# File 'lib/role_model/class_methods.rb', line 17

def roles_attribute=(name)
  self.roles_attribute_name = name.to_sym
end

#roles_from_mask(mask) ⇒ Object



27
28
29
# File 'lib/role_model/class_methods.rb', line 27

def roles_from_mask(mask)
  valid_roles.reject { |role| (mask.to_i & 2**valid_roles.index(role)).zero? }
end