<commit_before>module Pod
  # Converts objects to their YAML representation.
  #
  # This class was created for the need having control on how the YAML is
  # representation is generated. In details it provides:
  #
  # - sorting for hashes in ruby 1.8.x
  # - ability to hint the sorting of the keys of a dictionary when converting
  #   it. In this case the keys are also separated by an additional new line
  #   feed for readability.
  #
  # @note This class misses important features necessary for a correct YAML
  #       serialization and thus it is safe to use only for the Lockfile.
  #       The missing features include:
  #       - Strings are never quoted even when ambiguous.
  #
  class YAMLHelper
    class << self
      # Returns the YAML representation of the given object. If the given object
      # is an Hash it accepts an optional hint for sorting the keys.
      #
      # @param  [String, Symbol, Array, Hash] object
      #         the object to convert
      #
      # @param  [Array] hash_keys_hint
      #         an array to use as a hint for sorting the keys of the object to
      #         convert if it is an hash.
      #
      # @return [String] the YAML representation of the given object.
      #
      def convert(value)
        result = process_according_to_class(value)
        result << "\n"
      end

      def convert_hash(value, hash_keys_hint, line_separator = "\n")
        result = process_hash(value, hash_keys_hint, line_separator)
        result << "\n"
      end

      # Loads a YAML string and provide more informative 
      # error messages in special cases like merge conflict.
      #
      # @param [String] yaml_string
      #        The YAML String to be loaded
      #
      # @param [Pathname] file_path
      #        The (optional) file path to be used for read for the YAML file
      #
      # @return [Hash, Array] the Ruby YAML representaton
      #
      def load_string(yaml_string, file_path = nil)
        YAML.load(yaml_string)
        rescue Exception
          if yaml_has_merge_error?(yaml_string)
            raise Informative, yaml_merge_conflict_msg(yaml_string, file_path)
          else
            raise Informative, "Error parsing YAML: #{yaml_string}"
          end
      end

      # Loads a YAML file and leans on the #load_string imp
      # to do error detection. 
      # 
      # @param [Pathname] file_path
      #        The file path to be used for read for the YAML file
      #
      # @return [Hash, Array] the Ruby YAML representaton
      #
      def load_file(file_path)
        raise(Exception, "#load_file only accepts a Pathname") unless file_path.is_a?(Pathname)
        File.open(file_path, "r") do |file|
          return load_string(file.read, file.path)
        end
      end

      #-----------------------------------------------------------------------#

      private

      # Implementation notes:
      #
      # - each of the methods returns a YAML partial without an ending new
      #   line.
      # - if a partial needs to be indented is responsibility of the method
      #   using it.
      #
      # ---<commit_msg>some white space<commit_after>module Pod
  # Converts objects to their YAML representation.
  #
  # This class was created for the need having control on how the YAML is
  # representation is generated. In details it provides:
  #
  # - sorting for hashes in ruby 1.8.x
  # - ability to hint the sorting of the keys of a dictionary when converting
  #   it. In this case the keys are also separated by an additional new line
  #   feed for readability.
  #
  # @note This class misses important features necessary for a correct YAML
  #       serialization and thus it is safe to use only for the Lockfile.
  #       The missing features include:
  #       - Strings are never quoted even when ambiguous.
  #
  class YAMLHelper
    class << self
      # Returns the YAML representation of the given object. If the given object
      # is an Hash it accepts an optional hint for sorting the keys.
      #
      # @param  [String, Symbol, Array, Hash] object
      #         the object to convert
      #
      # @param  [Array] hash_keys_hint
      #         an array to use as a hint for sorting the keys of the object to
      #         convert if it is an hash.
      #
      # @return [String] the YAML representation of the given object.
      #
      def convert(value)
        result = process_according_to_class(value)
        result << "\n"
      end

      def convert_hash(value, hash_keys_hint, line_separator = "\n")
        result = process_hash(value, hash_keys_hint, line_separator)
        result << "\n"
      end

      # Loads a YAML string and provide more informative
      # error messages in special cases like merge conflict.
      #
      # @param [String] yaml_string
      #        The YAML String to be loaded
      #
      # @param [Pathname] file_path
      #        The (optional) file path to be used for read for the YAML file
      #
      # @return [Hash, Array] the Ruby YAML representaton
      #
      def load_string(yaml_string, file_path = nil)
        YAML.load(yaml_string)
        rescue Exception
          if yaml_has_merge_error?(yaml_string)
            raise Informative, yaml_merge_conflict_msg(yaml_string, file_path)
          else
            raise Informative, "Error parsing YAML: #{yaml_string}"
          end
      end

      # Loads a YAML file and leans on the #load_string imp
      # to do error detection
      #
      # @param [Pathname] file_path
      #        The file path to be used for read for the YAML file
      #
      # @return [Hash, Array] the Ruby YAML representaton
      #
      def load_file(file_path)
        raise(Exception, "#load_file only accepts a Pathname") unless file_path.is_a?(Pathname)
        File.open(file_path, "r") do |file|
          return load_string(file.read, file.path)
        end
      end

      #-----------------------------------------------------------------------#

      private

      # Implementation notes:
      #
      # - each of the methods returns a YAML partial without an ending new
      #   line.
      # - if a partial needs to be indented is responsibility of the method
      #   using it.
      #
      # ---
{'fraction_non_alphanumeric': 0.08462151394422311, 'fraction_numerical': 0.0006374501992031873, 'mean_word_length': 1.9561940650023553, 'pattern_counts': {'":': 0, '<': 15, '<?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': '2249363', 'n_tokens_mistral': 1765, 'n_tokens_neox': 1687, 'n_words': 690}