<commit_before>module CustomAttributeMixin
  extend ActiveSupport::Concern

  CUSTOM_ATTRIBUTES_PREFIX = "virtual_custom_attribute_".freeze
  SECTION_SEPARATOR        = ":SECTION:".freeze
  DEFAULT_SECTION_NAME     = 'Custom Attribute'.freeze

  included do
    has_many   :custom_attributes,     :as => :resource, :dependent => :destroy
    has_many   :miq_custom_attributes, -> { where(:source => 'EVM') }, :as => :resource, :dependent => :destroy, :class_name => "CustomAttribute"

    # This is a set of helper getter and setter methods to support the transition
    # between "custom_*" fields in the model and using the custom_attributes table.
    (1..9).each do |custom_id|
      custom_str = "custom_#{custom_id}"
      getter     = custom_str.to_sym
      setter     = "#{custom_str}=".to_sym

      define_method(getter) do
        miq_custom_get(custom_str)
      end
      virtual_column getter, :type => :string  # uses not set since miq_custom_get re-queries

      define_method(setter) do |value|
        miq_custom_set(custom_str, value)
      end
    end

    def self.custom_keys
      custom_attr_scope = CustomAttribute.where(:resource_type => base_class).where.not(:name => nil).distinct.pluck(:name, :section)
      custom_attr_scope.map do |x|
        "#{x[0]}#{x[1] ? SECTION_SEPARATOR + x[1] : ''}"
      end
    end

    def self.load_custom_attributes_for(cols)
      custom_attributes = CustomAttributeMixin.select_virtual_custom_attributes(cols)
      custom_attributes.each { |custom_attribute| add_custom_attribute(custom_attribute) }
    end

    def self.add_custom_attribute(custom_attribute)
      return if respond_to?(custom_attribute)

      virtual_column(custom_attribute.to_sym, :type => :string, :uses => :custom_attributes)

      define_method(custom_attribute.to_sym) do
        custom_attribute_without_prefix = custom_attribute.sub(CUSTOM_ATTRIBUTES_PREFIX, "")
        custom_attributes.detect { |x| custom_attribute_without_prefix == x.name }.try(:value)
      end<commit_msg>Update method for query to get value of custom attribute with section<commit_after>module CustomAttributeMixin
  extend ActiveSupport::Concern

  CUSTOM_ATTRIBUTES_PREFIX = "virtual_custom_attribute_".freeze
  SECTION_SEPARATOR        = ":SECTION:".freeze
  DEFAULT_SECTION_NAME     = 'Custom Attribute'.freeze

  included do
    has_many   :custom_attributes,     :as => :resource, :dependent => :destroy
    has_many   :miq_custom_attributes, -> { where(:source => 'EVM') }, :as => :resource, :dependent => :destroy, :class_name => "CustomAttribute"

    # This is a set of helper getter and setter methods to support the transition
    # between "custom_*" fields in the model and using the custom_attributes table.
    (1..9).each do |custom_id|
      custom_str = "custom_#{custom_id}"
      getter     = custom_str.to_sym
      setter     = "#{custom_str}=".to_sym

      define_method(getter) do
        miq_custom_get(custom_str)
      end
      virtual_column getter, :type => :string  # uses not set since miq_custom_get re-queries

      define_method(setter) do |value|
        miq_custom_set(custom_str, value)
      end
    end

    def self.custom_keys
      custom_attr_scope = CustomAttribute.where(:resource_type => base_class).where.not(:name => nil).distinct.pluck(:name, :section)
      custom_attr_scope.map do |x|
        "#{x[0]}#{x[1] ? SECTION_SEPARATOR + x[1] : ''}"
      end
    end

    def self.load_custom_attributes_for(cols)
      custom_attributes = CustomAttributeMixin.select_virtual_custom_attributes(cols)
      custom_attributes.each { |custom_attribute| add_custom_attribute(custom_attribute) }
    end

    def self.add_custom_attribute(custom_attribute)
      return if respond_to?(custom_attribute)

      virtual_column(custom_attribute.to_sym, :type => :string, :uses => :custom_attributes)

      define_method(custom_attribute.to_sym) do
        custom_attribute_without_prefix           = custom_attribute.sub(CUSTOM_ATTRIBUTES_PREFIX, "")
        custom_attribute_without_section, section = custom_attribute_without_prefix.split(SECTION_SEPARATOR)

        where_args = {}
        where_args[:name]    = custom_attribute_without_section
        where_args[:section] = section if section

        custom_attributes.find_by(where_args).try(:value)
      end
{'fraction_non_alphanumeric': 0.09718374884579871, 'fraction_numerical': 0.0023084025854108957, 'mean_word_length': 3.7407002188183807, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 27, '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': '7309680', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1315, 'n_words': 270}