<commit_before>module Datatable
	class Request
		attr_accessor :sEcho, :iDisplayStart, :iDisplayLength, :iColumns, :sColumns, :mDataProps
		attr_accessor :bSortables, :iSortingCols, :iSortCols, :sSortDirs
		attr_accessor :sSearch, :bRegex, :bSearchables, :bSearches, :sRegexes

		def initialize(opts)
			initialize_basics(opts)
			initialize_sorts(opts)
			initialize_searches(opts)
			return self
		end

		def initialize_basics(opts)
			self.sEcho = opts["sEcho"]
			self.iDisplayStart = opts["iDisplayStart"].to_i
			self.iDisplayLength = opts["iDisplayLength"].to_i
			self.iColumns = opts["iColumns"].to_i
			self.sColumns = opts["sColumns"].split(",")
			self.sSearch = opts["sSearch"]
			self.iSortingCols = opts["iSortingCols"].to_i
		end

		def initialize_sorts(opts)
			self.bSortables = (0..@iColumns-1).collect { |i| opts["bSortable_#{i}"] == "true" }

			self.iSortCols = []
			self.sSortDirs = []
			return if iSortingCols == 0
	
			@iSortingCols.times do |i|
				@iSortCols << opts["iSortCol_#{i}"].to_i
				@sSortDirs << opts["sSortDir_#{i}"].upcase
			end
		end

		def initialize_searches(opts)
			@bSearchables = []
			@bSearches = []
			@sRegexes = []
	
			@iColumns.times do |i|
				@bSearchables << (opts["bSearchable_#{i}"] == "true")
				@bSearches << opts["sSearch_#{i}"]
				@sRegexes << (opts["bRegex_#{i}"] == "true")
			end
		end

		def args_for_find
			args = {select: self.select, limit: @iDisplayLength, offset: @iDisplayStart - 1}
			args[:conditions] = self.conditions if self.searchable?
			args[:order] = self.order if self.sortable?
			return args
		end

		def searchable?
			return true unless @sSearch.empty?
			# return false unless @bSearchables.any? {|v| v}
			(@bSearchables.count).times do |i|
				return true if @bSearchables[i] && !@bSearches[i].empty?
			end
			return false
		end

		def sortable?
			@iSortingCols > 0
		end

		def select
			@sColumns.join(",")
		end

		def order
			res = (0..@iSortingCols-1).collect do |i|
				@sColumns[@iSortCols[i]] + " " + @sSortDirs[i]
			end
			res.join(",")
		end

		def conditions
			res = (0..@iColumns-1).collect do |i|
				if @bSearchables[i] && !@bSearches[i].empty? then
					if @sRegexes[i] then
						"#{sColumns[i]} ~ #{@bSearches[i]}"
					else 
						"#{sColumns[i]} LIKE '#{@bSearches[i]}'"
					end
				else
					nil
				end
			end
			res.compact.join(" AND ")
		end
	end
end<commit_msg>Added @opts as a property, so that it doesn't have to be passed as an argument.<commit_after>module Datatable
	class Request
		attr_accessor :sEcho, :iDisplayStart, :iDisplayLength, :iColumns, :sColumns, :mDataProps
		attr_accessor :bSortables, :iSortingCols, :iSortCols, :sSortDirs
		attr_accessor :sSearch, :bRegex, :bSearchables, :bSearches, :sRegexes
		attr_accessor :select_string, :order_string, :where_string
		attr_accessor :klass, :opts

		def initialize(opts)
			self.opts = opts
			initialize_basics
			initialize_sorts
			initialize_searches
			@select_string = self.construct_select
			@order_string = self.construct_order
			@where_string = self.construct_conditions
			return self
		end

		def initialize_basics
			self.sEcho = @opts["sEcho"]
			self.iDisplayStart = @opts["iDisplayStart"].to_i
			self.iDisplayLength = @opts["iDisplayLength"].to_i
			self.iColumns = @opts["iColumns"].to_i
			self.sColumns = @opts["sColumns"].split(",")
			self.sSearch = @opts["sSearch"]
			self.iSortingCols = @opts["iSortingCols"].to_i
		end

		def initialize_sorts
			self.bSortables = (0..@iColumns-1).collect { |i| @opts["bSortable_#{i}"] == "true" }

			self.iSortCols = []
			self.sSortDirs = []
			return if iSortingCols == 0
	
			@iSortingCols.times do |i|
				@iSortCols << @opts["iSortCol_#{i}"].to_i
				@sSortDirs << @opts["sSortDir_#{i}"].upcase
			end
			
		end

		def initialize_searches
			@bSearchables = []
			@bSearches = []
			@sRegexes = []
	
			@iColumns.times do |i|
				@bSearchables << (@opts["bSearchable_#{i}"] == "true")
				@bSearches << @opts["sSearch_#{i}"]
				@sRegexes << (@opts["bRegex_#{i}"] == "true")
			end
		end

		def args_for_find
			args = {select: @select_string, limit: @iDisplayLength, offset: @iDisplayStart - 1}
			args[:conditions] = self.construct_conditions if self.searchable?
			args[:order] = self.construct_order if self.sortable?
			return args
		end

		def searchable?
			return true unless @sSearch.empty?
			# return false unless @bSearchables.any? {|v| v}
			(@bSearchables.count).times do |i|
				return true if @bSearchables[i] && !@bSearches[i].empty?
			end
			return false
		end

		def sortable?
			@iSortingCols > 0
		end

		def construct_select
			cols = @sColumns
			cols = (0..iColumns-1).collect do |i|
				sSelect = @opts["sSelect_#{i}"]
				if sSelect then
					"#{sSelect} AS #{@sColumns[i]}"
				else
					@sColumns[i]
				end
			end
			return cols.join(',')
		end

		def construct_order
			res = (0..@iSortingCols - 1).collect do |i|
				sort = @opts["sSort_#{i}"]
				if sort then
					sort.gsub("DIR", @sSortDirs[i])
				else
					@sColumns[@iSortCols[i]] + " " + @sSortDirs[i]
				end
			end
			res.join(",")
		end

		def construct_conditions
			res = (0..@iColumns - 1).collect do |i|
				if @bSearchables[i] && !@bSearches[i].empty? then
					if @sRegexes[i] then
						"#{sColumns[i]} ~ '#{@bSearches[i]}'"
					else 
						"#{sColumns[i]} LIKE '#{@bSearches[i]}'"
					end
				else
					nil
				end
			end
			res.compact.join(" AND ")
		end

	end
end
{'fraction_non_alphanumeric': 0.149944669863519, 'fraction_numerical': 0.0036886757654002213, 'mean_word_length': 3.4892384105960264, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 5, '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': '4298966', 'n_tokens_mistral': 2462, 'n_tokens_neox': 2079, 'n_words': 421}