<commit_before>module Rackdis
  class Config

    DEFAULTS = {
      port: 7380,
      address: "0.0.0.0",
      daemonize: false
    }
    
    def initialize(opts)
      @config = DEFAULTS.clone
      process_options opts
    end

    def rack_options
      {
        Port: @config[:port],
        Host: @config[:address],
        daemonize: @config[:daemonize]
      }
    end
    
    def [](key)
      @config[key]
    end
    
    private
    
    def process_options(opts)
      load_from_file opts[:config]
      
      # We want to merge the command line opts
      # overtop of the config file options but
      # only if they are set because when they
      # are not set they are nil which ends up
      # wiping out all of the config
      if opts.config?
        merge_valid_opts(opts)
      else
        @config.merge!(opts)
      end
    end
    
    def load_from_file(file)
      return false if file.nil?
      raise ArgumentError, "Invalid config file: #{file}" unless File.exist? file
      @config.merge!(YAML.load_file(file))
    end

    def merge_valid_opts(opts)
      opts.to_hash.each do |key, value|
        next if value.nil?
        @config[key] = value
      end
    end

  end
end<commit_msg> #3 made merging the config options work better<commit_after>module Rackdis
  class Config

    DEFAULTS = {
      port: 7380,
      address: "0.0.0.0",
      daemonize: false
    }
    
    def initialize(opts)
      @config = DEFAULTS.clone
      process_options opts
    end

    def rack_options
      {
        Port: @config[:port],
        Host: @config[:address],
        daemonize: @config[:daemonize]
      }
    end
    
      process_options opts
    def [](key)
      @config[key]
    end
    
    private
    
    def process_options(opts)
      load_from_file opts[:config]
      
      # We want to merge the command line opts
      # overtop of the config file options but
      # only if they are set because when they
      # are not set they are nil which ends up
      # wiping out all of the config
      opts.each do |key, value|
        next if value.nil?
        @config[key] = value
      end
    end
    
    def load_from_file(file)
      return false if file.nil?
      raise ArgumentError, "Invalid config file: #{file}" unless File.exist? file
      @config.merge!(YAML.load_file(file))
    end

  end
end
{'fraction_non_alphanumeric': 0.08008474576271186, 'fraction_numerical': 0.007203389830508475, 'mean_word_length': 1.9076354679802956, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '771598', 'n_tokens_mistral': 737, 'n_tokens_neox': 722, 'n_words': 226}