mayak 0.0.11 → 0.0.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mayak/failable_function.rb +87 -0
- data/lib/mayak/optional_function.rb +69 -0
- data/lib/mayak/validation_result.rb +115 -0
- data/lib/mayak.rb +3 -0
- data/mayak.gemspec +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6c4f7f03caf2fb0cacb67575dca900e755c596ba01629b77ef9a3019497fb54
|
4
|
+
data.tar.gz: 612a525bb6a22ad1a06710efa8f93dead79184d0d6b87966b4cb8fa709068f58
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66348f91eb555c9f2056dce09b3a4dfc17e0d626eace68604380c9047e0b592d2cef835291220e2be9e08428b6977d2662a2c2a57cfb04abbb9c32401010ab19
|
7
|
+
data.tar.gz: d45c494d75ecc2b17fa306972aab09520006330d255773e4c10a75eada180d19ef6ec2784a79cabdbb3287de87e825a35fff831b09b7ff80d25426a602593671
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# typed: strict
|
3
|
+
|
4
|
+
module Mayak
|
5
|
+
class FailableFunction
|
6
|
+
extend T::Sig
|
7
|
+
extend T::Helpers
|
8
|
+
extend T::Generic
|
9
|
+
|
10
|
+
Input = type_member
|
11
|
+
Output = type_member
|
12
|
+
|
13
|
+
sig { params(blk: T.proc.params(input: Input).returns(Mayak::Monads::Try[Output])).void }
|
14
|
+
def initialize(&blk)
|
15
|
+
@blk = T.let(blk, T.proc.params(input: Input).returns(Mayak::Monads::Try[Output]))
|
16
|
+
end
|
17
|
+
|
18
|
+
sig {
|
19
|
+
type_parameters(
|
20
|
+
:Input,
|
21
|
+
:Output
|
22
|
+
).params(
|
23
|
+
proc: T.proc.params(
|
24
|
+
arg0: T.type_parameter(:Input)
|
25
|
+
).returns(
|
26
|
+
::Mayak::Monads::Try[T.type_parameter(:Output)]
|
27
|
+
)
|
28
|
+
).returns(
|
29
|
+
::Mayak::FailableFunction[T.type_parameter(:Input), T.type_parameter(:Output)]
|
30
|
+
)
|
31
|
+
}
|
32
|
+
def self.from_proc(proc)
|
33
|
+
::Mayak::FailableFunction[T.type_parameter(:Input), T.type_parameter(:Output)].new { |input| proc.call(input) }
|
34
|
+
end
|
35
|
+
|
36
|
+
sig { params(input: Input).returns(Mayak::Monads::Try[Output]) }
|
37
|
+
def call(input)
|
38
|
+
@blk.call(input)
|
39
|
+
end
|
40
|
+
|
41
|
+
sig { returns(T.proc.params(arg0: Input).returns(Mayak::Monads::Try[Output])) }
|
42
|
+
def to_proc
|
43
|
+
-> (input) { call(input) }
|
44
|
+
end
|
45
|
+
|
46
|
+
sig { returns(Mayak::Function[Input, Mayak::Monads::Try[Output]]) }
|
47
|
+
def to_function
|
48
|
+
Mayak::Function[Input, Mayak::Monads::Try[Output]].new { |input| call(input) }
|
49
|
+
end
|
50
|
+
|
51
|
+
sig {
|
52
|
+
type_parameters(:Output2)
|
53
|
+
.params(another: Mayak::FailableFunction[Output, T.type_parameter(:Output2)])
|
54
|
+
.returns(Mayak::FailableFunction[Input, T.type_parameter(:Output2)])
|
55
|
+
}
|
56
|
+
def and_then(another)
|
57
|
+
Mayak::FailableFunction[Input, T.type_parameter(:Output2)].new do |input|
|
58
|
+
@blk.call(input).flat_map { |output| another.call(output) }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
alias >> and_then
|
62
|
+
|
63
|
+
sig {
|
64
|
+
type_parameters(:Input2)
|
65
|
+
.params(another: Mayak::FailableFunction[T.type_parameter(:Input2), Input])
|
66
|
+
.returns(Mayak::FailableFunction[T.type_parameter(:Input2), Output])
|
67
|
+
}
|
68
|
+
def compose(another)
|
69
|
+
Mayak::FailableFunction[T.type_parameter(:Input2), Output].new { |input| another.call(input).flat_map { |new_input| @blk.call(new_input) } }
|
70
|
+
end
|
71
|
+
alias << compose
|
72
|
+
|
73
|
+
sig {
|
74
|
+
type_parameters(
|
75
|
+
:Input,
|
76
|
+
:Output
|
77
|
+
).params(
|
78
|
+
function: Mayak::Function[T.type_parameter(:Input), Mayak::Monads::Try[T.type_parameter(:Output)]]
|
79
|
+
).returns(
|
80
|
+
Mayak::FailableFunction[T.type_parameter(:Input), T.type_parameter(:Output)]
|
81
|
+
)
|
82
|
+
}
|
83
|
+
def self.from_function(function)
|
84
|
+
Mayak::FailableFunction[T.type_parameter(:Input), T.type_parameter(:Output)].new { |input| function.call(input) }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# typed: strict
|
3
|
+
|
4
|
+
module Mayak
|
5
|
+
class OptionalFunction
|
6
|
+
extend T::Sig
|
7
|
+
extend T::Helpers
|
8
|
+
extend T::Generic
|
9
|
+
|
10
|
+
Input = type_member
|
11
|
+
Output = type_member
|
12
|
+
|
13
|
+
sig { params(blk: T.proc.params(input: Input).returns(Mayak::Monads::Maybe[Output])).void }
|
14
|
+
def initialize(&blk)
|
15
|
+
@blk = T.let(blk, T.proc.params(input: Input).returns(Mayak::Monads::Maybe[Output]))
|
16
|
+
end
|
17
|
+
|
18
|
+
sig { params(input: Input).returns(Mayak::Monads::Maybe[Output]) }
|
19
|
+
def call(input)
|
20
|
+
@blk.call(input)
|
21
|
+
end
|
22
|
+
|
23
|
+
sig { returns(T.proc.params(arg0: Input).returns(Mayak::Monads::Maybe[Output])) }
|
24
|
+
def to_proc
|
25
|
+
-> (input) { call(input) }
|
26
|
+
end
|
27
|
+
|
28
|
+
sig { returns(Mayak::Function[Input, Mayak::Monads::Maybe[Output]]) }
|
29
|
+
def to_function
|
30
|
+
Mayak::Function[Input, Mayak::Monads::Maybe[Output]].new { |input| call(input) }
|
31
|
+
end
|
32
|
+
|
33
|
+
sig {
|
34
|
+
type_parameters(:Output2)
|
35
|
+
.params(another: Mayak::OptionalFunction[Output, T.type_parameter(:Output2)])
|
36
|
+
.returns(Mayak::OptionalFunction[Input, T.type_parameter(:Output2)])
|
37
|
+
}
|
38
|
+
def and_then(another)
|
39
|
+
Mayak::OptionalFunction[Input, T.type_parameter(:Output2)].new do |input|
|
40
|
+
@blk.call(input).flat_map { |output| another.call(output) }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
alias >> and_then
|
44
|
+
|
45
|
+
sig {
|
46
|
+
type_parameters(:Input2)
|
47
|
+
.params(another: Mayak::OptionalFunction[T.type_parameter(:Input2), Input])
|
48
|
+
.returns(Mayak::OptionalFunction[T.type_parameter(:Input2), Output])
|
49
|
+
}
|
50
|
+
def compose(another)
|
51
|
+
Mayak::OptionalFunction[T.type_parameter(:Input2), Output].new { |input| another.call(input).flat_map { |new_input| @blk.call(new_input) } }
|
52
|
+
end
|
53
|
+
alias << compose
|
54
|
+
|
55
|
+
sig {
|
56
|
+
type_parameters(
|
57
|
+
:Input,
|
58
|
+
:Output
|
59
|
+
).params(
|
60
|
+
function: Mayak::Function[T.type_parameter(:Input), Mayak::Monads::Maybe[T.type_parameter(:Output)]]
|
61
|
+
).returns(
|
62
|
+
Mayak::OptionalFunction[T.type_parameter(:Input), T.type_parameter(:Output)]
|
63
|
+
)
|
64
|
+
}
|
65
|
+
def self.from_function(function)
|
66
|
+
Mayak::OptionalFunction[T.type_parameter(:Input), T.type_parameter(:Output)].new { |input| function.call(input) }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# typed: strict
|
3
|
+
|
4
|
+
module Mayak
|
5
|
+
module ValidationResult
|
6
|
+
extend T::Sig
|
7
|
+
extend T::Generic
|
8
|
+
|
9
|
+
include Kernel
|
10
|
+
|
11
|
+
sealed!
|
12
|
+
abstract!
|
13
|
+
|
14
|
+
Error = type_member(:out)
|
15
|
+
|
16
|
+
sig {
|
17
|
+
abstract.type_parameters(
|
18
|
+
:NewError
|
19
|
+
).params(
|
20
|
+
blk: T.proc.params(arg: Error).returns(T.type_parameter(:NewError))
|
21
|
+
).returns(
|
22
|
+
::Mayak::ValidationResult[T.any(Error, T.type_parameter(:NewError))]
|
23
|
+
)
|
24
|
+
}
|
25
|
+
def map_errors(&blk)
|
26
|
+
end
|
27
|
+
|
28
|
+
sig { returns(T::Boolean) }
|
29
|
+
def valid?
|
30
|
+
is_a?(::Mayak::ValidationResult::Valid)
|
31
|
+
end
|
32
|
+
|
33
|
+
sig { returns(T::Boolean) }
|
34
|
+
def invalid?
|
35
|
+
!valid?
|
36
|
+
end
|
37
|
+
|
38
|
+
sig {
|
39
|
+
type_parameters(:A)
|
40
|
+
.params(blk: T.proc.returns(T.type_parameter(:A)))
|
41
|
+
.returns(T.any(::Mayak::ValidationResult::Invalid[Error], T.type_parameter(:A)))
|
42
|
+
}
|
43
|
+
def on_valid(&blk)
|
44
|
+
if is_a?(::Mayak::ValidationResult::Valid)
|
45
|
+
blk.call
|
46
|
+
else
|
47
|
+
self
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
sig { params(exceptions: T::Array[StandardError]).returns(::Mayak::ValidationResult[StandardError]) }
|
52
|
+
def self.from_exceptions(exceptions)
|
53
|
+
if exceptions.empty?
|
54
|
+
::Mayak::ValidationResult::Valid[StandardError].new
|
55
|
+
else
|
56
|
+
::Mayak::ValidationResult::Invalid[StandardError].new(errors: exceptions)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
sig { params(strings: T::Array[String]).returns(::Mayak::ValidationResult[String]) }
|
61
|
+
def self.from_strings(strings)
|
62
|
+
if strings.empty?
|
63
|
+
::Mayak::ValidationResult::Valid[String].new
|
64
|
+
else
|
65
|
+
::Mayak::ValidationResult::Invalid[String].new(errors: strings)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class Valid
|
70
|
+
extend T::Sig
|
71
|
+
extend T::Generic
|
72
|
+
|
73
|
+
include ::Mayak::ValidationResult
|
74
|
+
|
75
|
+
Error = type_member
|
76
|
+
|
77
|
+
sig {
|
78
|
+
override.type_parameters(
|
79
|
+
:NewError
|
80
|
+
).params(
|
81
|
+
blk: T.proc.params(arg: Error).returns(T.type_parameter(:NewError))
|
82
|
+
).returns(
|
83
|
+
::Mayak::ValidationResult[T.any(Error, T.type_parameter(:NewError))]
|
84
|
+
)
|
85
|
+
}
|
86
|
+
def map_errors(&blk)
|
87
|
+
self
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
class Invalid < T::Struct
|
92
|
+
extend T::Sig
|
93
|
+
extend T::Generic
|
94
|
+
|
95
|
+
include ::Mayak::ValidationResult
|
96
|
+
|
97
|
+
Error = type_member
|
98
|
+
|
99
|
+
const :errors, T::Array[Error]
|
100
|
+
|
101
|
+
sig {
|
102
|
+
override.type_parameters(
|
103
|
+
:NewError
|
104
|
+
).params(
|
105
|
+
blk: T.proc.params(arg: Error).returns(T.type_parameter(:NewError))
|
106
|
+
).returns(
|
107
|
+
::Mayak::ValidationResult[T.any(Error, T.type_parameter(:NewError))]
|
108
|
+
)
|
109
|
+
}
|
110
|
+
def map_errors(&blk)
|
111
|
+
::Mayak::ValidationResult::Invalid[T.any(Error, T.type_parameter(:NewError))].new(errors: errors.map(&blk))
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
data/lib/mayak.rb
CHANGED
@@ -5,6 +5,9 @@ require 'sorbet-runtime'
|
|
5
5
|
|
6
6
|
require_relative 'mayak/cache'
|
7
7
|
require_relative 'mayak/function'
|
8
|
+
require_relative 'mayak/failable_function'
|
9
|
+
require_relative 'mayak/optional_function'
|
10
|
+
require_relative 'mayak/validation_result'
|
8
11
|
require_relative 'mayak/json'
|
9
12
|
require_relative 'mayak/numeric'
|
10
13
|
require_relative 'mayak/random'
|
data/mayak.gemspec
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "mayak"
|
7
|
-
spec.version = "0.0.
|
7
|
+
spec.version = "0.0.13"
|
8
8
|
spec.summary = "Set of fully typed utility classes and interfaces integrated with Sorbet."
|
9
9
|
spec.description = spec.summary
|
10
10
|
spec.authors = ["Daniil Bober"]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mayak
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniil Bober
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-05-
|
11
|
+
date: 2024-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sorbet-runtime
|
@@ -111,6 +111,7 @@ files:
|
|
111
111
|
- lib/mayak/collections/queue.rb
|
112
112
|
- lib/mayak/decoder.rb
|
113
113
|
- lib/mayak/encoder.rb
|
114
|
+
- lib/mayak/failable_function.rb
|
114
115
|
- lib/mayak/function.rb
|
115
116
|
- lib/mayak/hash_serializable.rb
|
116
117
|
- lib/mayak/http/README.md
|
@@ -128,8 +129,10 @@ files:
|
|
128
129
|
- lib/mayak/monads/result.rb
|
129
130
|
- lib/mayak/monads/try.rb
|
130
131
|
- lib/mayak/numeric.rb
|
132
|
+
- lib/mayak/optional_function.rb
|
131
133
|
- lib/mayak/predicates/rule.rb
|
132
134
|
- lib/mayak/random.rb
|
135
|
+
- lib/mayak/validation_result.rb
|
133
136
|
- lib/mayak/version.rb
|
134
137
|
- lib/mayak/weak_ref.rb
|
135
138
|
- mayak.gemspec
|