<commit_before>require "psych"
require "thor"

module Nit
  class Config < Thor::Group # we have to derive from Group to get the create_file working - that's messy.
    include Thor::Actions

    class File
      def initialize(name, app) # FIXME: i hate the app dependency for #create_file.
        @name = name
        @app  = app
      end

      def read(key)
        return unless exist?
        yaml_hash[key]
      end

      def write(key, value)
        @app.create_file(@name) unless exist?

        hash = yaml_hash
        hash[key] = value
        ::File.open(@name, "w") { |f| f.write(Psych.dump(hash))  }
      end

      def rm!
        @app.remove_file(@name)
      end

    private
      def exist?
        ::File.exist?(@name)
      end



      def yaml_hash
        Psych.load_file(@name) || {}
      end
    end


    def initialize
      super
      @file = File.new(filename, self) # FIXME: remove the Thor::App dependency, see above!
    end

    def ignored_files
      file.read("ignored_files") or []
    end

    def add_ignored_files(*files)
      arr = ignored_files
      arr += files
      file.write("ignored_files", arr)
    end

  private
    attr_reader :file<commit_msg>convert File instance to_s before serializing.<commit_after>require "psych"
require "thor"

module Nit
  class Config < Thor::Group # we have to derive from Group to get the create_file working - that's messy.
    include Thor::Actions

    class File
      def initialize(name, app) # FIXME: i hate the app dependency for #create_file.
        @name = name
        @app  = app
      end

      def read(key)
        return unless exist?
        yaml_hash[key]
      end

      def write(key, value)
        @app.create_file(@name) unless exist?

        hash = yaml_hash
        hash[key] = value
        ::File.open(@name, "w") { |f| f.write(Psych.dump(hash))  }
      end

      def rm!
        @app.remove_file(@name)
      end

    private
      def exist?
        ::File.exist?(@name)
      end

      def yaml_hash
        Psych.load_file(@name) || {}
      end
    end


    def initialize
      super
      @file = File.new(filename, self) # FIXME: remove the Thor::App dependency, see above!
    end

    def ignored_files
      file.read("ignored_files") or []
    end

    def add_ignored_files(*files)
      arr = ignored_files
      arr += files.collect(&:to_s)
      file.write("ignored_files", arr)
    end

  private
    attr_reader :file
{'fraction_non_alphanumeric': 0.09825416159155502, 'fraction_numerical': 0.0, 'mean_word_length': 2.0646766169154227, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5585488', 'n_tokens_mistral': 787, 'n_tokens_neox': 746, 'n_words': 222}