mayak 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a36466c4a142e690196d7f63f0adbca6287b6ba1772309948cce797f3dfdf24d
4
- data.tar.gz: 176703f0c3c2d1d95f4653bfc6aa65a10eb3c876d823d95882331699166a6de9
3
+ metadata.gz: 5e0f62a1884888e93ab1375ff4a8f240b53a7886228ff9bbd6750ccb7cc68705
4
+ data.tar.gz: cd02858a1b245c5060f94923b1cf8bc4f490dc92768b388da369ce8ef38c4cc5
5
5
  SHA512:
6
- metadata.gz: ee26f758734c1dbc67390edf6ec6227f6bafc337ece3c54b8fd6b17a8b270382fa39527c096796ec053dc3dc8738fe73f7974a2609fbaf872ec0f3c5e8f9df9b
7
- data.tar.gz: 02e9e4b30fae37c91e9dfdaed064b081601c43b06321537d81f78fbfcc19a54c5be20f4ac91f78f421aacdfb846626345bde0de750f0e563be10b84f25b30be2
6
+ metadata.gz: c4dc1ffc9f62a1958f7f50849298cb3e38992bb727b7015ab480248ed314beef0ad5d5480483219c9ee4298d95e51ad7f644bbe6b99fb32abb65792d9f79477c
7
+ data.tar.gz: 65a3581b578d4192e21dda936d6b431b5d29d6b31941c98ed626223203a3d5781d3d497c2ab322c9b6653ad04120f9209d9e6503240e050740b2239a968988fe
@@ -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,7 @@ require 'sorbet-runtime'
5
5
 
6
6
  require_relative 'mayak/cache'
7
7
  require_relative 'mayak/function'
8
+ require_relative 'mayak/failable_function'
8
9
  require_relative 'mayak/json'
9
10
  require_relative 'mayak/numeric'
10
11
  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.11"
7
+ spec.version = "0.0.12"
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.11
4
+ version: 0.0.12
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-18 00:00:00.000000000 Z
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