class Seatbelt::Translator

Public: Interface to the implementation sections of TQL. (TranslationQuery Language)

Public Class Methods

config() click to toggle source
# File lib/seatbelt/translator.rb, line 41
def self.config
  Translator::Config
end
setup() { |config| ... } click to toggle source
# File lib/seatbelt/translator.rb, line 37
def self.setup(&block)
  yield(config)
end
tell_me(query) click to toggle source

Public: Takes sentence an delegates it to the responding class.

query - The natural language sentence as String.

Example

Translator.tell_me "Hotel: Find the 2 cheapest near London"
Translator.tell_me "Offer: Find all for three weeks in Finnland"

A query starts with the class name the query should pointed to following by ':'. If this is omitted the class defaults to Offer

# File lib/seatbelt/translator.rb, line 55
def self.tell_me(query)
  model_prefix  = config.namespace
  name_regex    = config.name_regex
  result        = query.scan(name_regex).first
  klass         = result.gsub(":", "") if result.respond_to?(:gsub)
  klass         = config.default_model_class unless klass
  pattern       = query.gsub(name_regex, "").lstrip
  Module.const_get("#{model_prefix}#{klass}").send(:respond,pattern)
end