<commit_before>require 'bitmask_attribute/value_proxy'
require 'bitmask_attribute/attribute'
require 'bitmask_attribute/core_ext/hash_with_indifferent_access' unless defined?(HashWithIndifferentAccess)
require 'bitmask_attribute/core_ext/returning' unless Object.respond_to?(:returning)
require 'bitmask_attribute/core_ext/blank' unless Object.respond_to?(:blank?)

module BitmaskAttribute
  
  class Definition
    
    attr_reader :attribute, :values, :extension
    def initialize(attribute, values=[], &extension)
      @attribute = attribute
      @values = values
      @extension = extension
    end
    
    def install_on(model)
      validate_for model
      generate_bitmasks_on model
      override model
      create_convenience_class_method_on(model)
      create_convenience_instance_methods_on(model)
      create_named_scopes_on(model) if defined?(ActiveRecord::Base) && model.respond_to?(scope_method)
    end
    
    #######
    private
    #######

    def validate_for(model)
      # The model cannot be validated if it is preloaded and the attribute/column is not in the
      # database (the migration has not been run).  This usually
      # occurs in the 'test' and 'production' environments.
      return if defined?(Rails) && Rails.configuration.cache_classes

      unless !model.respond_to?(:columns) || model.columns.detect { |col| col.name == attribute.to_s }
        raise ArgumentError, "`#{attribute}' is not an attribute of `#{model}'"
      end
    end
    
    def generate_bitmasks_on(model)<commit_msg>Fix validate_for bug during migrations and tests<commit_after>require 'bitmask_attribute/value_proxy'
require 'bitmask_attribute/attribute'
require 'bitmask_attribute/core_ext/hash_with_indifferent_access' unless defined?(HashWithIndifferentAccess)
require 'bitmask_attribute/core_ext/returning' unless Object.respond_to?(:returning)
require 'bitmask_attribute/core_ext/blank' unless Object.respond_to?(:blank?)

module BitmaskAttribute
  
  class Definition
    
    attr_reader :attribute, :values, :extension
    def initialize(attribute, values=[], &extension)
      @attribute = attribute
      @values = values
      @extension = extension
    end
    
    def install_on(model)
      validate_for model
      generate_bitmasks_on model
      override model
      create_convenience_class_method_on(model)
      create_convenience_instance_methods_on(model)
      create_named_scopes_on(model) if defined?(ActiveRecord::Base) && model.respond_to?(scope_method)
    end
    
    #######
    private
    #######

    def validate_for(model)
      unless (model.columns.detect { |col| col.name == attribute.to_s } rescue true)
        raise ArgumentError, "`#{attribute}' is not an attribute of `#{model}'"
      end
    end
    
    def generate_bitmasks_on(model)
{'fraction_non_alphanumeric': 0.09145907473309609, 'fraction_numerical': 0.0, 'mean_word_length': 4.010695187165775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6780866', 'n_tokens_mistral': 806, 'n_tokens_neox': 809, 'n_words': 187}