Sha256: 2f98f202a0a4bd503ef186b30404cf3fe7559d54cc0cdad26ba56db20291a9fb

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

class BCDD::Result
  module Mixin
    module Factory
      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

    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(config_flags)
        Config::Options.addon(map: config_flags, from: OPTIONS)
      end
    end
  end

  def self.mixin(config: nil)
    addons = mixin_module::Addons.options(config)

    mod = mixin_module::Factory.module!
    mod.send(:include, mixin_module::Methods)
    mod.const_set(:Result, result_factory)
    mod.send(:include, *addons) unless addons.empty?
    mod
  end

  def self.mixin_module
    Mixin
  end

  def self.result_factory
    ::BCDD::Result
  end

  private_class_method :mixin_module, :result_factory
end

Version data entries

1 entries across 1 versions & 1 rubygems

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