Sha256: 6621c9ad6634037b383ac4e39ebb62dd31c93915bf910a31cf634cc4f0c9bb06

Contents?: true

Size: 1.12 KB

Versions: 6

Compression:

Stored size: 1.12 KB

Contents

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

    module Implementation 
      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

      # assign roles
      def roles=(*roles)
        self.send("#{role_attribute}=", (roles.flatten.map { |r| r.to_sym } & strategy_class.valid_roles).map { |r| calc_index(r) }.inject { |sum, bitvalue| sum + bitvalue })
      end
      alias_method :role=, :roles=

      # query assigned roles
      def roles
        strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((self.send(role_attribute) || 0) & calc_index(r)).zero? })
      end

      def roles_list
        roles.to_a
      end        

      protected

      def calc_index(r)
        2**strategy_class.valid_roles.index(r)
      end
    end    
    
    extend Roles::Generic::User::Configuration
    configure            
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
roles_generic-0.2.7 lib/roles_generic/strategy/multi/roles_mask.rb
roles_generic-0.2.6 lib/roles_generic/strategy/multi/roles_mask.rb
roles_generic-0.2.5 lib/roles_generic/strategy/multi/roles_mask.rb
roles_generic-0.2.4 lib/roles_generic/strategy/multi/roles_mask.rb
roles_generic-0.2.3 lib/roles_generic/strategy/multi/roles_mask.rb
roles_generic-0.2.2 lib/roles_generic/strategy/multi/roles_mask.rb