Sha256: bb466c87a908d457986cd95c031768bfa31e2c2625a1ad363358451f6b4f67d2

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Kind
  module Checkable
    def class?(value)
      Kind::Is.__call__(__kind, value)
    end

    def instance(value, options = Empty::HASH)
      default = options[:or]

      return Kind::Of.(__kind, value) if ::Kind::Maybe::Value.none?(default)

      value != Kind::Undefined && instance?(value) ? value : Kind::Of.(__kind, default)
    end

    def [](value, options = options = Empty::HASH)
      instance(value, options)
    end

    def instance?(value = Kind::Undefined)
      return __is_instance__(value) if value != Kind::Undefined

      to_proc
    end

    def __is_instance__(value)
      value.is_a?(__kind)
    end

    def to_proc
      @to_proc ||=
        -> checker { -> value { checker.__is_instance__(value) } }.call(self)
    end

    def or_nil(value)
      return value if instance?(value)
    end

    def or_undefined(value)
      or_nil(value) || Kind::Undefined
    end

    def as_maybe(value = Kind::Undefined)
      return __as_maybe__(value) if value != Kind::Undefined

      as_maybe_to_proc
    end

    def as_optional(value = Kind::Undefined)
      as_maybe(value)
    end

    def __as_maybe__(value)
      Kind::Maybe.new(or_nil(value))
    end

    def as_maybe_to_proc
      @as_maybe_to_proc ||=
        -> checker { -> value { checker.__as_maybe__(value) } }.call(self)
    end
  end

  private_constant :Checkable

  class Checker
    include Checkable

    attr_reader :__kind

    def initialize(kind)
      @__kind = kind
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kind-2.0.0 lib/kind/checker.rb
kind-1.9.0 lib/kind/checker.rb