Sha256: 35da77b5a13f3b917258254db06a400ec7df95e0496d537051e81026955632e6

Contents?: true

Size: 1.68 KB

Versions: 6

Compression:

Stored size: 1.68 KB

Contents

module Lucid
  module AST
    class TDLWalker
      attr_accessor :configuration #:nodoc:
      attr_reader   :runtime #:nodoc:

      def initialize(runtime, listeners = [], configuration = Lucid::Configuration.default)
        @runtime, @listeners, @configuration = runtime, listeners, configuration
      end

      # This is being used to forward on messages from the AST to
      # the formatters. This is being done in lieu of the explicit
      # forwarding that was previously done.
      def method_missing(message, *args, &block)
        broadcast_message(message, *args, &block)
      end

      def visit_multiline_arg(multiline_arg) #:nodoc:
        broadcast(multiline_arg) do
          multiline_arg.accept(self)
        end
      end

      private

      def broadcast(*args, &block)
        message = extract_method_name_from(caller[0])
        broadcast_message message, *args, &block
        self
      end

      def broadcast_message(message, *args, &block)
        return self if Lucid.wants_to_quit
        message = message.to_s.gsub('visit_', '')
        if block_given?
          send_to_all("before_#{message}", *args)
          yield if block_given?
          send_to_all("after_#{message}", *args)
        else
          send_to_all(message, *args)
        end
        self
      end

      def send_to_all(message, *args)
        @listeners.each do |listener|
          if listener.respond_to?(message)
            listener.__send__(message, *args)
          end
        end
      end
      def extract_method_name_from(call_stack)
        #call_stack[0].match(/in `(.*)'/).captures[0]
        match = call_stack.match(/in `(.*)'/)
        match.captures[0]
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lucid-0.3.3 lib/lucid/ast/tdl_walker.rb
lucid-0.3.0 lib/lucid/ast/tdl_walker.rb
lucid-0.2.1 lib/lucid/ast/tdl_walker.rb
lucid-0.2.0 lib/lucid/ast/tdl_walker.rb
lucid-0.1.1 lib/lucid/ast/tdl_walker.rb
lucid-0.1.0 lib/lucid/ast/tdl_walker.rb