diff --git a/spec/config_spec.cr b/spec/config_spec.cr new file mode 100644 index 0000000..27e36d4 --- /dev/null +++ b/spec/config_spec.cr @@ -0,0 +1,10 @@ +require "./spec_helper" + +describe TextToSqlSearch do + it "can create config" do + t= TextToSqlSearch::Base.new + c = t.config + c.class.should eq TextToSqlSearch::Config + c.operators_allowed.should eq ["<", ">", "=", ":"] + end +end diff --git a/spec/methods_spec.cr b/spec/methods_spec.cr new file mode 100644 index 0000000..d582817 --- /dev/null +++ b/spec/methods_spec.cr @@ -0,0 +1,15 @@ +require "./spec_helper" + +describe TextToSqlSearch do + it "can create config" do + t= TextToSqlSearch::Base.new + c = t.config + + tokens= %w(at stage = 7th leg of long ! dreary road) + + t.peek_next(tokens, 0).should eq :todo + t.peek_next(tokens, 1).should eq :operator + t.peek_next(tokens, 6).should eq :negation + t.peek_next(tokens, 6, ["!"]).should eq :todo + end +end diff --git a/spec/text_to_sql_search_spec.cr b/spec/text_to_sql_search_spec.cr index bb213da..960d523 100644 --- a/spec/text_to_sql_search_spec.cr +++ b/spec/text_to_sql_search_spec.cr @@ -1,9 +1,6 @@ require "./spec_helper" describe TextToSqlSearch do - # TODO: Write tests - it "works" do - false.should eq(true) end end diff --git a/src/text_to_sql_search.cr b/src/text_to_sql_search.cr index cb919fa..c37dd24 100644 --- a/src/text_to_sql_search.cr +++ b/src/text_to_sql_search.cr @@ -1,4 +1,37 @@ +require "yaml" require "./text_to_sql_search/**" module TextToSqlSearch + class Config + @operators_allowed = ["<", ">", "=", ":"] + @negation_words = ["!", "no", "not"] + @ignored_words = ["with", "than"] + + def initialize; end + + YAML.mapping( + operators_allowed: {type: Array(String)}, + negation_words: {type: Array(String)}, + ignored_words: {type: Array(String)}, + ) + end + + class Base + # Instantiates new, default config. Returns existing config on subsequent calls. + def config + @config||= Config.new + end + + # Given list and current position, peeks into upcoming elements to determine their type. + # All decision-making is done based on current config. + def peek_next( list, i, ignored= config.ignored_words) + while el= list[i+=1]? + next if ignored.includes? el + return :operator if config.operators_allowed.includes? el + return :negation if config.negation_words.includes? el + return :todo + end + end + + end end