<commit_before>module Oga
  module XML
    ##
    # Class for storing information about a single XML attribute.
    #
    class Attribute
      # The name of the attribute.
      # @return [String]
      attr_accessor :name

      # @return [String]
      attr_accessor :namespace_name

      # The element this attribute belongs to.
      # @return [Oga::XML::Element]
      attr_accessor :element

      ##
      # The default namespace available to all attributes. This namespace can
      # not be modified.
      #
      # @return [Oga::XML::Namespace]
      #
      DEFAULT_NAMESPACE = Namespace.new(
        :name => 'xml',
        :uri  => XML::DEFAULT_NAMESPACE.uri
      ).freeze

      ##
      # @param [Hash] options
      #
      # @option options [String] :name
      # @option options [String] :namespace_name
      # @option options [String] :value
      # @option options [Oga::XML::Element] :element
      #
      def initialize(options = {})
        @name    = options[:name]
        @value   = options[:value]
        @element = options[:element]

        @namespace_name = options[:namespace_name]
      end

      ##
      # Returns the {Oga::XML::Namespace} instance for the current namespace
      # name.
      #
      # @return [Oga::XML::Namespace]
      #
      def namespace
        unless @namespace
          if namespace_name == DEFAULT_NAMESPACE.name
            @namespace = DEFAULT_NAMESPACE
          else
            @namespace = element.available_namespaces[namespace_name]
          end
        end

        @namespace
      end

      ##
      # @param [String] value
      #
      def value=(value)
        @value   = value
        @decoded = false
      end

      ##
      # Returns the value of the attribute or nil if no explicit value was set.
      #
      # @return [String|NilClass]
      #
      def value
        if !@decoded and @value
          @value   = EntityDecoder.try_decode(@value, html?)
          @decoded = true
        end

        @value
      end

      ##
      # @return [String]
      #
      def text
        value.to_s
      end

      alias_method :to_s, :text

      ##
      # @return [String]
      #
      def to_xml
        if namespace_name
          full_name = "#{namespace_name}:#{name}"
        else
          full_name = name
        end

        if value
          enc_value = Entities.encode_attribute(value)
        else
          enc_value = nil
        end

        %Q(#{full_name}="#{enc_value}")
      end

      ##
      # @return [String]
      #
      def inspect
        segments = []

        [:name, :namespace, :value].each do |attr|
          value = send(attr)
<commit_msg>Use ternary "if" when encoding attribute values<commit_after>module Oga
  module XML
    ##
    # Class for storing information about a single XML attribute.
    #
    class Attribute
      # The name of the attribute.
      # @return [String]
      attr_accessor :name

      # @return [String]
      attr_accessor :namespace_name

      # The element this attribute belongs to.
      # @return [Oga::XML::Element]
      attr_accessor :element

      ##
      # The default namespace available to all attributes. This namespace can
      # not be modified.
      #
      # @return [Oga::XML::Namespace]
      #
      DEFAULT_NAMESPACE = Namespace.new(
        :name => 'xml',
        :uri  => XML::DEFAULT_NAMESPACE.uri
      ).freeze

      ##
      # @param [Hash] options
      #
      # @option options [String] :name
      # @option options [String] :namespace_name
      # @option options [String] :value
      # @option options [Oga::XML::Element] :element
      #
      def initialize(options = {})
        @name    = options[:name]
        @value   = options[:value]
        @element = options[:element]

        @namespace_name = options[:namespace_name]
      end

      ##
      # Returns the {Oga::XML::Namespace} instance for the current namespace
      # name.
      #
      # @return [Oga::XML::Namespace]
      #
      def namespace
        unless @namespace
          if namespace_name == DEFAULT_NAMESPACE.name
            @namespace = DEFAULT_NAMESPACE
          else
            @namespace = element.available_namespaces[namespace_name]
          end
        end

        @namespace
      end

      ##
      # @param [String] value
      #
      def value=(value)
        @value   = value
        @decoded = false
      end

      ##
      # Returns the value of the attribute or nil if no explicit value was set.
      #
      # @return [String|NilClass]
      #
      def value
        if !@decoded and @value
          @value   = EntityDecoder.try_decode(@value, html?)
          @decoded = true
        end

        @value
      end

      ##
      # @return [String]
      #
      def text
        value.to_s
      end

      alias_method :to_s, :text

      ##
      # @return [String]
      #
      def to_xml
        if namespace_name
          full_name = "#{namespace_name}:#{name}"
        else
          full_name = name
        end

        enc_value = value ? Entities.encode_attribute(value) : nil

        %Q(#{full_name}="#{enc_value}")
      end

      ##
      # @return [String]
      #
      def inspect
        segments = []

        [:name, :namespace, :value].each do |attr|
          value = send(attr)

{'fraction_non_alphanumeric': 0.09898572501878287, 'fraction_numerical': 0.0, 'mean_word_length': 1.6758793969849246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '4117002', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1496, 'n_words': 402}