Class: Seatbelt::Translator

Inherits:
Object
  • Object
show all
Defined in:
lib/seatbelt/translator.rb

Overview

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

Defined Under Namespace

Classes: Config

Class Method Summary collapse

Class Method Details

.configObject



41
42
43
# File 'lib/seatbelt/translator.rb', line 41

def self.config
  Translator::Config
end

.setup {|config| ... } ⇒ Object

Yields:



37
38
39
# File 'lib/seatbelt/translator.rb', line 37

def self.setup(&block)
  yield(config)
end

.tell_me(query) ⇒ Object

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



55
56
57
58
59
60
61
62
63
# 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