<commit_before>module Roles::Base
  def valid_roles_are(*role_list)
    strategy_class.valid_roles = role_list.to_symbols
  end
end

module Roles::ActiveRecord
  mattr_accessor :warning
    
  def self.included(base) 
    base.extend Roles::Base
    base.extend ClassMethods
    base.orm_name = :active_record
  end

  module ClassMethods      

    def valid_single_strategies
      [:admin_flag, :one_role, :role_string]
    end

    def valid_multi_strategies
      [:many_roles, :roles_mask]
    end

    def strategies_with_role_class
      [:one_role, :many_roles]
    end 

    def valid_strategies
      valid_single_strategies + valid_multi_strategies
    end
    
    def strategy name, options = {}
      strategy_name = name.to_sym
      raise ArgumentError, "Unknown role strategy #{strategy_name}" if !valid_strategies.include? strategy_name
      use_roles_strategy strategy_name
            
      set_role_class(strategy_name, options) if strategies_with_role_class.include? strategy_name

      # one_role reference
      if strategy_name == :one_role
        self.belongs_to :one_role, :foreign_key => 'role_id', :class_name => @role_class_name.to_s
      end
      
      # many_roles references
      if strategy_name == :many_roles
        user_roles_class = options[:user_roles_class] if options.kind_of? Hash 
        user_roles_class ||= 'user_roles'
      
        instance_eval %{
          has_many :many_roles, :through => :#{user_roles_class}, :source => :#{@role_class_name.to_s.underscore}
          has_many :#{user_roles_class}
        }
      end
      
      set_role_strategy name, options
    end    
    
    private

    def set_role_class strategy_name, options = {}
      @role_class_name = !options.kind_of?(Symbol) ? get_role_class(strategy_name, options) : default_role_class(strategy_name)
    end

    def statement code_str
      code_str.gsub /Role/, @role_class_name.to_s
    end

    def default_role_class strategy_name
      if defined? ::Role
        require "roles_active_record/#{strategy_name}"
        return ::Role 
      end
      raise "Default Role class not defined" if @@warning
    end
    
    def get_role_class strategy_name, options
      options[:role_class] ? options[:role_class].to_s.camelize.constantize : default_role_class(strategy_name)
    end
  end
end<commit_msg>removed debugging statement and created fall back for role class<commit_after>module Roles::Base
  def valid_roles_are(*role_list)
    strategy_class.valid_roles = role_list.to_symbols
  end
end

module Roles::ActiveRecord
  mattr_accessor :warnings_on
    
  def self.included(base) 
    base.extend Roles::Base
    base.extend ClassMethods
    base.orm_name = :active_record
  end

  module ClassMethods      

    def valid_single_strategies
      [:admin_flag, :one_role, :role_string]
    end

    def valid_multi_strategies
      [:many_roles, :roles_mask]
    end

    def strategies_with_role_class
      [:one_role, :many_roles]
    end 

    def valid_strategies
      valid_single_strategies + valid_multi_strategies
    end
    
    def strategy name, options = {}
      strategy_name = name.to_sym
      raise ArgumentError, "Unknown role strategy #{strategy_name}" if !valid_strategies.include? strategy_name
      use_roles_strategy strategy_name
            
      set_role_class(strategy_name, options) if strategies_with_role_class.include? strategy_name

      # one_role reference
      if strategy_name == :one_role
        self.belongs_to :one_role, :foreign_key => 'role_id', :class_name => role_class_name.to_s
      end
      
      # many_roles references
      if strategy_name == :many_roles      
        urc = user_roles_class options
        instance_eval many_roles_stmt(urc)
      end
      
      set_role_strategy name, options
    end    
    
    private
     
    def many_roles_stmt urc
      %{
        has_many :many_roles, :through => :#{urc}, :source => :#{role_class_name.to_s.underscore}
        has_many :#{urc}
      }
    end

    def role_class_name options = {}
      return @role_class_name if @role_class_name  
      return options[:role_class] if options.kind_of?(Hash) && options[:role_class] 
      'Role'    
    end

    def user_roles_class options
      return options[:user_roles_class] if options.kind_of?(Hash) && options[:user_roles_class]
      'user_roles'
    end

    def set_role_class strategy_name, options = {}
      @role_class_name = !options.kind_of?(Symbol) ? get_role_class(strategy_name, options) : default_role_class(strategy_name)
    end

    def statement code_str
      code_str.gsub /Role/, @role_class_name.to_s
    end

    def default_role_class strategy_name
      if defined? ::Role
        require "roles_active_record/#{strategy_name}"
        return ::Role 
      end
      raise "Default Role class not defined" if Roles::ActiveRecord.warnings_on
    end
    
    def get_role_class strategy_name, options
      options[:role_class] ? options[:role_class].to_s.camelize.constantize : default_role_class(strategy_name)
    end
  end
end
{'fraction_non_alphanumeric': 0.07414711102346677, 'fraction_numerical': 0.0, 'mean_word_length': 2.9968479117415288, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '948260', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1633, 'n_words': 337}