Sha256: 91240c9a1f577c2fe692fa0a231ef814ae10a81e6bd53d9de0022aa9422acdfb

Contents?: true

Size: 1.25 KB

Versions: 8

Compression:

Stored size: 1.25 KB

Contents

class Usher
  module Interface
    class TextInterface
      
      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

8 entries across 8 versions & 2 rubygems

Version Path
joshbuddy-usher-0.5.6 lib/usher/interface/text_interface.rb
joshbuddy-usher-0.5.7 lib/usher/interface/text_interface.rb
usher-0.5.11 lib/usher/interface/text_interface.rb
usher-0.5.10 lib/usher/interface/text_interface.rb
usher-0.5.8 lib/usher/interface/text_interface.rb
usher-0.5.7 lib/usher/interface/text_interface.rb
usher-0.5.6 lib/usher/interface/text_interface.rb
usher-0.5.5 lib/usher/interface/text_interface.rb