kind 4.1.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +0 -2
- data/CHANGELOG.md +33 -5
- data/README.md +8 -2
- data/lib/kind.rb +14 -38
- data/lib/kind/core.rb +0 -2
- data/lib/kind/empty.rb +0 -2
- data/lib/kind/empty/constant.rb +7 -0
- data/lib/kind/maybe/wrappable.rb +1 -1
- data/lib/kind/version.rb +1 -1
- metadata +3 -11
- data/lib/kind/core/deprecation.rb +0 -29
- data/lib/kind/core/wrong_number_of_args.rb +0 -9
- data/lib/kind/deprecations/built_in_type_checkers.rb +0 -23
- data/lib/kind/deprecations/checker.rb +0 -16
- data/lib/kind/deprecations/checker/factory.rb +0 -31
- data/lib/kind/deprecations/checker/protocol.rb +0 -73
- data/lib/kind/deprecations/is.rb +0 -35
- data/lib/kind/deprecations/of.rb +0 -258
- data/lib/kind/deprecations/types.rb +0 -121
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a58bdad80758ce4b68dd663984a2b505dc5bcc346861c8aa0da7826e458d71b
|
4
|
+
data.tar.gz: 4de72ab2b437c0743e28440ddf7a637a03ee290d03d484157202f2d967b4863c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ab61cb4339b6e979e505cf623b391ce9b946b853893b7e32ea734b6ff325ca007b8509b4c66669a90cfbdade64830843ed7ac0a2aeb30eb4324dca11d056c79
|
7
|
+
data.tar.gz: 04cc0c6f36ab19167de1b13cd484ccd00b576c13f1703ec42017c334e5c270b02e9d77d20f14346b74d2564837cf97024cc3bafc0dd7b7675c4ac8c0b42c5398
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -3,6 +3,9 @@
|
|
3
3
|
This project follows [semver 2.0.0](http://semver.org/spec/v2.0.0.html) and the recommendations of [keepachangelog.com](http://keepachangelog.com/).
|
4
4
|
|
5
5
|
- [Unreleased](#unreleased)
|
6
|
+
- [5.0.0 (2021-02-22)](#500-2021-02-22)
|
7
|
+
- [Breaking Changes](#breaking-changes)
|
8
|
+
- [Removed](#removed)
|
6
9
|
- [4.1.0 (2021-02-22)](#410-2021-02-22)
|
7
10
|
- [Added](#added)
|
8
11
|
- [4.0.0 (2021-02-22)](#400-2021-02-22)
|
@@ -12,7 +15,7 @@ This project follows [semver 2.0.0](http://semver.org/spec/v2.0.0.html) and the
|
|
12
15
|
- [3.1.0 (2020-07-08)](#310-2020-07-08)
|
13
16
|
- [Added](#added-2)
|
14
17
|
- [3.0.0 (2020-06-25)](#300-2020-06-25)
|
15
|
-
- [Breaking Changes](#breaking-changes)
|
18
|
+
- [Breaking Changes](#breaking-changes-1)
|
16
19
|
- [Added](#added-3)
|
17
20
|
- [2.3.0 (2020-06-24)](#230-2020-06-24)
|
18
21
|
- [Added](#added-4)
|
@@ -20,11 +23,11 @@ This project follows [semver 2.0.0](http://semver.org/spec/v2.0.0.html) and the
|
|
20
23
|
- [Added](#added-5)
|
21
24
|
- [2.1.0 (2020-05-12)](#210-2020-05-12)
|
22
25
|
- [Added](#added-6)
|
23
|
-
- [Breaking Changes](#breaking-changes-
|
26
|
+
- [Breaking Changes](#breaking-changes-2)
|
24
27
|
- [2.0.0 (2020-05-07)](#200-2020-05-07)
|
25
28
|
- [Added](#added-7)
|
26
|
-
- [Breaking Changes](#breaking-changes-
|
27
|
-
- [Removed](#removed)
|
29
|
+
- [Breaking Changes](#breaking-changes-3)
|
30
|
+
- [Removed](#removed-1)
|
28
31
|
- [1.9.0 (2020-05-06)](#190-2020-05-06)
|
29
32
|
- [Added](#added-8)
|
30
33
|
- [1.8.0 (2020-05-03)](#180-2020-05-03)
|
@@ -55,7 +58,7 @@ This project follows [semver 2.0.0](http://semver.org/spec/v2.0.0.html) and the
|
|
55
58
|
- [Added](#added-19)
|
56
59
|
- [0.3.0 (2020-01-03)](#030-2020-01-03)
|
57
60
|
- [Added](#added-20)
|
58
|
-
- [Breaking Changes](#breaking-changes-
|
61
|
+
- [Breaking Changes](#breaking-changes-4)
|
59
62
|
- [0.2.0 (2020-01-02)](#020-2020-01-02)
|
60
63
|
- [Added](#added-21)
|
61
64
|
- [0.1.0 (2019-12-26)](#010-2019-12-26)
|
@@ -71,6 +74,31 @@ This project follows [semver 2.0.0](http://semver.org/spec/v2.0.0.html) and the
|
|
71
74
|
### Fixed
|
72
75
|
-->
|
73
76
|
|
77
|
+
5.0.0 (2021-02-22)
|
78
|
+
------------------
|
79
|
+
|
80
|
+
### Breaking Changes
|
81
|
+
|
82
|
+
* [#44](https://github.com/serradura/kind/pull/44) - Now, do you need to require `"kind/empty/constant"` to define the constant `Empty` as a `Kind::Empty` alias.
|
83
|
+
|
84
|
+
### Removed
|
85
|
+
|
86
|
+
* [#44](https://github.com/serradura/kind/pull/44) - Remove `Kind::Is.call`
|
87
|
+
|
88
|
+
* [#44](https://github.com/serradura/kind/pull/44) - Remove `Kind::Of.call`
|
89
|
+
|
90
|
+
* [#44](https://github.com/serradura/kind/pull/44) - Remove `Kind::Types.add`.
|
91
|
+
|
92
|
+
* [#44](https://github.com/serradura/kind/pull/44) - Remove `Kind::Of::<Type>` and `Kind::Is::<Type>` modules.
|
93
|
+
|
94
|
+
* [#44](https://github.com/serradura/kind/pull/44) - Remove `Kind::Checker`, `Kind::Checker::Protocol`, `Kind::Checker::Factory`.
|
95
|
+
|
96
|
+
* [#44](https://github.com/serradura/kind/pull/44) - Remove the invocation of `Kind.is` without arguments.
|
97
|
+
|
98
|
+
* [#44](https://github.com/serradura/kind/pull/44) - Remove the invocation of `Kind.of` without arguments.
|
99
|
+
|
100
|
+
* [#44](https://github.com/serradura/kind/pull/44) - Remove the invocation of `Kind.of` with a single argument (the kind).
|
101
|
+
|
74
102
|
4.1.0 (2021-02-22)
|
75
103
|
------------------
|
76
104
|
|
data/README.md
CHANGED
@@ -35,6 +35,7 @@ One of the goals of this project is to do simple type checking like `"some strin
|
|
35
35
|
Version | Documentation
|
36
36
|
---------- | -------------
|
37
37
|
unreleased | https://github.com/serradura/u-case/blob/main/README.md
|
38
|
+
5.0.0 | https://github.com/serradura/u-case/blob/v5.x/README.md
|
38
39
|
4.1.0 | https://github.com/serradura/u-case/blob/v4.x/README.md
|
39
40
|
3.1.0 | https://github.com/serradura/u-case/blob/v3.x/README.md
|
40
41
|
2.3.0 | https://github.com/serradura/u-case/blob/v2.x/README.md
|
@@ -95,6 +96,7 @@ unreleased | https://github.com/serradura/u-case/blob/main/README.md
|
|
95
96
|
- [Kind::Maybe#check](#kindmaybecheck)
|
96
97
|
- [Kind::Maybe#presence](#kindmaybepresence)
|
97
98
|
- [Kind::Empty](#kindempty)
|
99
|
+
- [Defining Empty as Kind::Empty an alias](#defining-empty-as-kindempty-an-alias)
|
98
100
|
- [Kind::Validator (ActiveModel::Validations)](#kindvalidator-activemodelvalidations)
|
99
101
|
- [Usage](#usage-1)
|
100
102
|
- [Object#===](#object)
|
@@ -1429,7 +1431,11 @@ def do_something(value, with_options: Kind::Empty::HASH)
|
|
1429
1431
|
end
|
1430
1432
|
```
|
1431
1433
|
|
1432
|
-
|
1434
|
+
### Defining Empty as Kind::Empty an alias
|
1435
|
+
|
1436
|
+
You can require `kind/empty/constant` to define `Empty` as a `Kind::Empty` alias. But, a `LoadError` will be raised if there is an already defined constant `Empty`.
|
1437
|
+
|
1438
|
+
So if you required this file, the previous example could be written like this:
|
1433
1439
|
|
1434
1440
|
```ruby
|
1435
1441
|
def do_something(value, with_options: Empty::HASH)
|
@@ -1437,7 +1443,7 @@ def do_something(value, with_options: Empty::HASH)
|
|
1437
1443
|
end
|
1438
1444
|
```
|
1439
1445
|
|
1440
|
-
Follows the list of constants
|
1446
|
+
Follows the list of constants if the alias was defined:
|
1441
1447
|
|
1442
1448
|
- `Empty::SET`
|
1443
1449
|
- `Empty::HASH`
|
data/lib/kind.rb
CHANGED
@@ -17,70 +17,46 @@ require 'kind/maybe'
|
|
17
17
|
require 'kind/type_checker'
|
18
18
|
require 'kind/type_checkers'
|
19
19
|
|
20
|
-
require 'kind/deprecations/checker'
|
21
|
-
require 'kind/deprecations/of'
|
22
|
-
require 'kind/deprecations/is'
|
23
|
-
require 'kind/deprecations/types'
|
24
|
-
require 'kind/deprecations/built_in_type_checkers'
|
25
|
-
|
26
20
|
module Kind
|
27
|
-
|
21
|
+
extend self
|
22
|
+
|
23
|
+
def is?(kind, arg)
|
28
24
|
KIND.is?(kind, arg)
|
29
25
|
end
|
30
26
|
|
31
|
-
|
27
|
+
alias is is?
|
28
|
+
|
29
|
+
def of?(kind, *args)
|
32
30
|
KIND.of?(kind, args)
|
33
31
|
end
|
34
32
|
|
35
|
-
def
|
33
|
+
def of_class?(value)
|
36
34
|
KIND.of_class?(value)
|
37
35
|
end
|
38
36
|
|
39
|
-
def
|
37
|
+
def of_module?(value)
|
40
38
|
KIND.of_module?(value)
|
41
39
|
end
|
42
40
|
|
43
|
-
def
|
41
|
+
def respond_to(value, *method_names)
|
44
42
|
method_names.each { |method_name| KIND.respond_to!(method_name, value) }
|
45
43
|
|
46
44
|
value
|
47
45
|
end
|
48
46
|
|
49
|
-
def
|
47
|
+
def of_module_or_class(value)
|
50
48
|
KIND.of_module_or_class!(value)
|
51
49
|
end
|
52
50
|
|
53
|
-
def
|
54
|
-
|
55
|
-
DEPRECATION.warn('`Kind.is` without args is deprecated. This behavior will be removed in %{version}')
|
56
|
-
|
57
|
-
return Is
|
58
|
-
end
|
59
|
-
|
60
|
-
return is?(expected, object) if UNDEFINED != object
|
61
|
-
|
62
|
-
WRONG_NUMBER_OF_ARGS.error!(given: 1, expected: 2)
|
63
|
-
end
|
64
|
-
|
65
|
-
def self.of(kind = UNDEFINED, object = UNDEFINED)
|
66
|
-
if UNDEFINED == kind && UNDEFINED == object
|
67
|
-
DEPRECATION.warn('`Kind.of` without args is deprecated. This behavior will be removed in %{version}')
|
68
|
-
|
69
|
-
Of
|
70
|
-
elsif UNDEFINED != object
|
71
|
-
KIND.of!(kind, object)
|
72
|
-
else
|
73
|
-
DEPRECATION.warn_method_replacement('Kind.of(<Kind>)', 'Kind::Of(<Kind>)')
|
74
|
-
|
75
|
-
Checker::Factory.create(kind)
|
76
|
-
end
|
51
|
+
def of(kind, object)
|
52
|
+
KIND.of!(kind, object)
|
77
53
|
end
|
78
54
|
|
79
|
-
def
|
55
|
+
def value(kind, value, default:)
|
80
56
|
KIND.value(kind, value, of(kind, default))
|
81
57
|
end
|
82
58
|
|
83
|
-
def
|
59
|
+
def Of(kind, opt = Empty::HASH)
|
84
60
|
TypeChecker::Object.new(kind, opt)
|
85
61
|
end
|
86
62
|
end
|
data/lib/kind/core.rb
CHANGED
data/lib/kind/empty.rb
CHANGED
data/lib/kind/maybe/wrappable.rb
CHANGED
data/lib/kind/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kind
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 5.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-02-
|
11
|
+
date: 2021-02-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A simple type system (at runtime) for Ruby - free of dependencies.
|
14
14
|
email:
|
@@ -34,19 +34,11 @@ files:
|
|
34
34
|
- lib/kind/active_model/kind_validator.rb
|
35
35
|
- lib/kind/active_model/validation.rb
|
36
36
|
- lib/kind/core.rb
|
37
|
-
- lib/kind/core/deprecation.rb
|
38
37
|
- lib/kind/core/kind.rb
|
39
38
|
- lib/kind/core/undefined.rb
|
40
|
-
- lib/kind/core/wrong_number_of_args.rb
|
41
|
-
- lib/kind/deprecations/built_in_type_checkers.rb
|
42
|
-
- lib/kind/deprecations/checker.rb
|
43
|
-
- lib/kind/deprecations/checker/factory.rb
|
44
|
-
- lib/kind/deprecations/checker/protocol.rb
|
45
|
-
- lib/kind/deprecations/is.rb
|
46
|
-
- lib/kind/deprecations/of.rb
|
47
|
-
- lib/kind/deprecations/types.rb
|
48
39
|
- lib/kind/dig.rb
|
49
40
|
- lib/kind/empty.rb
|
41
|
+
- lib/kind/empty/constant.rb
|
50
42
|
- lib/kind/error.rb
|
51
43
|
- lib/kind/maybe.rb
|
52
44
|
- lib/kind/maybe/none.rb
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Kind
|
4
|
-
module DEPRECATION # :nodoc: all
|
5
|
-
WARN_IS_DISABLED = String(ENV['DISABLE_KIND_DEPRECATION']).strip == 't'
|
6
|
-
|
7
|
-
module DevNull
|
8
|
-
def self.warn(_)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
OUTPUT = WARN_IS_DISABLED ? DevNull : ::Kernel
|
13
|
-
|
14
|
-
def self.warn(message)
|
15
|
-
OUTPUT.warn("[DEPRECATION] #{message}" % { version: 'version 5.0' })
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.warn_method_replacement(old_method, new_method)
|
19
|
-
self.warn "`#{old_method}` is deprecated, it will be removed in %{version}. " \
|
20
|
-
"Please use `#{new_method}` instead."
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.warn_removal(name)
|
24
|
-
self.warn "`#{name}` is deprecated, it will be removed in %{version}."
|
25
|
-
end
|
26
|
-
|
27
|
-
private_constant :DevNull, :OUTPUT
|
28
|
-
end
|
29
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Kind
|
4
|
-
# -- Classes
|
5
|
-
[
|
6
|
-
::String, ::Symbol, ::Numeric, ::Integer, ::Float, ::Regexp, ::Time,
|
7
|
-
::Array, ::Range, ::Hash, ::Struct, ::Enumerator, ::Set, ::OpenStruct,
|
8
|
-
::Method, ::Proc,
|
9
|
-
::IO, ::File
|
10
|
-
].each { |klass| Types.add(klass) }
|
11
|
-
|
12
|
-
Types.add(::Queue, name: 'Queue'.freeze)
|
13
|
-
|
14
|
-
# -- Modules
|
15
|
-
[
|
16
|
-
::Enumerable, ::Comparable
|
17
|
-
].each { |klass| Types.add(klass) }
|
18
|
-
|
19
|
-
# -- Kind::Of::Maybe
|
20
|
-
|
21
|
-
Types.add(Kind::Maybe::Result, name: 'Maybe'.freeze)
|
22
|
-
Types.add(Kind::Maybe::Result, name: 'Optional'.freeze)
|
23
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'kind/deprecations/checker/factory'
|
4
|
-
require 'kind/deprecations/checker/protocol'
|
5
|
-
|
6
|
-
module Kind
|
7
|
-
class Checker
|
8
|
-
include Protocol
|
9
|
-
|
10
|
-
attr_reader :__kind
|
11
|
-
|
12
|
-
def initialize(kind)
|
13
|
-
@__kind = kind
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'singleton'
|
4
|
-
|
5
|
-
module Kind
|
6
|
-
class Checker
|
7
|
-
class Factory
|
8
|
-
include Singleton
|
9
|
-
|
10
|
-
def self.create(kind)
|
11
|
-
instance.create(kind)
|
12
|
-
end
|
13
|
-
|
14
|
-
def initialize
|
15
|
-
@__checkers__ = {}
|
16
|
-
end
|
17
|
-
|
18
|
-
def create(kind)
|
19
|
-
@__checkers__[kind] ||= begin
|
20
|
-
kind_name = kind.name
|
21
|
-
|
22
|
-
if Kind::Of.const_defined?(kind_name, false)
|
23
|
-
Kind::Of.const_get(kind_name)
|
24
|
-
else
|
25
|
-
Kind::Checker.new(Kind.of_module_or_class(kind))
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,73 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Kind
|
4
|
-
class Checker
|
5
|
-
module Protocol
|
6
|
-
def class?(value)
|
7
|
-
KIND.is?(__kind, value)
|
8
|
-
end
|
9
|
-
|
10
|
-
def instance(value, options = Empty::HASH)
|
11
|
-
default = options[:or]
|
12
|
-
|
13
|
-
return KIND.of!(__kind, value) if ::Kind::KIND.null?(default)
|
14
|
-
|
15
|
-
Kind::Undefined != value && instance?(value) ? value : KIND.of!(__kind, default)
|
16
|
-
end
|
17
|
-
|
18
|
-
def [](value, options = options = Empty::HASH)
|
19
|
-
instance(value, options)
|
20
|
-
end
|
21
|
-
|
22
|
-
def to_proc
|
23
|
-
@to_proc ||=
|
24
|
-
-> checker { -> value { checker.instance(value) } }.call(self)
|
25
|
-
end
|
26
|
-
|
27
|
-
def __is_instance__(value)
|
28
|
-
value.kind_of?(__kind)
|
29
|
-
end
|
30
|
-
|
31
|
-
def is_instance_to_proc
|
32
|
-
@is_instance_to_proc ||=
|
33
|
-
-> checker { -> value { checker.__is_instance__(value) } }.call(self)
|
34
|
-
end
|
35
|
-
|
36
|
-
def instance?(*args)
|
37
|
-
return is_instance_to_proc if args.empty?
|
38
|
-
|
39
|
-
return args.all? { |object| __is_instance__(object) } if args.size > 1
|
40
|
-
|
41
|
-
arg = args[0]
|
42
|
-
Kind::Undefined == arg ? is_instance_to_proc : __is_instance__(arg)
|
43
|
-
end
|
44
|
-
|
45
|
-
def or_nil(value)
|
46
|
-
return value if instance?(value)
|
47
|
-
end
|
48
|
-
|
49
|
-
def or_undefined(value)
|
50
|
-
or_nil(value) || Kind::Undefined
|
51
|
-
end
|
52
|
-
|
53
|
-
def __as_maybe__(value)
|
54
|
-
Kind::Maybe.new(or_nil(value))
|
55
|
-
end
|
56
|
-
|
57
|
-
def as_maybe_to_proc
|
58
|
-
@as_maybe_to_proc ||=
|
59
|
-
-> checker { -> value { checker.__as_maybe__(value) } }.call(self)
|
60
|
-
end
|
61
|
-
|
62
|
-
def as_maybe(value = Kind::Undefined)
|
63
|
-
return __as_maybe__(value) if Kind::Undefined != value
|
64
|
-
|
65
|
-
as_maybe_to_proc
|
66
|
-
end
|
67
|
-
|
68
|
-
def as_optional(value = Kind::Undefined)
|
69
|
-
as_maybe(value)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
data/lib/kind/deprecations/is.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Kind
|
4
|
-
module Is
|
5
|
-
def self.call(expected, object)
|
6
|
-
DEPRECATION.warn_method_replacement('Kind::Is.call', 'Kind::KIND.is?')
|
7
|
-
|
8
|
-
KIND.is?(expected, object)
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.Class(value)
|
12
|
-
DEPRECATION.warn_method_replacement('Kind::Is.Class', 'Kind.of_class?')
|
13
|
-
|
14
|
-
Kind.of_class?(value)
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.Module(value)
|
18
|
-
DEPRECATION.warn_method_replacement('Kind::Is.Module', 'Kind.of_module?')
|
19
|
-
|
20
|
-
Kind.of_module?(value)
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.Boolean(value)
|
24
|
-
DEPRECATION.warn_removal('Kind::Is.Boolean')
|
25
|
-
|
26
|
-
Kind::Class[value] <= TrueClass || value <= FalseClass
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.Callable(value)
|
30
|
-
DEPRECATION.warn_method_replacement('Kind::Is.Callable', 'Kind::Callable?')
|
31
|
-
|
32
|
-
value.respond_to?(:call)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
data/lib/kind/deprecations/of.rb
DELETED
@@ -1,258 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Kind
|
4
|
-
module Of
|
5
|
-
def self.call(kind, object, kind_name = nil)
|
6
|
-
DEPRECATION.warn_method_replacement('Kind::Of.call', 'Kind::KIND.of!')
|
7
|
-
|
8
|
-
KIND.of!(kind, object, kind_name)
|
9
|
-
end
|
10
|
-
|
11
|
-
# -- Class
|
12
|
-
|
13
|
-
def self.Class(object = Undefined)
|
14
|
-
DEPRECATION.warn_method_replacement('Kind::Of::Class', 'Kind::Class')
|
15
|
-
|
16
|
-
return Class if Undefined == object
|
17
|
-
|
18
|
-
self.call(::Class, object)
|
19
|
-
end
|
20
|
-
|
21
|
-
const_set(:Class, ::Module.new do
|
22
|
-
extend Checker::Protocol
|
23
|
-
|
24
|
-
def self.__kind; ::Class; end
|
25
|
-
|
26
|
-
def self.class?(value); Kind.of_class?(value); end
|
27
|
-
|
28
|
-
def self.__is_instance__(value); class?(value); end
|
29
|
-
end)
|
30
|
-
|
31
|
-
def self.Class?(*args)
|
32
|
-
DEPRECATION.warn_method_replacement('Kind::Of::Class?', 'Kind::Class?')
|
33
|
-
|
34
|
-
Kind::Class?(*args)
|
35
|
-
end
|
36
|
-
|
37
|
-
# -- Module
|
38
|
-
|
39
|
-
def self.Module(object = Undefined)
|
40
|
-
DEPRECATION.warn_method_replacement('Kind::Of::Module', 'Kind::Module')
|
41
|
-
|
42
|
-
return Module if Undefined == object
|
43
|
-
|
44
|
-
self.call(::Module, object)
|
45
|
-
end
|
46
|
-
|
47
|
-
const_set(:Module, ::Module.new do
|
48
|
-
extend Checker::Protocol
|
49
|
-
|
50
|
-
def self.__kind_undefined(value)
|
51
|
-
__kind_error(Kind::Undefined) if Kind::Undefined == value
|
52
|
-
|
53
|
-
yield
|
54
|
-
end
|
55
|
-
|
56
|
-
def self.__kind_error(value)
|
57
|
-
raise Kind::Error.new('Module'.freeze, value)
|
58
|
-
end
|
59
|
-
|
60
|
-
def self.__kind_of(value)
|
61
|
-
return value if Kind.of_module?(value)
|
62
|
-
|
63
|
-
__kind_error(value)
|
64
|
-
end
|
65
|
-
|
66
|
-
def self.__kind; ::Module; end
|
67
|
-
|
68
|
-
def self.class?(value); Kind.of_module?(value); end
|
69
|
-
|
70
|
-
def self.instance(value, options = Empty::HASH)
|
71
|
-
default = options[:or]
|
72
|
-
|
73
|
-
if ::Kind::KIND.null?(default)
|
74
|
-
__kind_undefined(value) { __kind_of(value) }
|
75
|
-
else
|
76
|
-
return value if Kind::Undefined != value && instance?(value)
|
77
|
-
|
78
|
-
__kind_undefined(default) { __kind_of(default) }
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
def self.__is_instance__(value); class?(value); end
|
83
|
-
end)
|
84
|
-
|
85
|
-
def self.Module?(*args)
|
86
|
-
DEPRECATION.warn_method_replacement('Kind::Of::Module?', 'Kind::Module?')
|
87
|
-
|
88
|
-
Kind::Module?(*args)
|
89
|
-
end
|
90
|
-
|
91
|
-
# -- Boolean
|
92
|
-
|
93
|
-
def self.Boolean(object = Undefined, options = Empty::HASH)
|
94
|
-
DEPRECATION.warn_method_replacement('Kind::Of::Boolean', 'Kind::Boolean')
|
95
|
-
|
96
|
-
default = options[:or]
|
97
|
-
|
98
|
-
return Kind::Of::Boolean if Undefined == object && default.nil?
|
99
|
-
|
100
|
-
bool = object.nil? ? default : object
|
101
|
-
|
102
|
-
return bool if bool.is_a?(::TrueClass) || bool.is_a?(::FalseClass)
|
103
|
-
|
104
|
-
raise Kind::Error.new('Boolean'.freeze, bool)
|
105
|
-
end
|
106
|
-
|
107
|
-
const_set(:Boolean, ::Module.new do
|
108
|
-
extend Checker::Protocol
|
109
|
-
|
110
|
-
def self.__kind; [TrueClass, FalseClass].freeze; end
|
111
|
-
|
112
|
-
def self.class?(value); Kind.is.Boolean(value); end
|
113
|
-
|
114
|
-
def self.instance(value, options= Empty::HASH)
|
115
|
-
default = options[:or]
|
116
|
-
|
117
|
-
if ::Kind::KIND.null?(default)
|
118
|
-
__kind_undefined(value) { Kind::Of::Boolean(value) }
|
119
|
-
else
|
120
|
-
return value if Kind::Undefined != value && instance?(value)
|
121
|
-
|
122
|
-
__kind_undefined(default) { Kind::Of::Boolean(default) }
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
def self.__kind_undefined(value)
|
127
|
-
if Kind::Undefined == value
|
128
|
-
raise Kind::Error.new('Boolean'.freeze, Kind::Undefined)
|
129
|
-
else
|
130
|
-
yield
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
def self.__is_instance__(value);
|
135
|
-
value.is_a?(TrueClass) || value.is_a?(FalseClass)
|
136
|
-
end
|
137
|
-
|
138
|
-
def self.or_undefined(value)
|
139
|
-
result = or_nil(value)
|
140
|
-
result.nil? ? Kind::Undefined : result
|
141
|
-
end
|
142
|
-
end)
|
143
|
-
|
144
|
-
def self.Boolean?(*args)
|
145
|
-
DEPRECATION.warn_method_replacement('Kind::Of::Boolean?', 'Kind::Boolean?')
|
146
|
-
|
147
|
-
Kind::Boolean?(*args)
|
148
|
-
end
|
149
|
-
|
150
|
-
# -- Lambda
|
151
|
-
|
152
|
-
def self.Lambda(object = Undefined, options = Empty::HASH)
|
153
|
-
DEPRECATION.warn_method_replacement('Kind::Of::Lambda', 'Kind::Lambda')
|
154
|
-
|
155
|
-
default = options[:or]
|
156
|
-
|
157
|
-
return Kind::Of::Lambda if Undefined == object && default.nil?
|
158
|
-
|
159
|
-
func = object || default
|
160
|
-
|
161
|
-
return func if func.is_a?(::Proc) && func.lambda?
|
162
|
-
|
163
|
-
raise Kind::Error.new('Lambda'.freeze, func)
|
164
|
-
end
|
165
|
-
|
166
|
-
const_set(:Lambda, ::Module.new do
|
167
|
-
extend Checker::Protocol
|
168
|
-
|
169
|
-
def self.__kind; ::Proc; end
|
170
|
-
|
171
|
-
def self.instance(value, options = Empty::HASH)
|
172
|
-
default = options[:or]
|
173
|
-
|
174
|
-
if ::Kind::KIND.null?(default)
|
175
|
-
__kind_undefined(value) { Kind::Of::Lambda(value) }
|
176
|
-
else
|
177
|
-
return value if Kind::Undefined != value && instance?(value)
|
178
|
-
|
179
|
-
__kind_undefined(default) { Kind::Of::Lambda(default) }
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
183
|
-
def self.__kind_undefined(value)
|
184
|
-
if Kind::Undefined == value
|
185
|
-
raise Kind::Error.new('Lambda'.freeze, Kind::Undefined)
|
186
|
-
else
|
187
|
-
yield
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
def self.__is_instance__(value)
|
192
|
-
value.is_a?(__kind) && value.lambda?
|
193
|
-
end
|
194
|
-
end)
|
195
|
-
|
196
|
-
def self.Lambda?(*args)
|
197
|
-
DEPRECATION.warn_method_replacement('Kind::Of::Lambda?', 'Kind::Lambda?')
|
198
|
-
|
199
|
-
Kind::Lambda?(*args)
|
200
|
-
end
|
201
|
-
|
202
|
-
# -- Callable
|
203
|
-
|
204
|
-
def self.Callable(object = Undefined, options = Empty::HASH)
|
205
|
-
DEPRECATION.warn_method_replacement('Kind::Of::Callable', 'Kind::Callable')
|
206
|
-
|
207
|
-
default = options[:or]
|
208
|
-
|
209
|
-
return Kind::Of::Callable if Undefined == object && default.nil?
|
210
|
-
|
211
|
-
callable = object || default
|
212
|
-
|
213
|
-
return callable if callable.respond_to?(:call)
|
214
|
-
|
215
|
-
raise Kind::Error.new('Callable'.freeze, callable)
|
216
|
-
end
|
217
|
-
|
218
|
-
const_set(:Callable, ::Module.new do
|
219
|
-
extend Checker::Protocol
|
220
|
-
|
221
|
-
def self.__kind; Object; end
|
222
|
-
|
223
|
-
def self.class?(value)
|
224
|
-
Kind::Is::Callable(value)
|
225
|
-
end
|
226
|
-
|
227
|
-
def self.instance(value, options = Empty::HASH)
|
228
|
-
default = options[:or]
|
229
|
-
|
230
|
-
if ::Kind::KIND.null?(default)
|
231
|
-
__kind_undefined(value) { Kind::Of::Callable(value) }
|
232
|
-
else
|
233
|
-
return value if Kind::Undefined != value && instance?(value)
|
234
|
-
|
235
|
-
__kind_undefined(default) { Kind::Of::Callable(default) }
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
|
-
def self.__kind_undefined(value)
|
240
|
-
if Kind::Undefined == value
|
241
|
-
raise Kind::Error.new('Callable'.freeze, Kind::Undefined)
|
242
|
-
else
|
243
|
-
yield
|
244
|
-
end
|
245
|
-
end
|
246
|
-
|
247
|
-
def self.__is_instance__(value);
|
248
|
-
value.respond_to?(:call)
|
249
|
-
end
|
250
|
-
end)
|
251
|
-
|
252
|
-
def self.Callable?(*args)
|
253
|
-
DEPRECATION.warn_method_replacement('Kind::Of::Callable?', 'Kind::Callable?')
|
254
|
-
|
255
|
-
Kind::Callable?(*args)
|
256
|
-
end
|
257
|
-
end
|
258
|
-
end
|
@@ -1,121 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Kind
|
4
|
-
module Types
|
5
|
-
extend self
|
6
|
-
|
7
|
-
COLONS = '::'.freeze
|
8
|
-
|
9
|
-
KIND_OF = <<-RUBY
|
10
|
-
def self.%{method_name}(object = Undefined, options = Empty::HASH)
|
11
|
-
DEPRECATION.warn_method_replacement('Kind::Of::%{method_name}', 'Kind::%{method_name}')
|
12
|
-
|
13
|
-
default = options[:or]
|
14
|
-
|
15
|
-
return Kind::Of::%{kind_name} if Undefined == object && default.nil?
|
16
|
-
|
17
|
-
is_instance = Kind::Of::%{kind_name}.__is_instance__(object)
|
18
|
-
|
19
|
-
return object if is_instance
|
20
|
-
|
21
|
-
KIND.of!(::%{kind_name_to_check}, object && default ? default : object || default)
|
22
|
-
end
|
23
|
-
RUBY
|
24
|
-
|
25
|
-
KIND_OF_IS = <<-RUBY
|
26
|
-
def self.%{method_name}?(*args)
|
27
|
-
DEPRECATION.warn_method_replacement('Kind::Of::%{method_name}?', 'Kind::%{method_name}?')
|
28
|
-
|
29
|
-
Kind::Of::%{kind_name}.instance?(*args)
|
30
|
-
end
|
31
|
-
RUBY
|
32
|
-
|
33
|
-
KIND_IS = <<-RUBY
|
34
|
-
def self.%{method_name}(value)
|
35
|
-
DEPRECATION.warn_removal('Kind::Is::%{method_name}')
|
36
|
-
|
37
|
-
KIND.is!(::%{kind_name_to_check}, value)
|
38
|
-
end
|
39
|
-
RUBY
|
40
|
-
|
41
|
-
private_constant :KIND_OF, :KIND_IS, :COLONS
|
42
|
-
|
43
|
-
def add(kind, name: nil)
|
44
|
-
DEPRECATION.warn_removal('Kind::Types')
|
45
|
-
|
46
|
-
kind_name = KIND.of_module_or_class!(kind).name
|
47
|
-
checker = name ? Kind::String[name] : kind_name
|
48
|
-
|
49
|
-
if checker.include?(COLONS)
|
50
|
-
add_kind_with_namespace(checker, method_name: name)
|
51
|
-
else
|
52
|
-
add_root(checker, kind_name, method_name: name,
|
53
|
-
kind_name_to_check: kind_name,
|
54
|
-
create_kind_is_mod: false)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
private
|
59
|
-
|
60
|
-
def add_root(checker, kind_name, method_name:, create_kind_is_mod:, kind_name_to_check: nil)
|
61
|
-
root_kind_name = method_name ? method_name : kind_name
|
62
|
-
|
63
|
-
params = {
|
64
|
-
method_name: method_name ? method_name : checker,
|
65
|
-
kind_name: method_name ? method_name : kind_name,
|
66
|
-
kind_name_to_check: kind_name_to_check || root_kind_name
|
67
|
-
}
|
68
|
-
|
69
|
-
add_kind(params, Kind::Of, Kind::Is, create_kind_is_mod)
|
70
|
-
end
|
71
|
-
|
72
|
-
def add_kind_with_namespace(checker, method_name:)
|
73
|
-
raise NotImplementedError if method_name
|
74
|
-
|
75
|
-
const_names = checker.split(COLONS)
|
76
|
-
const_names.each_with_index do |const_name, index|
|
77
|
-
if index == 0
|
78
|
-
add_root(const_name, const_name, method_name: nil, create_kind_is_mod: true)
|
79
|
-
else
|
80
|
-
add_node(const_names, index)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
def add_node(const_names, index)
|
86
|
-
namespace = const_names[0..(index-1)]
|
87
|
-
namespace_name = namespace.join(COLONS)
|
88
|
-
|
89
|
-
kind_of_mod = Kind::Of.const_get(namespace_name)
|
90
|
-
kind_is_mod = Kind::Is.const_get(namespace_name)
|
91
|
-
|
92
|
-
checker = const_names[index]
|
93
|
-
kind_name = const_names[0..index].join(COLONS)
|
94
|
-
|
95
|
-
params = { method_name: checker, kind_name: kind_name }
|
96
|
-
|
97
|
-
add_kind(params, kind_of_mod, kind_is_mod, true)
|
98
|
-
end
|
99
|
-
|
100
|
-
def add_kind(params, kind_of_mod, kind_is_mod, create_kind_is_mod)
|
101
|
-
method_name = params[:method_name]
|
102
|
-
|
103
|
-
unless kind_of_mod.respond_to?(method_name)
|
104
|
-
kind_name = params[:kind_name]
|
105
|
-
params[:kind_name_to_check] ||= kind_name
|
106
|
-
|
107
|
-
kind_checker = ::Module.new { extend Checker::Protocol }
|
108
|
-
kind_checker.module_eval("def self.__kind; ::#{params[:kind_name_to_check]}; end")
|
109
|
-
|
110
|
-
kind_of_mod.instance_eval(KIND_OF % params)
|
111
|
-
kind_of_mod.const_set(method_name, kind_checker)
|
112
|
-
kind_of_mod.instance_eval(KIND_OF_IS % params)
|
113
|
-
end
|
114
|
-
|
115
|
-
unless kind_is_mod.respond_to?(method_name)
|
116
|
-
kind_is_mod.instance_eval(KIND_IS % params)
|
117
|
-
kind_is_mod.const_set(method_name, ::Module.new) if create_kind_is_mod
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|