<commit_before>require 'active_support/core_ext/class/inheritable_attributes'

module DarDaDa
  module ActiveRecordExtension
    def self.included(base)
      base.extend(ClassMethods)
    end
    
    def self.decorate(base, config)
      base.send :include, DarDaDa::ActiveRecordExtension::InstanceMethods
      
      config.each do |name, role|
        base.class_eval("
          def #{name}?
            role_symbol == :#{name}
          end
          
          scope :#{name.to_s.pluralize}, where(\"#{base.table_name}.#{config.options[:role_attribute]} = '#{name}'\")
        ")
      end
      config.all_rights.each do |right|
        base.class_eval("
          def allowed_to_#{right}?
            allowed_to?(:#{right})
          end
        ")
      end
    end
    
    module ClassMethods
      def define_roles(options={}, &block)
        (self.dar_dar_da ||= DarDaDa::Config.new(options)).tap do |config|
          config.reopen(&block)
          DarDaDa::ActiveRecordExtension.decorate(self, config)
        end
      end
      
      def dar_dar_da=(config)
        write_inheritable_attribute(:dar_dar_da, config)
      end
      
      def dar_dar_da
        read_inheritable_attribute(:dar_dar_da)
      end
    end
    
    module InstanceMethods
      def self.included(base)
        base.send :before_save, :set_role_to_default_value
      end
      
      def role
        role_symbol.to_s
      end
      
      def role=(new_role)
        write_role_attribute(new_role) if self.class.dar_dar_da[new_role]<commit_msg>Compatiblity with Rails 3.2:   * Swapped out the deprecated inheritable_attribute for class_attribute<commit_after>require 'active_support/core_ext/class/attribute'

module DarDaDa
  module ActiveRecordExtension
    def self.included(base)
      base.class_attribute(:dar_dar_da, :instance_reader => false, :instance_writer => false)
      base.extend(ClassMethods)
    end
    
    def self.decorate(base, config)
      base.send :include, DarDaDa::ActiveRecordExtension::InstanceMethods
      
      config.each do |name, role|
        base.class_eval("
          def #{name}?
            role_symbol == :#{name}
          end
          
          scope :#{name.to_s.pluralize}, where(\"#{base.table_name}.#{config.options[:role_attribute]} = '#{name}'\")
        ")
      end
      config.all_rights.each do |right|
        base.class_eval("
          def allowed_to_#{right}?
            allowed_to?(:#{right})
          end
        ")
      end
    end
    
    module ClassMethods
      def define_roles(options={}, &block)
        (self.dar_dar_da ||= DarDaDa::Config.new(options)).tap do |config|
          config.reopen(&block)
          DarDaDa::ActiveRecordExtension.decorate(self, config)
        end
      end
    end
    
    module InstanceMethods
      def self.included(base)
        base.send :before_save, :set_role_to_default_value
      end
      
      def role
        role_symbol.to_s
      end
      
      def role=(new_role)
        write_role_attribute(new_role) if self.class.dar_dar_da[new_role]
{'fraction_non_alphanumeric': 0.10126994464343862, 'fraction_numerical': 0.0006512536633018561, 'mean_word_length': 2.240506329113924, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6527332', 'n_tokens_mistral': 945, 'n_tokens_neox': 943, 'n_words': 169}