class Core::Handler::Compiler

public

Public Class Methods

compile(callable) click to toggle source
public
# File lib/core/handler/compiler.rb, line 11
        def compile(callable)
          <<~CODE
            def trigger(object, event, ...)
            #{compile_trigger(callable)}
            end
          CODE
        end

Private Class Methods

compile_trigger(callable) click to toggle source
# File lib/core/handler/compiler.rb, line 19
        def compile_trigger(callable)
  cache_lookup = {}
  callable.instance_variable_set(:@__cache_lookup__, cache_lookup)

  compiled = +"event = event.to_sym if event.is_a?(String)\n"

  callable.pipelines.each do |event, pipeline|
    case event
    when :__core_event_global__
      # skip till end
    when String, Symbol
      compiled << "call_handler(object, #{event.to_sym.inspect}, event, ...) if event == #{event.to_sym.inspect}\n"
    when Class
      if event.name
        compiled << "call_handler(object, #{event}, event, ...) if event.is_a?(#{event})\n"
      else
        cache_key = event.object_id
        cache_lookup[cache_key] = event
        compiled << "cache_value = @__cache_lookup__[#{cache_key}]\n"
        compiled << "call_handler(object, cache_value, event, ...) if event.is_a?(cache_value)\n"
      end
    else
      raise "expected handler event to be string, symbol, or class; received: #{event.class}"
    end
  end

  compiled << "call_handler(object, :__core_event_global__, event, ...)\n"

  compiled
end