Sha256: 75e6f1f591b43b4f99b4a7ab839071d76ef916cc893d8ca128a397e5cca8c693

Contents?: true

Size: 1023 Bytes

Versions: 1

Compression:

Stored size: 1023 Bytes

Contents

# frozen_string_literal: true

class BCDD::Result
  module Mixin
    module Methods
      def Success(type, value = nil)
        Success.new(type: type, value: value, subject: self)
      end

      def Failure(type, value = nil)
        Failure.new(type: type, value: value, subject: self)
      end
    end

    module Addons
      module Continuable
        private def Continue(value)
          Success(:continued, value)
        end
      end

      OPTIONS = { Continue: Continuable }.freeze

      def self.options(names)
        Array(names).filter_map { |name| OPTIONS[name] }
      end
    end

    def self.module!
      ::Module.new do
        def self.included(base); base.const_set(:ResultMixin, self); end
        def self.extended(base); base.const_set(:ResultMixin, self); end
      end
    end
  end

  def self.mixin(with: nil)
    addons = Mixin::Addons.options(with)

    mod = Mixin.module!
    mod.send(:include, Mixin::Methods)
    mod.send(:include, *addons) unless addons.empty?
    mod
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bcdd-result-0.7.0 lib/bcdd/result/mixin.rb