Sha256: 4a8c55f7bd4b2ec046fc3fa268263649a9715e3be94fc83291ea5a75b739d1e0

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

module WaterDrop
  # Aspects module which include all available aspects
  module Aspects
    # Base class for all aspects
    class BaseAspect < ::Aspector::Base
      default private_methods: true

      # @param this is an instance on which we execute aspect (original method caller)
      # @param [Hash] options aspect options
      # @param [Array] args original method arguments
      # @param [Block] message block which we evaluate to get a message that we will send
      # @param result original method result
      def handle(this, options, args, message, *result)
        formatter = Formatter.new(
          options,
          args,
          instance_run(this, result, message)
        )

        Event.new(options[:topic], formatter.message).send!
      end

      private

      # Method used to change message block binding, so it will be evaluated
      # in the caller instance context
      # @param this is an instance on which we execute aspect (original method caller)
      # @param result original method call result
      # @param [Block] message block
      def instance_run(this, result, message)
        return this.instance_eval(&message) if message.parameters.empty?

        this.instance_exec(result, message) { |res, block| block.call(res.first) }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
waterdrop-0.1.3 lib/water_drop/aspects/base_aspect.rb
waterdrop-0.1.1 lib/water_drop/aspects/base_aspect.rb
waterdrop-0.1.0 lib/water_drop/aspects/base_aspect.rb