Sha256: ec8d3d8ba0a5dd031db2f32d1268c5e02ebf89ba811f9afe0a02a0175d40f683

Contents?: true

Size: 1.92 KB

Versions: 7

Compression:

Stored size: 1.92 KB

Contents

require 'roles_active_record/strategy/multi'

module RoleStrategy::ActiveRecord
  module RolesMask
    def self.default_role_attribute
      :roles_mask
    end

    def self.included base
      base.extend ClassMethods
    end

    module ClassMethods
      def role_attribute
        strategy_class.roles_attribute_name.to_sym
      end 

      def in_role(role)     
        mask = calc_index(role.to_s)
        all.select do |user| 
          (user.send(role_attribute) & mask) > 0
        end
      end    

      def in_any_role(*roles)
        all.select do |user| 
          roles.flatten.any? do |role|
            mask = calc_index(role.to_s)
            (user.send(role_attribute) & mask) > 0
          end
        end
      end    
  
      def calc_index(r)
        2**strategy_class.valid_roles.index(r.to_sym)
      end 
    end

    module Implementation
      include Roles::ActiveRecord::Strategy::Multi      

      class Roles < ::Set # :nodoc:
        attr_reader :model_instance

        def initialize(sender, *roles)
          super(*roles)
          @model_instance = sender
        end

        def <<(role)
          model_instance.roles = super.to_a
          self
        end
      end

      protected

      def calc_index(r)
        2**strategy_class.valid_roles.index(r)
      end

      def get_roles
        strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((get_role || 0) & calc_index(r)).zero? })        
      end
      
      def new_roles *role_names
        role_names = role_names.flatten.map{ |r| r.to_sym } & strategy_class.valid_roles
        role_names.map { |r| calc_index(r) }.inject { |sum, bitvalue| sum + bitvalue }
      end

      def set_empty_roles
        self.send("#{role_attribute}=", 0)
      end
      
      def present_roles *role_names
        role_names.to_a.to_symbols
      end
    end    
    
    extend Roles::Generic::User::Configuration
    configure            
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
roles_active_record-0.4.0 lib/roles_active_record/strategy/multi/roles_mask.rb
roles_active_record-0.3.6 lib/roles_active_record/strategy/multi/roles_mask.rb
roles_active_record-0.3.5 lib/roles_active_record/strategy/multi/roles_mask.rb
roles_active_record-0.3.4 lib/roles_active_record/strategy/multi/roles_mask.rb
roles_active_record-0.3.3 lib/roles_active_record/strategy/multi/roles_mask.rb
roles_active_record-0.3.2 lib/roles_active_record/strategy/multi/roles_mask.rb
roles_active_record-0.3.1 lib/roles_active_record/strategy/multi/roles_mask.rb