<commit_before>require 'pp'

module RubyXL
  class OOXMLObject

    # Throughout this class, setting class variables through explicit method calls rather
    # than by directly addressing the name of the variable because of context issues:
    # addressing variable by name creates it in the context of defining class,
    # while calling the setter/getter method addresses it in the context of descendant class, 
    # which is what we need.
    def self.obtain_class_variable(var_name, default = {})
      if class_variable_defined?(var_name) then 
        self.class_variable_get(var_name)
      else
        self.class_variable_set(var_name, default)
      end
    end

    def obtain_class_variable(var_name, default = {})
      self.class.obtain_class_variable(var_name, default)
    end
    private :obtain_class_variable

    def self.define_attribute(attr_name, attr_type, extra_params = {})
      attrs = obtain_class_variable(:@@ooxml_attributes)

      accessor = extra_params[:accessor]
      if accessor.nil? then 
        accessor = attr_name.to_s
        accessor.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
        accessor.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
        accessor.downcase!
        accessor = accessor.to_sym
      end

      attrs[accessor] = {
        :attr_name  => attr_name.to_s,
        :attr_type  => attr_type,
        :optional   => !extra_params[:required], 
        :default    => extra_params[:default],
        :validation => extra_params[:values]
      }

      self.send(:attr_accessor, accessor)
    end
    
    def self.define_child_node(klass, extra_params = {})
      child_nodes = obtain_class_variable(:@@ooxml_child_nodes)
      child_node_name = (extra_params[:node_name] || klass.class_variable_get(:@@ooxml_tag_name)).to_s
      accessor = (extra_params[:accessor] || child_node_name).to_sym

      child_nodes[child_node_name] = { 
        :class => klass,
        :is_array => extra_params[:collection],
        :accessor => accessor
      }

      self.send(:attr_accessor, accessor)
    end

    def self.define_element_name(v)
      self.class_variable_set(:@@ooxml_tag_name, v)
    end

    def write_xml(xml)
      before_write_xml if self.respond_to?(:before_write_xml)
      attrs = prepare_attributes
      content = attrs.delete('_')
      elem = xml.create_element(obtain_class_variable(:@@ooxml_tag_name), attrs, content)
      child_nodes = obtain_class_variable(:@@ooxml_child_nodes)
      child_nodes.each_value { |child_node_params|
        obj = self.send(child_node_params[:accessor])
        unless obj.nil?
          if child_node_params[:is_array] then obj.each { |item| elem << item.write_xml(xml) }
          else elem << obj.write_xml(xml)
          end
        end
      }
      elem
    end

    def initialize(params = {})
      return super unless self.class.class_variable_defined?(:@@ooxml_attributes)
      obtain_class_variable(:@@ooxml_attributes).each_key { |k| instance_variable_set("@#{k}", params[k]) }
      obtain_class_variable(:@@ooxml_child_nodes).each_pair { |k, v|

        initial_value =<commit_msg>Allow for overriding the child node names (needed for DataValidation and others)<commit_after>require 'pp'

module RubyXL
  class OOXMLObject

    # Throughout this class, setting class variables through explicit method calls rather
    # than by directly addressing the name of the variable because of context issues:
    # addressing variable by name creates it in the context of defining class,
    # while calling the setter/getter method addresses it in the context of descendant class, 
    # which is what we need.
    def self.obtain_class_variable(var_name, default = {})
      if class_variable_defined?(var_name) then 
        self.class_variable_get(var_name)
      else
        self.class_variable_set(var_name, default)
      end
    end

    def obtain_class_variable(var_name, default = {})
      self.class.obtain_class_variable(var_name, default)
    end
    private :obtain_class_variable

    def self.define_attribute(attr_name, attr_type, extra_params = {})
      attrs = obtain_class_variable(:@@ooxml_attributes)

      accessor = extra_params[:accessor]
      if accessor.nil? then 
        accessor = attr_name.to_s
        accessor.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
        accessor.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
        accessor.downcase!
        accessor = accessor.to_sym
      end

      attrs[accessor] = {
        :attr_name  => attr_name.to_s,
        :attr_type  => attr_type,
        :optional   => !extra_params[:required], 
        :default    => extra_params[:default],
        :validation => extra_params[:values]
      }

      self.send(:attr_accessor, accessor)
    end
    
    def self.define_child_node(klass, extra_params = {})
      child_nodes = obtain_class_variable(:@@ooxml_child_nodes)
      child_node_name = (extra_params[:node_name] || klass.class_variable_get(:@@ooxml_tag_name)).to_s
      accessor = (extra_params[:accessor] || child_node_name).to_sym

      child_nodes[child_node_name] = { 
        :class => klass,
        :is_array => extra_params[:collection],
        :accessor => accessor
      }

      self.send(:attr_accessor, accessor)
    end

    def self.define_element_name(v)
      self.class_variable_set(:@@ooxml_tag_name, v)
    end

    def write_xml(xml, node_name_override = nil)
      before_write_xml if self.respond_to?(:before_write_xml)
      attrs = prepare_attributes
      element_text = attrs.delete('_')
      elem = xml.create_element(node_name_override || obtain_class_variable(:@@ooxml_tag_name), attrs, element_text)
      child_nodes = obtain_class_variable(:@@ooxml_child_nodes)
      child_nodes.each_pair { |child_node_name, child_node_params|
        obj = self.send(child_node_params[:accessor])
        unless obj.nil?
          if child_node_params[:is_array] then obj.each { |item| elem << item.write_xml(xml) }
          else elem << obj.write_xml(xml, child_node_name)
          end
        end
      }
      elem
    end

    def initialize(params = {})
      return super unless self.class.class_variable_defined?(:@@ooxml_attributes)
      obtain_class_variable(:@@ooxml_attributes).each_key { |k| instance_variable_set("@#{k}", params[k]) }
      obtain_class_variable(:@@ooxml_child_nodes).each_pair { |k, v|

        initial_value =
{'fraction_non_alphanumeric': 0.10771653543307087, 'fraction_numerical': 0.0012598425196850393, 'mean_word_length': 3.1536952256376716, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 36}, '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': '4919139', 'n_tokens_mistral': 2025, 'n_tokens_neox': 2014, 'n_words': 392}