Sha256: b16f8d5b9024ae29377c2db2b1bda8ba162c1439835d116c2649ca5eb4c24bfb

Contents?: true

Size: 1.99 KB

Versions: 4

Compression:

Stored size: 1.99 KB

Contents

module Plutonium
  module Interaction
    module Response
      # Base class for interaction responses.
      #
      # This class provides common functionality for handling flash messages
      # and processing responses in controllers.
      #
      # @abstract Subclass and override {#execute} to implement
      #   specific response behavior.
      class Base
        # @return [Array<Array(String, Symbol)>] Flash messages associated with the response.
        attr_reader :flash

        # Initializes a new Response::Base instance.
        def initialize
          @flash = []
        end

        # Processes the response in the context of a controller.
        #
        # @param controller [ActionController::Base] The controller instance.
        # @yield [Object] Executed if the response doesn't handle its own rendering.
        # @return [void]
        def process(controller, &)
          set_flash(controller)
          execute(controller, &)
        end

        # Adds flash messages to the response.
        #
        # @param messages [Array<Array(String, Symbol)>] The messages to add.
        # @return [self]
        def with_flash(messages)
          @flash.concat(messages) unless messages.blank?
          self
        end

        private

        # Sets flash messages in the controller.
        #
        # @param controller [ActionController::Base] The controller instance.
        # @return [void]
        def set_flash(controller)
          @flash.each do |message, type|
            controller.flash[type] = message
          end
        end

        # Executes the response logic.
        #
        # @abstract
        # @param controller [ActionController::Base] The controller instance.
        # @yield [Object] Executed if the response doesn't handle its own rendering.
        # @raise [NotImplementedError] if not implemented in subclass.
        def execute(controller, &)
          raise NotImplementedError, "#{self.class} must implement #execute"
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
plutonium-0.15.0 lib/plutonium/interaction/response/base.rb
plutonium-0.15.0.pre.rc3 lib/plutonium/interaction/response/base.rb
plutonium-0.15.0.pre.rc2 lib/plutonium/interaction/response/base.rb
plutonium-0.15.0.pre.rc1 lib/plutonium/interaction/response/base.rb