Sha256: e89c7ae1f803adec96950a12a9abb56ab3be09882ae7340ac8dff9d3e3aae4f7

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require_relative "context"
require_relative "failure"

module Ucasy
  class Base
    include Ucasy::Callable

    class << self
      def call(context)
        new(context).perform
      end

      def required_attributes(*attributes)
        @required_attributes = attributes
      end

      def _required_attributes
        @required_attributes || []
      end
    end

    def initialize(context = {})
      @context = Context.build(context)
    end

    def perform
      return if failure?

      validate_context!
      before if respond_to?(:before)
      call
      after if respond_to?(:after)

      self
    rescue Failure
      self
    end

    private

    attr_reader :context

    def validate_context!
      self.class._required_attributes.each do |attribute|
        next if context.respond_to?(attribute)

        raise ArgumentError, "You must set '#{attribute}' variable"
      end
    end

    def method_missing(method_name, *, &block)
      context.public_send(method_name)
    end

    def respond_to_missing?(method_name, include_private = false)
      context.respond_to?(method_name, include_private)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ucasy-0.0.3 lib/ucasy/base.rb