Sha256: 56d7096bcb2bd49574b9ed2d9035fbaca5e07ad94553eb647ebfedb4222f2fd7

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

require "core/inspect"
require "core/state"
require "securerandom"

module Core
  module Operation
    # [public] An operation action.
    #
    class Action
      include Core::State
      state :__used_random_suffixes__, default: []

      class << self
        # [public] Builds an action for a given target.
        #
        def build(target = nil, *args, &block)
          raise ArgumentError, "actions must be defined with a block" unless block

          Actions::Block.new(target, &block)
        end

        def build_reference
          suffix = generate_random_suffix
          if __used_random_suffixes__.include?(suffix)
            build_reference
          else
            __used_random_suffixes__ << suffix
            "action_#{suffix}"
          end
        end

        def generate_random_suffix
          SecureRandom.hex(8)
        end
      end

      include Core::Inspect

      def initialize(name)
        @name = (name || :call).to_sym
        @reference = self.class.build_reference.to_sym
      end

      # [public] The action name.
      #
      attr_reader :name

      # [public] The action reference.
      #
      attr_reader :reference

      # [public] Finalizes the action for the given context.
      #
      def finalize(context)
        raise "not implemented"
      end

      require_relative "actions/block"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
core-operation-0.2.0 lib/core/operation/action.rb