Sha256: 199daafd676d81e504b416dfc5354114938e1215a76e5bf23728231e74892673

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module ActiveFunction
  class MissingRouteMethod < Error
    MESSAGE_TEMPLATE = "Missing function route: %s"

    attr_reader :message

    def initialize(context)
      @message = MESSAGE_TEMPLATE % context
    end
  end

  class NotRenderedError < Error
    MESSAGE_TEMPLATE = "render was not called: %s"

    attr_reader :message

    def initialize(context)
      @message = MESSAGE_TEMPLATE % context
    end
  end

  module Functions
    module Core
      attr_reader :action_name, :request, :response

      def dispatch(action_name, request, response)
        @action_name = action_name
        @request     = request
        @response    = response

        raise MissingRouteMethod, @action_name unless respond_to?(action_name)

        process(@action_name)

        raise NotRenderedError, @action_name unless performed?

        @response.to_h
      end

      private

      def process(action) ;  public_send(action); end

      def performed? ;  @response.committed?; end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activefunction-0.3.5 lib/.rbnext/3.0/active_function/functions/core.rb
activefunction-0.3.4 lib/.rbnext/3.0/active_function/functions/core.rb