Sha256: c9c9514cb33d81ae873937b475c36980db8ed186045e29d24af9f4de8bcb83ee

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

require_relative "context"
require_relative "failure"
require_relative "validators/required_attributes"

module Ucasy
  class Base < 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_required_attributes!
      try(:before) if success?
      call if success?
      try(:after) if success?

      self
    rescue Failure
      self
    end

    private

    attr_reader :context

    def validate_required_attributes!
      Validators::RequiredAttributes.call(context, self.class._required_attributes)
    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

2 entries across 2 versions & 1 rubygems

Version Path
ucasy-0.0.5 lib/ucasy/base.rb
ucasy-0.0.4 lib/ucasy/base.rb