Sha256: 7276d986c2aacce7113f50d83dbd6ceb4ce43a8eb37c3c6f12aae71af88aa691

Contents?: true

Size: 1.24 KB

Versions: 21

Compression:

Stored size: 1.24 KB

Contents

class Usher
  module Interface
    class Text
      
      def initialize(&blk)
        @usher = Usher.new(:delimiters => [' '], :generator => Usher::Util::Generators::Generic.new)
        instance_eval(&blk) if blk
      end
      
      def generate(name, params = nil)
        @usher.generator.generate(name, params)
      end
      
      def on(text, name = nil, &blk)
        r = @usher.add_route(text).to(:block => blk, :arg_type => :array)
        r.name(name) if name
      end
      
      def unrecognized(&blk)
        @unrecognize_block = blk
      end
      
      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
      
      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

    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
usher-0.8.3 lib/usher/interface/text.rb
usher-0.8.2 lib/usher/interface/text.rb
usher-0.8.1 lib/usher/interface/text.rb
usher-0.8.0 lib/usher/interface/text.rb
usher-0.7.5 lib/usher/interface/text.rb
usher-0.7.4 lib/usher/interface/text.rb
usher-0.7.3 lib/usher/interface/text.rb
usher-0.7.2 lib/usher/interface/text.rb
usher-0.7.1 lib/usher/interface/text.rb
usher-0.7.0 lib/usher/interface/text.rb
usher-0.6.8 lib/usher/interface/text.rb
usher-0.6.7 lib/usher/interface/text.rb
usher-0.6.6 lib/usher/interface/text.rb
usher-0.6.5 lib/usher/interface/text.rb
usher-0.6.4 lib/usher/interface/text.rb
usher-0.6.3 lib/usher/interface/text.rb
usher-0.6.2 lib/usher/interface/text.rb
usher-0.6.1 lib/usher/interface/text.rb
usher-0.6.0 lib/usher/interface/text.rb
usher-0.5.13 lib/usher/interface/text.rb