<commit_before>require_relative '../restful_api'

module Sinatra
  module RestfulApi
    class ConditionsParser
      def self.parse(conditions)
        conditions.empty? ? :all : conditions
      end
    end

    def restful_api(name)
      helpers do
        define_method("#{name}_api") do
          instance_variable_get("@#{name}_api") ||
            instance_variable_set("@#{name}_api", restful_api_for(name))
        end

        def restful_api_for(name)
          klass = name.to_s.singularize.classify.constantize
          settings.restful_api_adapter.new(klass).tap do |adapter|
            restful_json_api_setup(adapter)
          end
        end

        def restful_json_api_setup(adapter)
          json = ::RestfulApi::Json
          json.include_root_in_json = setting_or_default(:include_root_in_json, false)
          adapter.extend json
        end

        def read_conditions
          conditions = params.except('include')
          conditions_parser.parse(conditions)
        end

        def conditions_parser
          setting_or_default(:restful_api_conditions_parser, ConditionsParser)
        end

        def setting_or_default(setting, default)
          settings.respond_to?(setting) ? settings.send(setting) : default
        end
      end

      post "/api/v1/#{name}" do
        send("#{name}_api").create(request.body.read)
      end

      get "/api/v1/#{name}/:id" do
        send("#{name}_api").read(params[:id], include: params[:include])
      end

      get "/api/v1/#{name}" do
        send("#{name}_api").read(read_conditions, include: params[:include])
      end

      put "/api/v1/#{name}/:id" do
        send("#{name}_api").update(params[:id], request.body.read)
      end

      delete "/api/v1/#{name}/:id" do
        send("#{name}_api").destroy(params[:id])
      end
    end
  end

  register RestfulApi
end<commit_msg>pass resource into conditions parser<commit_after>require_relative '../restful_api'

module Sinatra
  module RestfulApi
    class ConditionsParser
      def self.parse(conditions, model)
        conditions.empty? ? :all : conditions
      end
    end

    def restful_api(name)
      helpers do
        define_method("#{name}_api") do
          instance_variable_get("@#{name}_api") ||
            instance_variable_set("@#{name}_api", restful_api_for(name))
        end

        def restful_api_for(name)
          klass = name.to_s.singularize.classify.constantize
          settings.restful_api_adapter.new(klass).tap do |adapter|
            restful_json_api_setup(adapter)
          end
        end

        def restful_json_api_setup(adapter)
          json = ::RestfulApi::Json
          json.include_root_in_json = setting_or_default(:include_root_in_json, false)
          adapter.extend json
        end

        def read_conditions(resource)
          conditions = params.except('include')
          conditions_parser.parse(conditions, resource)
        end

        def conditions_parser
          setting_or_default(:restful_api_conditions_parser, ConditionsParser)
        end

        def setting_or_default(setting, default)
          settings.respond_to?(setting) ? settings.send(setting) : default
        end
      end

      post "/api/v1/#{name}" do
        send("#{name}_api").create(request.body.read)
      end

      get "/api/v1/#{name}/:id" do
        send("#{name}_api").read(params[:id], include: params[:include])
      end

      get "/api/v1/#{name}" do
        api = send("#{name}_api")
        api.read(read_conditions(api), include: params[:include])
      end

      put "/api/v1/#{name}/:id" do
        send("#{name}_api").update(params[:id], request.body.read)
      end

      delete "/api/v1/#{name}/:id" do
        send("#{name}_api").destroy(params[:id])
      end
    end
  end

  register RestfulApi
end
{'fraction_non_alphanumeric': 0.11750850562679926, 'fraction_numerical': 0.0026171159382360636, 'mean_word_length': 2.424731182795699, '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': '1330228', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1131, 'n_words': 205}