Sha256: a229ad3d47bd4e4e195e77943278c80a3a09ae4501f2cb9c9fb61d528fc1251e

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

require_relative "../errors/invalid_action_response"
require_relative "../support"
module Moleculer
  module Service
    ##
    # Represents an action
    class Action
      include Support

      # @!attribute [r] name
      #   @return [String] the name of the action
      attr_reader :name, :service

      ##
      # @param name [String|Symbol] the name of the action.
      # @param method [Symbol] the service method to be called.
      # @param service [Moleculer::Service::Base] the moleculer service class
      # @param options [Hash] action options.
      # @option options [Boolean|Hash] :cache if true, will use default caching options, if a hash is provided caching
      # options will reflect the hash.
      # @option options [Hash] params list of param and param types. Can be used to coerce specific params to the
      # provided type.
      def initialize(name, service, method, options = {})
        @name    = name
        @service = service
        @method  = method
        @service = service
        @options = options
      end

      ##
      # @param context [Moleculer::Context] the execution contextd
      #
      # @return [Moleculer::Support::Hash] returns a hash which will be converted into json for the response.
      def execute(context, broker)
        response = @service.new(broker).public_send(@method, context)
        # rubocop:disable Style/RaiseArgs
        raise Errors::InvalidActionResponse.new(response) unless response.is_a? Hash

        response
      end

      def node
        @service.node
      end

      def as_json
        {
          name:    "#{@service.service_name}.#{name}",
          rawName: name,
          cache:   HashUtil.fetch(@options, :cache, false),
          metrics: {
            params: false,
            meta:   true,
          },
        }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
moleculer-0.1.1 lib/moleculer/service/action.rb
moleculer-0.1.0 lib/moleculer/service/action.rb