Skip to content

Commit

Permalink
Add first methods and specs
Browse files Browse the repository at this point in the history
  • Loading branch information
docelic committed Jan 7, 2018
1 parent 3b821fb commit 5f9b6f5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
10 changes: 10 additions & 0 deletions 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
15 changes: 15 additions & 0 deletions 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
3 changes: 0 additions & 3 deletions 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
33 changes: 33 additions & 0 deletions 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

0 comments on commit 5f9b6f5

Please sign in to comment.