Sha256: 3aa7e83ec3ad826ba42194e3837b11d594b8fc896ece7ac228b34b5ea0c8c49c

Contents?: true

Size: 1.78 KB

Versions: 4

Compression:

Stored size: 1.78 KB

Contents

module Adhearsion
  module MenuDSL

    class MenuBuilder

      attr_accessor :patterns, :menu_callbacks

      def initialize
        @patterns = []
        @menu_callbacks = {}
      end

      def build(&block)
        @context = eval "self", block.binding
        instance_eval &block
      end

      def match(*args, &block)
        payload = if block_given?
          raise ArgumentError, "You cannot specify both a block and a controller name." if args.last.is_a? Class
          nil
        else
          raise ArgumentError, "You need to provide a block or a controller name." unless args.last.is_a? Class
          args.pop
        end

        raise ArgumentError, "You cannot call this method without patterns." if args.empty?

        args.each do |pattern|
          @patterns << MatchCalculator.build_with_pattern(pattern, payload, &block)
        end
      end

      def weighted_match_calculators
        @patterns
      end

      def execute_hook_for(symbol, input)
        callback = @menu_callbacks[symbol]
        @context.instance_exec input, &callback
      end

      def invalid(&block)
        raise LocalJumpError, "Must supply a block!" unless block_given?
        @menu_callbacks[:invalid] = block
      end

      def timeout(&block)
        raise LocalJumpError, "Must supply a block!" unless block_given?
        @menu_callbacks[:timeout] = block
      end

      def failure(&block)
        raise LocalJumpError, "Must supply a block!" unless block_given?
        @menu_callbacks[:failure] = block
      end

      def calculate_matches_for(result)
        CalculatedMatchCollection.new.tap do |collection|
          weighted_match_calculators.each do |pattern|
            collection << pattern.match(result)
          end
        end
      end

    end # class MenuBuilder

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
adhearsion-2.0.0.beta1 lib/adhearsion/menu_dsl/menu_builder.rb
adhearsion-2.0.0.alpha3 lib/adhearsion/menu_dsl/menu_builder.rb
adhearsion-2.0.0.alpha2 lib/adhearsion/menu_dsl/menu_builder.rb
adhearsion-2.0.0.alpha1 lib/adhearsion/menu_dsl/menu_builder.rb