Class: Usher::Interface::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/usher/interface/text.rb

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ Text

Returns a new instance of Text.



5
6
7
8
# File 'lib/usher/interface/text.rb', line 5

def initialize(&blk)
  @usher = Usher.new(:delimiters => [' '], :generator => Usher::Util::Generators::Generic.new)
  instance_eval(&blk) if blk
end

Instance Method Details

#generate(name, params = nil) ⇒ Object



10
11
12
# File 'lib/usher/interface/text.rb', line 10

def generate(name, params = nil)
  @usher.generator.generate(name, params)
end

#on(text, name = nil, &blk) ⇒ Object



14
15
16
17
# File 'lib/usher/interface/text.rb', line 14

def on(text, name = nil, &blk)
  r = @usher.add_route(text).to(:block => blk, :arg_type => :array)
  r.name(name) if name
end

#on_with_hash(text, name = nil, &blk) ⇒ Object



23
24
25
26
# File 'lib/usher/interface/text.rb', line 23

def on_with_hash(text, name = nil, &blk)
  r = @usher.add_route(text).to(:block => blk, :arg_type => :hash)
  r.name(name) if name
end

#run(text) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/usher/interface/text.rb', line 28

def run(text)
  response = @usher.recognize_path(text.strip)
  if response
    case response.path.route.destination[:arg_type]
    when :hash
      response.path.route.destination[:block].call(response.params.inject({}){|h,(k,v)| h[k]=v; h })
    when :array
      response.path.route.destination[:block].call(*response.params.collect{|p| p.last})
    end
  else
    @unrecognize_block ? @unrecognize_block.call(text) : nil
  end
end

#unrecognized(&blk) ⇒ Object



19
20
21
# File 'lib/usher/interface/text.rb', line 19

def unrecognized(&blk)
  @unrecognize_block = blk
end