<commit_before>#!/usr/bin/env ruby

require 'optparse'
require 'pathname'
require 'json'

class TldDefs
  class TldDef
    attr_accessor :name

    attr_accessor :host
    attr_accessor :adapter
    attr_accessor :format
    attr_accessor :url

    attr_accessor :type
    attr_accessor :group
    attr_accessor :note

    ATTRIBUTES = {
        host: :host,
        adapter: :adapter,
        format: :format,
        url: :url,

        type: :_type,
        group: :_group,
        note: :_note,
    }


    # Normalizes the TLD name by appending the dot, if missing.
    #
    # @return [String] the normalized TLD name
    def self.name(string)
      string = string.to_str
      string.start_with?(".") ? string : ".#{string}"
    end

    def initialize(name, attributes = {})
      @name = self.class.name(name)
      @attributes = {}

      update(attributes)
    end

    # Updates the definition attributes.
    #
    # @param  attributes [Hash]
    # @return [void]
    def update(attributes = {})
      validate(attributes)

      attributes.each do |key, value|
        @attributes[ATTRIBUTES[key.to_sym]] = value
      end
    end

    # Validates the definitions to make sure there are no unknown attributes.
    #
    # @param  attributes [Hash]
    # @return [void]
    # @raise  [ArgumentError] when a definition attribute is unknown
    def validate(attributes)
      keys = ATTRIBUTES.keys
      attributes.each do |key, _|
        keys.include?(key.to_sym) or
            raise ArgumentError, "Invalid attribute `#{key}`"
      end
    end

    # Dump the definition object into a serializable Hash.
    #
    # @return [Hash] the serializable hash
    def dump
      @attributes.reject { |_, value| value.nil? }
    end
  end

  def initialize(file_path)
    @path = Pathname.new(file_path)
  end

  def read
    JSON.load(@path)
  end

  def write(data)
    data = Hash[data.sort]
    JSON.pretty_generate(data)
  end

  def count
    read.count
  end

  def tlds_add(*tlds, **attributes)<commit_msg>Sort tlds from right-to-left<commit_after>#!/usr/bin/env ruby

require 'optparse'
require 'pathname'
require 'json'

class TldDefs
  class TldDef
    attr_accessor :name

    attr_accessor :host
    attr_accessor :adapter
    attr_accessor :format
    attr_accessor :url

    attr_accessor :type
    attr_accessor :group
    attr_accessor :note

    ATTRIBUTES = {
        host: :host,
        adapter: :adapter,
        format: :format,
        url: :url,

        type: :_type,
        group: :_group,
        note: :_note,
    }


    # Normalizes the TLD name by appending the dot, if missing.
    #
    # @return [String] the normalized TLD name
    def self.name(string)
      string = string.to_str
      string.start_with?(".") ? string : ".#{string}"
    end

    def initialize(name, attributes = {})
      @name = self.class.name(name)
      @attributes = {}

      update(attributes)
    end

    # Updates the definition attributes.
    #
    # @param  attributes [Hash]
    # @return [void]
    def update(attributes = {})
      validate(attributes)

      attributes.each do |key, value|
        @attributes[ATTRIBUTES[key.to_sym]] = value
      end
    end

    # Validates the definitions to make sure there are no unknown attributes.
    #
    # @param  attributes [Hash]
    # @return [void]
    # @raise  [ArgumentError] when a definition attribute is unknown
    def validate(attributes)
      keys = ATTRIBUTES.keys
      attributes.each do |key, _|
        keys.include?(key.to_sym) or
            raise ArgumentError, "Invalid attribute `#{key}`"
      end
    end

    # Dump the definition object into a serializable Hash.
    #
    # @return [Hash] the serializable hash
    def dump
      @attributes.reject { |_, value| value.nil? }
    end
  end

  def initialize(file_path)
    @path = Pathname.new(file_path)
  end

  def read
    JSON.load(@path)
  end

  def write(data)
    data = Hash[data.sort_by { |tld, _| tld.split(".").reverse.join(".") }]
    JSON.pretty_generate(data)
  end

  def count
    read.count
  end

  def tlds_add(*tlds, **attributes)
{'fraction_non_alphanumeric': 0.10214007782101167, 'fraction_numerical': 0.0, 'mean_word_length': 2.3879736408566723, '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': '6490156', 'n_tokens_mistral': 1332, 'n_tokens_neox': 1299, 'n_words': 358}