Sha256: 2dad0ba02de9d2f72d2b620ac0e5cdda6c78466a2e7e938182a43d30b58ed843
Contents?: true
Size: 1.4 KB
Versions: 1
Compression:
Stored size: 1.4 KB
Contents
require 'search_lingo/tokenizer' module SearchLingo class AbstractSearch def initialize(query, scope, tokenizer: Tokenizer) @query = query || '' @scope = scope @tokenizer = tokenizer.new @query end attr_reader :query, :scope, :tokenizer def self.parsers @parsers ||= [] end def self.parser(callable = nil, &block) unless callable || block_given? raise ArgumentError, '.parse must be called with callable or block' end if callable && block_given? warn "WARNING: parse called with callable and block (#{caller.first}" end parsers << (callable || block) end def parsers self.class.parsers end def results @results ||= conditions.inject(scope) do |query, condition| query.public_send(*condition) end end def conditions tokenizer.inject([]) do |conditions, token| conditions << catch(:match) do parse token if token.compound? token = tokenizer.simplify parse token end default_parse token end end end def parse(token) parsers.each do |parser| result = parser.call token throw :match, result if result end end def default_parse(token) raise NotImplementedError, "#default_parse must be implemented by #{self.class}" end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
search_lingo-1.0.0.beta2 | lib/search_lingo/abstract_search.rb |