<commit_before>require "multi_json"

module Rulers
  module Model
    class FileModel
      attr_reader :id

      def initialize (filename)
        @filename = filename

        # If filename is "dir/37.json", @id is 37
        basename = File.split(filename)[-1]
        @id = File.basename(basename, ".json").to_i

        obj = File.read(filename)
        @hash = MultiJson.load(obj)
      end 

      def [](name)
        @hash[name.to_s]
      end 

      def []=(name, value)
        @hash[name.to_s] = value
      end 

      def self.find(id)
        begin 
          FileModel.new("db/quotes/#{id}.json")
        rescue 
          return nil
        end 
      end 

      def self.all
        files = Dir["db/quotes/*json"]
        files.map { |f| FileModel.new f }
      end 

      def self.save(model)
        File.open("db/quotes/#{model.id}.json", "w") do |f|
          f.write <<-TEMPLATE
      {
        "submitter": "#{hash ["submitter"]}",
        "quote": "#{hash["quote"]}",
        "attribution": "#{hash["attribution"]}"
      }
        TEMPLATE
        end
      end

      def self.create(attrs)
        hash = {}
        hash["submitter"] = attrs["submitter"] || ""
        hash ["quote"] = attrs["quote"] || ""
        hash["attribution"] = attrs["attribution"] || ""

        files = Dir["db/quotes/*json"]
        names = files.map { |f| f.split("/")[-1] }
        highest = names.map { |b| b.to_i }.max 
        id = highest + 1 
        File.open("db/quotes/#{id}.json", "w") do |f|
          f.write <<-TEMPLATE
      {
        "submitter": "#{hash ["submitter"]}",
        "quote": "#{hash["quote"]}",<commit_msg>ch 5 ex 1 II<commit_after>require "multi_json"

module Rulers
  module Model
    class FileModel
      attr_reader :id

      def initialize (filename)
        @filename = filename

        # If filename is "dir/37.json", @id is 37
        basename = File.split(filename)[-1]
        @id = File.basename(basename, ".json").to_i

        obj = File.read(filename)
        @hash = MultiJson.load(obj)
      end 

      def [](name)
        @hash[name.to_s]
      end 

      def []=(name, value)
        @hash[name.to_s] = value
      end 

      def self.find(id)
        begin 
          FileModel.new("db/quotes/#{id}.json")
        rescue 
          return nil
        end 
      end 

      def self.all
        files = Dir["db/quotes/*json"]
        files.map { |f| FileModel.new f }
      end 

      def self.save(model)
        id = model.id
        hash = {}
        hash["submitter"] = model["submitter"] || ""
        hash ["quote"] = model["quote"] || ""
        hash["attribution"] = model["attribution"] || ""

        File.open("db/quotes/#{id}.json", "w") do |f|
          f.write <<-TEMPLATE
      {
        "submitter": "#{hash ["submitter"]}",
        "quote": "#{hash["quote"]}",
        "attribution": "#{hash["attribution"]}"
      }
        TEMPLATE
        end
        
        FileModel.new "db/quotes/#{id}.json"
      end

      def self.create(attrs)
        hash = {}
        hash["submitter"] = attrs["submitter"] || ""
        hash ["quote"] = attrs["quote"] || ""
        hash["attribution"] = attrs["attribution"] || ""

        files = Dir["db/quotes/*json"]
        names = files.map { |f| f.split("/")[-1] }
        highest = names.map { |b| b.to_i }.max 
        id = highest + 1 
        File.open("db/quotes/#{id}.json", "w") do |f|
          f.write <<-TEMPLATE
      {
        "submitter": "#{hash ["submitter"]}",
        "quote": "#{hash["quote"]}",
{'fraction_non_alphanumeric': 0.17297908422837763, 'fraction_numerical': 0.0045223289994347085, 'mean_word_length': 1.7498057498057498, 'pattern_counts': {'":': 10, '<': 11, '<?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': '738390', 'n_tokens_mistral': 1196, 'n_tokens_neox': 1140, 'n_words': 241}