Sha256: 31a5929567aa6c49613584e50ce0aa4a3a55e7a0803de3ded5d1ed180a30ea05

Contents?: true

Size: 1.46 KB

Versions: 10

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require "pakyow/support/class_state"
require "pakyow/support/makeable"
require "pakyow/support/pipeline"
require "pakyow/support/pipeline/object"

require "pakyow/behavior/verification"

module Pakyow
  class Operation
    extend Support::ClassState
    class_state :__handlers, default: {}, inheritable: true

    extend Support::Makeable

    include Support::Pipeline
    include Support::Pipeline::Object

    attr_reader :app, :values

    include Behavior::Verification
    verifies :values

    def initialize(app:, **values)
      @app, @values = app, values
    end

    def perform
      call(self)
    rescue => error
      if handler = self.class.__handlers[error.class] || self.class.__handlers[:global]
        instance_exec(&handler); self
      else
        raise error
      end
    end

    def method_missing(name, *args, &block)
      name = name.to_sym
      if @values.key?(name)
        @values[name.to_sym]
      else
        super
      end
    end

    def respond_to_missing?(name, include_private = false)
      @values.key?(name.to_sym) || super
    end

    class << self
      # Perform input verification before performing the operation
      #
      # @see Pakyow::Verifier
      #
      def verify(&block)
        define_method :__verify do
          verify(&block)
        end

        action :__verify
      end

      def handle(error = nil, &block)
        @__handlers[error || :global] = block
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pakyow-core-1.0.6 lib/pakyow/operation.rb
pakyow-core-1.0.5 lib/pakyow/operation.rb
pakyow-core-1.0.4 lib/pakyow/operation.rb
pakyow-core-1.0.3 lib/pakyow/operation.rb
pakyow-core-1.0.2 lib/pakyow/operation.rb
pakyow-core-1.0.1 lib/pakyow/operation.rb
pakyow-core-1.0.0 lib/pakyow/operation.rb
pakyow-core-1.0.0.rc5 lib/pakyow/operation.rb
pakyow-core-1.0.0.rc4 lib/pakyow/operation.rb
pakyow-core-1.0.0.rc3 lib/pakyow/operation.rb