Module: CommandSearch

Defined in:
lib/command_search.rb,
lib/command_search/lexer.rb,
lib/command_search/memory.rb,
lib/command_search/parser.rb,
lib/command_search/aliaser.rb,
lib/command_search/mongoer.rb,
lib/command_search/optimizer.rb,
lib/command_search/command_dealiaser.rb

Defined Under Namespace

Modules: Aliaser, CommandDealiaser, Lexer, Memory, Mongoer, Optimizer, Parser

Class Method Summary collapse

Class Method Details

.search(source, query, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/command_search.rb', line 14

def search(source, query, options = {})
  aliases = options[:aliases] || {}
  fields = options[:fields] || []
  command_fields = options[:command_fields] || {}

  aliased_query = Aliaser.alias(query, aliases)
  tokens = Lexer.lex(aliased_query)
  parsed = Parser.parse!(tokens)
  dealiased = CommandDealiaser.dealias(parsed, command_fields)
  cleaned = CommandDealiaser.decompose_unaliasable(dealiased, command_fields)
  opted = Optimizer.optimize(cleaned)

  if source.respond_to?(:mongo_client) && source.queryable
    fields = [:__CommandSearch_mongo_fields_dummy_key__] if fields.empty?
    mongo_query = Mongoer.build_query(opted, fields, command_fields)
    return source.where(mongo_query)
  end

  selector = Memory.build_query(opted, fields, command_fields)
  source.select(&selector)
end