Module: Kind

Extended by:
Kind, Either::Methods, Result::Methods
Included in:
Kind
Defined in:
lib/kind/any.rb,
lib/kind/dig.rb,
lib/kind/try.rb,
lib/kind/enum.rb,
lib/kind/basic.rb,
lib/kind/empty.rb,
lib/kind/maybe.rb,
lib/kind/monad.rb,
lib/kind/action.rb,
lib/kind/either.rb,
lib/kind/result.rb,
lib/kind/objects.rb,
lib/kind/version.rb,
lib/kind/function.rb,
lib/kind/presence.rb,
lib/kind/enum/item.rb,
lib/kind/validator.rb,
lib/kind/__lib__/of.rb,
lib/kind/functional.rb,
lib/kind/maybe/none.rb,
lib/kind/maybe/some.rb,
lib/kind/basic/error.rb,
lib/kind/either/left.rb,
lib/kind/maybe/monad.rb,
lib/kind/maybe/typed.rb,
lib/kind/objects/nil.rb,
lib/kind/__lib__/kind.rb,
lib/kind/either/monad.rb,
lib/kind/either/right.rb,
lib/kind/enum/methods.rb,
lib/kind/result/monad.rb,
lib/kind/maybe/methods.rb,
lib/kind/maybe/wrapper.rb,
lib/kind/__lib__/strict.rb,
lib/kind/either/methods.rb,
lib/kind/objects/object.rb,
lib/kind/result/failure.rb,
lib/kind/result/methods.rb,
lib/kind/result/success.rb,
lib/kind/basic/undefined.rb,
lib/kind/objects/not_nil.rb,
lib/kind/result/abstract.rb,
lib/kind/strict/disabled.rb,
lib/kind/functional/steps.rb,
lib/kind/__lib__/undefined.rb,
lib/kind/functional/action.rb,
lib/kind/__lib__/attributes.rb,
lib/kind/objects/respond_to.rb,
lib/kind/objects/union_type.rb,
lib/kind/__lib__/assert_hash.rb,
lib/kind/maybe/monad/wrapper.rb,
lib/kind/__lib__/action_steps.rb,
lib/kind/either/monad/wrapper.rb,
lib/kind/immutable_attributes.rb,
lib/kind/objects/basic_object.rb,
lib/kind/result/monad/wrapper.rb,
lib/kind/objects/modules/core/io.rb,
lib/kind/objects/modules/core/file.rb,
lib/kind/objects/modules/core/hash.rb,
lib/kind/objects/modules/core/proc.rb,
lib/kind/objects/modules/core/time.rb,
lib/kind/objects/modules/core/array.rb,
lib/kind/objects/modules/core/class.rb,
lib/kind/objects/modules/core/float.rb,
lib/kind/objects/modules/core/queue.rb,
lib/kind/objects/modules/core/range.rb,
lib/kind/objects/modules/stdlib/set.rb,
lib/kind/immutable_attributes/reader.rb,
lib/kind/objects/modules/core/method.rb,
lib/kind/objects/modules/core/module.rb,
lib/kind/objects/modules/core/regexp.rb,
lib/kind/objects/modules/core/string.rb,
lib/kind/objects/modules/core/struct.rb,
lib/kind/objects/modules/core/symbol.rb,
lib/kind/objects/modules/core/integer.rb,
lib/kind/objects/modules/core/numeric.rb,
lib/kind/objects/modules/custom/lambda.rb,
lib/kind/objects/modules/custom/boolean.rb,
lib/kind/objects/modules/core/comparable.rb,
lib/kind/objects/modules/core/enumerable.rb,
lib/kind/objects/modules/core/enumerator.rb,
lib/kind/objects/modules/custom/callable.rb,
lib/kind/immutable_attributes/initializer.rb,
lib/kind/objects/modules/stdlib/open_struct.rb

Defined Under Namespace

Modules: Action, Array, AssertHash, BasicObject, Boolean, Callable, Class, Comparable, Dig, Either, Empty, Enum, Enumerable, Enumerator, File, Float, Function, Functional, Hash, IO, ImmutableAttributes, Integer, Lambda, Maybe, Method, Module, Monad, Nil, NotNil, Numeric, Object, OpenStruct, Presence, Proc, Queue, Range, Regexp, Result, Set, String, Struct, Symbol, Time, Try, Validator Classes: Any, Error, RespondTo, UnionType

Constant Summary collapse

Optional =
Maybe
None =
Maybe::NONE_INSTANCE
VERSION =
'5.10.0'
Undefined =
Object.new.tap do |undefined|
  def undefined.inspect
    @inspect ||= 'Kind::Undefined'.freeze
  end
  undefined.inspect

  def undefined.empty?
    true
  end

  def undefined.to_s
    inspect
  end

  def undefined.clone
    self
  end

  def undefined.dup
    clone
  end

  def undefined.default(value, default)
    return value if self != value

    default.respond_to?(:call) ? default.call : default
  end

  undefined.freeze
end

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Either::Methods

Left, Right, included

Methods included from Result::Methods

Failure, Success, included

Class Method Details

.[](kind, opt = Empty::HASH) ⇒ Object

Kind[]



45
46
47
# File 'lib/kind/objects/object.rb', line 45

def self.[](kind, opt = Empty::HASH)
  Object::Instance.new(kind, opt)
end

.Array?(*values) ⇒ Boolean

Returns:



16
17
18
# File 'lib/kind/objects/modules/core/array.rb', line 16

def self.Array?(*values)
  KIND.of?(::Array, values)
end

.Boolean?(*values) ⇒ Boolean

Returns:



16
17
18
# File 'lib/kind/objects/modules/custom/boolean.rb', line 16

def self.Boolean?(*values)
  KIND.of?(Boolean, values)
end

.Callable?(*values) ⇒ Boolean

Returns:



16
17
18
# File 'lib/kind/objects/modules/custom/callable.rb', line 16

def self.Callable?(*values)
  KIND.of?(Callable, values)
end

.Class?(*values) ⇒ Boolean

Returns:



10
11
12
# File 'lib/kind/objects/modules/core/class.rb', line 10

def self.Class?(*values)
  KIND.of?(::Class, values)
end

.Comparable?(*values) ⇒ Boolean

Returns:



10
11
12
# File 'lib/kind/objects/modules/core/comparable.rb', line 10

def self.Comparable?(*values)
  KIND.of?(::Comparable, values)
end

.Enumerable?(*values) ⇒ Boolean

Returns:



10
11
12
# File 'lib/kind/objects/modules/core/enumerable.rb', line 10

def self.Enumerable?(*values)
  KIND.of?(::Enumerable, values)
end

.Enumerator?(*values) ⇒ Boolean

Returns:



10
11
12
# File 'lib/kind/objects/modules/core/enumerator.rb', line 10

def self.Enumerator?(*values)
  KIND.of?(::Enumerator, values)
end

.File?(*values) ⇒ Boolean

Returns:



10
11
12
# File 'lib/kind/objects/modules/core/file.rb', line 10

def self.File?(*values)
  KIND.of?(::File, values)
end

.Float?(*values) ⇒ Boolean

Returns:



10
11
12
# File 'lib/kind/objects/modules/core/float.rb', line 10

def self.Float?(*values)
  KIND.of?(::Float, values)
end

.Hash?(*values) ⇒ Boolean

Returns:



16
17
18
# File 'lib/kind/objects/modules/core/hash.rb', line 16

def self.Hash?(*values)
  KIND.of?(::Hash, values)
end

.Integer?(*values) ⇒ Boolean

Returns:



10
11
12
# File 'lib/kind/objects/modules/core/integer.rb', line 10

def self.Integer?(*values)
  KIND.of?(::Integer, values)
end

.IO?(*values) ⇒ Boolean

Returns:



10
11
12
# File 'lib/kind/objects/modules/core/io.rb', line 10

def self.IO?(*values)
  KIND.of?(::IO, values)
end

.Lambda?(*values) ⇒ Boolean

Returns:



16
17
18
# File 'lib/kind/objects/modules/custom/lambda.rb', line 16

def self.Lambda?(*values)
  KIND.of?(Lambda, values)
end

.Maybe(kind) ⇒ Object



56
57
58
59
60
61
# File 'lib/kind/maybe.rb', line 56

def self.Maybe(kind)
  warn '[DEPRECATION] Kind::Maybe() is deprecated; use Kind::Maybe::Typed[] instead. ' \
      'It will be removed on next major release.'

  Maybe::Typed[kind]
end

.Method?(*values) ⇒ Boolean

Returns:



10
11
12
# File 'lib/kind/objects/modules/core/method.rb', line 10

def self.Method?(*values)
  KIND.of?(::Method, values)
end

.Module?(*values) ⇒ Boolean

Returns:



14
15
16
# File 'lib/kind/objects/modules/core/module.rb', line 14

def self.Module?(*values)
  KIND.of?(::Kind::Module, values)
end

.NoneObject



48
49
50
# File 'lib/kind/maybe.rb', line 48

def self.None
  Maybe::NONE_INSTANCE
end

.Numeric?(*values) ⇒ Boolean

Returns:



10
11
12
# File 'lib/kind/objects/modules/core/numeric.rb', line 10

def self.Numeric?(*values)
  KIND.of?(::Numeric, values)
end

.object(name:, &block) ⇒ Object

Kind.object(name:,&block)



50
51
52
# File 'lib/kind/objects/object.rb', line 50

def self.object(name:, &block)
  self[block, name: name]
end

.Of(*args) ⇒ Object

Kind::Of()



55
56
57
58
59
60
# File 'lib/kind/objects/object.rb', line 55

def self.Of(*args)
  warn '[DEPRECATION] Kind::Of() is deprecated; use Kind[] instead. ' \
      'It will be removed on next major release.'

  self[*args]
end

.OpenStruct?(*values) ⇒ Boolean

Returns:



12
13
14
# File 'lib/kind/objects/modules/stdlib/open_struct.rb', line 12

def self.OpenStruct?(*values)
  KIND.of?(::OpenStruct, values)
end

.Optional(kind) ⇒ Object



63
64
65
66
67
68
# File 'lib/kind/maybe.rb', line 63

def self.Optional(kind)
  warn '[DEPRECATION] Kind::Optional() is deprecated; use Kind::Optional::Typed[] instead. ' \
      'It will be removed on next major release.'

  Optional::Typed[kind]
end

.Proc?(*values) ⇒ Boolean

Returns:



10
11
12
# File 'lib/kind/objects/modules/core/proc.rb', line 10

def self.Proc?(*values)
  KIND.of?(::Proc, values)
end

.Queue?(*values) ⇒ Boolean

Returns:



11
12
13
# File 'lib/kind/objects/modules/core/queue.rb', line 11

def self.Queue?(*values)
  KIND.of?(::Queue, values)
end

.Range?(*values) ⇒ Boolean

Returns:



10
11
12
# File 'lib/kind/objects/modules/core/range.rb', line 10

def self.Range?(*values)
  KIND.of?(::Range, values)
end

.Regexp?(*values) ⇒ Boolean

Returns:



10
11
12
# File 'lib/kind/objects/modules/core/regexp.rb', line 10

def self.Regexp?(*values)
  KIND.of?(::Regexp, values)
end

.Set?(*values) ⇒ Boolean

Returns:



16
17
18
# File 'lib/kind/objects/modules/stdlib/set.rb', line 16

def self.Set?(*values)
  KIND.of?(::Set, values)
end

.Some(value) ⇒ Object



52
53
54
# File 'lib/kind/maybe.rb', line 52

def self.Some(value)
  Maybe::Some[value]
end

.String?(*values) ⇒ Boolean

Returns:



16
17
18
# File 'lib/kind/objects/modules/core/string.rb', line 16

def self.String?(*values)
  KIND.of?(::String, values)
end

.Struct?(*values) ⇒ Boolean

Returns:



10
11
12
# File 'lib/kind/objects/modules/core/struct.rb', line 10

def self.Struct?(*values)
  KIND.of?(::Struct, values)
end

.Symbol?(*values) ⇒ Boolean

Returns:



10
11
12
# File 'lib/kind/objects/modules/core/symbol.rb', line 10

def self.Symbol?(*values)
  KIND.of?(::Symbol, values)
end

.Time?(*values) ⇒ Boolean

Returns:



10
11
12
# File 'lib/kind/objects/modules/core/time.rb', line 10

def self.Time?(*values)
  KIND.of?(::Time, values)
end

Instance Method Details

#assert_hash!(hash, **kargs) ⇒ Object



92
93
94
# File 'lib/kind/basic.rb', line 92

def assert_hash!(hash, **kargs)
  STRICT.assert_hash!(hash, kargs)
end

#in!(list, value) ⇒ Object



84
85
86
# File 'lib/kind/basic.rb', line 84

def in!(list, value)
  STRICT.in!(list, value)
end

#include!(value, list) ⇒ Object



88
89
90
# File 'lib/kind/basic.rb', line 88

def include!(value, list)
  STRICT.in!(list, value)
end

#is(*args) ⇒ Object



18
19
20
21
22
23
# File 'lib/kind/basic.rb', line 18

def is(*args)
  warn '[DEPRECATION] Kind.is will behave like Kind.is! in the next major release; ' \
      'use Kind.is? instead.'

  is?(*args)
end

#is!(kind, arg, label: nil) ⇒ Object

Raises:



25
26
27
28
29
30
31
# File 'lib/kind/basic.rb', line 25

def is!(kind, arg, label: nil)
  return arg if KIND.is?(kind, arg)

  label_text = label ? "#{label}: " : ''

  raise Kind::Error.new("#{label_text}#{arg} expected to be a #{kind}")
end

#is?(kind, arg) ⇒ Boolean

Returns:



14
15
16
# File 'lib/kind/basic.rb', line 14

def is?(kind, arg)
  KIND.is?(kind, arg)
end

#of(kind, value, label: nil, expected: nil) ⇒ Object Also known as: of!



51
52
53
# File 'lib/kind/basic.rb', line 51

def of(kind, value, label: nil, expected: nil)
  STRICT.object_is_a(kind, value, label, expected)
end

#of?(kind, *args) ⇒ Boolean

Returns:



33
34
35
# File 'lib/kind/basic.rb', line 33

def of?(kind, *args)
  KIND.of?(kind, args)
end

#of_class(value) ⇒ Object



57
58
59
# File 'lib/kind/basic.rb', line 57

def of_class(value)
  STRICT.class!(value)
end

#of_class?(value) ⇒ Boolean

Returns:



37
38
39
# File 'lib/kind/basic.rb', line 37

def of_class?(value)
  OF.class?(value)
end

#of_module(value) ⇒ Object



61
62
63
# File 'lib/kind/basic.rb', line 61

def of_module(value)
  STRICT.module!(value)
end

#of_module?(value) ⇒ Boolean

Returns:



41
42
43
# File 'lib/kind/basic.rb', line 41

def of_module?(value)
  OF.module?(value)
end

#of_module_or_class(value) ⇒ Object



65
66
67
# File 'lib/kind/basic.rb', line 65

def of_module_or_class(value)
  STRICT.module_or_class(value)
end

#or_nil(kind, value) ⇒ Object



80
81
82
# File 'lib/kind/basic.rb', line 80

def or_nil(kind, value)
  return value if kind === value
end

#respond_to(value, *method_names) ⇒ Object Also known as: respond_to!



69
70
71
72
73
# File 'lib/kind/basic.rb', line 69

def respond_to(value, *method_names)
  method_names.each { |method_name| KIND.respond_to!(method_name, value) }

  value
end

#respond_to?(value, *method_names) ⇒ Boolean

Returns:



45
46
47
48
49
# File 'lib/kind/basic.rb', line 45

def respond_to?(value, *method_names)
  return super(value) if method_names.empty?

  KIND.interface?(method_names, value)
end

#value(kind, value, default:) ⇒ Object



76
77
78
# File 'lib/kind/basic.rb', line 76

def value(kind, value, default:)
  KIND.value(kind, value, of(kind, default))
end