<commit_before>require "multi_json"

module Rulers
  module Model
    class FileModel
      @@cache = {}

      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.decode(obj)
      end

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

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

      def self.find(id)
        path = "db/quotes/#{id}.json"
        begin
          if should_be_cached?(id)
            @@cache[path][:file_model]
          else
            @@cache[path] = Hash.new
            @@cache[path][:cache_time] = Time.now
            @@cache[path][:file_model] = FileModel.new(path)
          end
        rescue => e
          raise Rulers::Exceptions::RecordNotFound.new("Record was not found: " << e.message)
        end
      end

      def self.all
        files = Dir["db/quotes/*.json"]
        files.map { |f| FileModel.new f }
      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"]}",
              "attribution": "#{hash["attribution"]}"
            }
          TEMPLATE
        end
        
        FileModel.new("db/quotes/#{id}.json") 
      end

      def self.update(id, attrs)
        # Tested using the following POST
        # curl "http://localhost:3001/quotes/update_quote" -d id=6 -d attribs='{"quote": "testing"}'
        file = File.read("db/quotes/#{id}.json")
        hash = MultiJson.decode(file)
        attributes = MultiJson.decode(attrs)
        hash = hash.merge(attributes)
        save(id, hash)
      end

      def self.save(id, hash_data)
        json_data = MultiJson.encode(hash_data)
        File.open("db/quotes/#{id}.json", "w") do |f|
          f.write json_data
        end
      end

      def self.find_all_by_submitter(attrs)
        attributes = MultiJson.decode(attrs)
        results = []
        all.each do |file|
          results << file if file[:submitter] == attributes["submitter"]
        end
        results
      end

      def self.should_be_cached?(id)
        path = "db/quotes/#{id}.json"
        begin
          @@cache[path] && @@cache[path][:cache_time] >= File.mtime(path) ? true : false
        rescue
          false
        end
      end
    end
  end
end
<commit_msg>add method_missing and respond_to_missing for filemodel.all queries<commit_after>require "multi_json"

module Rulers
  module Model
    class FileModel
      @@cache = {}

      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.decode(obj)
      end

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

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

      def self.find(id)
        path = "db/quotes/#{id}.json"
        begin
          if should_be_cached?(id)
            @@cache[path][:file_model]
          else
            @@cache[path] = Hash.new
            @@cache[path][:cache_time] = Time.now
            @@cache[path][:file_model] = FileModel.new(path)
          end
        rescue => e
          raise Rulers::Exceptions::RecordNotFound.new("Record was not found: " << e.message)
        end
      end

      def self.all
        files = Dir["db/quotes/*.json"]
        files.map { |f| FileModel.new f }
      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"]}",
              "attribution": "#{hash["attribution"]}"
            }
          TEMPLATE
        end
        
        FileModel.new("db/quotes/#{id}.json") 
      end

      def self.update(id, attrs)
        # Tested using the following POST
        # curl "http://localhost:3001/quotes/update_quote" -d id=6 -d attribs='{"quote": "testing"}'
        file = File.read("db/quotes/#{id}.json")
        hash = MultiJson.decode(file)
        attributes = MultiJson.decode(attrs)
        hash = hash.merge(attributes)
        save(id, hash)
      end

      def self.save(id, hash_data)
        json_data = MultiJson.encode(hash_data)
        File.open("db/quotes/#{id}.json", "w") do |f|
          f.write json_data
        end
      end

      def self.find_all_by_submitter(attrs)
        attributes = MultiJson.decode(attrs)
        results = []
        all.each do |file|
          results << file if file[:submitter] == attributes["submitter"]
        end
        results
      end

      def self.should_be_cached?(id)
        path = "db/quotes/#{id}.json"
        begin
          @@cache[path] && @@cache[path][:cache_time] >= File.mtime(path) ? true : false
        rescue
          false
        end
      end

      def self.find_all_by_attribute(attribute, attrs)
        attributes = MultiJson.decode(attrs)
        results = []
        all.each do |file|
          results << file if file[attribute.to_sym] == attributes[attribute.to_s]
        end
        results
      end

      def self.method_missing(method_name, *args, &block)
        if method_name.to_s =~ /^find_all_by_(.*)/
          find_all_by_attribute($1, *args)
        else
          super
        end
      end

      def self.respond_to_missing?(method_name, include_private = false)
        method_name.to_s.start_with?('find_all_by_') || super
      end

    end
  end
end

{'fraction_non_alphanumeric': 0.14263661623958013, 'fraction_numerical': 0.003859215807347947, 'mean_word_length': 1.8120659722222223, 'pattern_counts': {'":': 8, '<': 17, '<?xml version=': 0, '>': 7, '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': '888637', 'n_tokens_mistral': 2089, 'n_tokens_neox': 2033, 'n_words': 415}