Sha256: 5c3030cf1184334be57e63cacfd17a13d829d3c4cf2e540d1743e1a8fffd2927

Contents?: true

Size: 1003 Bytes

Versions: 1

Compression:

Stored size: 1003 Bytes

Contents

# frozen_string_literal: true

module Lite
  module Command
    class Complex

      class << self

        def call(*args, **kwargs, &block)
          instance = new(*args, **kwargs, &block)

          raise Lite::Command::NotImplementedError unless instance.respond_to?(:execute)

          instance.call
          instance
        end

        def execute(*args, **kwargs, &block)
          instance = call(*args, **kwargs, &block)
          instance.result
        end

      end

      attr_reader :args, :result

      def initialize(*args)
        @args = args
      end

      def call
        raise Lite::Command::NotImplementedError unless defined?(execute)

        return @result if called?

        @called = true
        @result = execute
      end

      def called?
        @called ||= false
      end

      def recall!
        @called = false
        %i[cache errors].each { |method_name| send(method_name).clear if respond_to?(method_name) }
        call
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lite-command-1.5.0 lib/lite/command/complex.rb