Sha256: 7ccaa347e23eb9a0f2c07f2ddd5d8fb0f5ebef1b298a512793abfee18bdc31d0

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

module Temple
  module Mixins
    module Dispatcher
      def self.included(base)
        base.class_eval { extend ClassMethods }
      end

      def compile(exp)
        compile!(exp)
      end

      def compile!(exp)
        type, *args = exp
        if respond_to?("on_#{type}")
          send("on_#{type}", *args)
        else
          exp
        end
      end

      def on_multi(*exps)
        [:multi, *exps.map {|exp| compile!(exp) }]
      end

      def on_capture(name, exp)
        [:capture, name, compile!(exp)]
      end

      module ClassMethods
        def temple_dispatch(*bases)
          bases.each do |base|
            class_eval %{def on_#{base}(type, *args)
              if respond_to?("on_" #{base.to_s.inspect} "_\#{type}")
                send("on_" #{base.to_s.inspect} "_\#{type}", *args)
              else
                [:#{base}, type, *args]
              end
            end}
          end
        end
      end
    end

    module Options
      def self.included(base)
        base.class_eval { extend ClassMethods }
      end

      attr_reader :options

      def initialize(options = {})
        @options = self.class.default_options.merge(options)
      end

      module ClassMethods
        def set_default_options(opts)
          default_options.merge!(opts)
        end

        def default_options
          @default_options ||= if superclass.respond_to?(:default_options)
                                 Hash.new {|hash, key| superclass.default_options[key] }
                               else
                                 {}
                               end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
temple-0.1.8 lib/temple/mixins.rb
temple-0.1.7 lib/temple/mixins.rb
temple-0.1.6 lib/temple/mixins.rb