uinit-sig 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7316f9bbb53f246138e12879366589e154a01a2a135c858fe84c90815cf1d14e
4
+ data.tar.gz: f35d84939f6bb218161c587aa12bdb7d84abe7d6a9b91c705909e8eafb64816a
5
+ SHA512:
6
+ metadata.gz: d2ad9d556fab68adad980b85c8692ee1a68bfcbcaa4dc4213b907774bce254e06e337cbb53599367e95ec73b80266f48138c9f3640bb17d9a77dc229c1b9c12b
7
+ data.tar.gz: 18eb82848d896f79bf755f4015bcaea66a5d0e2c5f5b8de33c7c09b10b24513ffbf7ed78af3aaaa0d6a9d4da783d44013ecc07b3b36a10c65b9c17933cb22096
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Kimoja
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,10 @@
1
+ # Uinit::Sig
2
+
3
+ Signed methods
4
+
5
+ WIP
6
+
7
+ ## TODO
8
+ - une class spéciale pour les Proc, comme SignedProc, prenant en param un liste de type (non nommée)
9
+ Une methode bind prenant une proc à liéer et retournant une SignedBindedProc ou du genre
10
+ Au call, check les type et call la proc bindée
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Layout/EmptyLinesAroundAttributeAccessor
4
+ module Uinit
5
+ module Sig
6
+ class Method < Uinit::Struct
7
+ include Memoizable
8
+
9
+ struct do
10
+ attr(:base, Class | Module)
11
+ attr(:class_method, bool)
12
+
13
+ attr(:parameters, [Parameter]).init(false)
14
+
15
+ attr(:return_type, none | Uinit::Type::Base) do
16
+ set { |type| Uinit::Type.from(type) unless type.nil? }
17
+ init(false)
18
+ end
19
+
20
+ attr(:signed_name, Symbol) do
21
+ default { (0...30).map { ('a'..'z').to_a[rand(26)] }.join.to_sym }
22
+ init(false)
23
+ end
24
+
25
+ attr(:method_name, Symbol).init(false)
26
+ attr(:checker, Proc).init(false)
27
+ end
28
+
29
+ def returns(return_type)
30
+ self.return_type = return_type
31
+
32
+ self
33
+ end
34
+
35
+ def for(method_name)
36
+ self.method_name = method_name
37
+ add_parameters_types
38
+ MethodCompiler.compile(method: self)
39
+ end
40
+
41
+ def return_is!(value)
42
+ return_type.is!(value)
43
+ rescue Type::Error => e
44
+ raise ReturnTypeError.new(self, e)
45
+ end
46
+
47
+ private
48
+
49
+ memo def method_origin
50
+ return base.instance_method(method_name) unless class_method
51
+
52
+ base.singleton_class.instance_method(method_name)
53
+ end
54
+
55
+ def add_parameters_types
56
+ method_origin.parameters.each_with_index do |(parameter_type, name), _i|
57
+ parameter = parameters.find { _1.name == name }
58
+ raise ArgumentError, "Unable to retrieve parameter '#{name}'" unless parameter
59
+
60
+ parameter.parameter_type = parameter_type
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ # rubocop:enable Layout/EmptyLinesAroundAttributeAccessor
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Uinit
4
+ module Sig
5
+ class MethodCompiler < Uinit::Struct
6
+ def self.compile(...)
7
+ new(...).compile
8
+ end
9
+
10
+ struct do
11
+ attr(:method, Method)
12
+ end
13
+
14
+ def compile
15
+ compile_arguments_check_method
16
+ compile_wrapped_method
17
+ end
18
+
19
+ def compile_arguments_check_method
20
+ compile_method(<<~RUBY, __FILE__, __LINE__ + 1)
21
+ def __check_#{method.method_name}(#{compile_parameters_sig})
22
+ __method__ = _signed_methods.#{method.signed_name}
23
+ #{compile_parameters_check}
24
+ end
25
+ RUBY
26
+ end
27
+
28
+ def compile_wrapped_method
29
+ compile_method(<<~RUBY, __FILE__, __LINE__ + 1)
30
+ def #{method.method_name}(...)
31
+ method = _signed_methods.#{method.signed_name}
32
+ __check_#{method.method_name}(...)
33
+ #{compile_original_method_call}
34
+ end
35
+ RUBY
36
+ end
37
+
38
+ private
39
+
40
+ def compile_method(str, file, line)
41
+ puts str.gsub(/^$\s*\n/, '').gsub(/\s+$/, '')
42
+ puts '----------------------------------------------------------------'
43
+
44
+ method.base.class_eval(str.gsub(/^$\s*\n/, '').gsub(/\s+$/, ''), file, line)
45
+ end
46
+
47
+ def compile_parameters_sig
48
+ method.parameters.map { compile_parameter_sig(_1).to_s.strip }.join(', ')
49
+ end
50
+
51
+ def compile_parameter_sig(parameter)
52
+ return parameter.name if parameter.req_arg?
53
+ return "#{parameter.name} = Uinit::Sig::UNDEFINED" if parameter.opt_arg?
54
+ return "*#{parameter.name}" if parameter.rest_arg?
55
+
56
+ return "#{parameter.name}:" if parameter.req_kwarg?
57
+ return "#{parameter.name}: Uinit::Sig::UNDEFINED" if parameter.opt_kwarg?
58
+
59
+ "**#{parameter.name}" if parameter.rest_kwarg?
60
+ end
61
+
62
+ def compile_parameters_check
63
+ parameters_check = method.parameters.map.with_index do |parameter, index|
64
+ compile_parameter_check(parameter, index).to_s.strip
65
+ end
66
+
67
+ parameters_check.join("\n")
68
+ end
69
+
70
+ def compile_parameter_check(parameter, index)
71
+ return unless parameter.typed?
72
+
73
+ if parameter.opt?
74
+ return <<~RUBY
75
+ unless #{parameter.name} == Uinit::Sig::UNDEFINED
76
+ __method__.parameters[#{index}].is!(#{parameter.name})
77
+ end
78
+ RUBY
79
+ end
80
+
81
+ <<~RUBY
82
+ __method__.parameters[#{index}].is!(#{parameter.name})
83
+ RUBY
84
+ end
85
+
86
+ def compile_original_method_call
87
+ unless method.return_type
88
+ return <<~RUBY
89
+ method.method_origin.bind_call(self, ...)
90
+ RUBY
91
+ end
92
+
93
+ <<~RUBY
94
+ ret = method.method_origin.bind_call(self, ...)
95
+ method.return_is!(ret)
96
+ ret
97
+ RUBY
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ # class A
104
+ # def jo(...)
105
+ # e = proc do |a, b = "ok"|
106
+ # pp a, b
107
+ # end
108
+ # e.call(...)
109
+ # end
110
+ # end
111
+
112
+ # def si(...)
113
+ # a = A.new
114
+ # A.instance_method(:jo).bind_call(a, ...)
115
+ # end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Uinit
4
+ module Sig
5
+ class Parameter < Uinit::Struct
6
+ struct do
7
+ attr(:name, Symbol)
8
+
9
+ attr(:type, none | Uinit::Type::Base).set { |type| Uinit::Type.from(type) unless type.nil? }
10
+ attr(:method, Method)
11
+
12
+ attr(:parameter_type, any_of(:req, :opt, :rest, :keyreq, :key, :keyrest)).init(false)
13
+ end
14
+
15
+ def req_arg?
16
+ parameter_type == :req
17
+ end
18
+
19
+ def opt_arg?
20
+ parameter_type == :opt
21
+ end
22
+
23
+ def rest_arg?
24
+ parameter_type == :rest
25
+ end
26
+
27
+ def req_kwarg?
28
+ parameter_type == :keyreq
29
+ end
30
+
31
+ def opt_kwarg?
32
+ parameter_type == :key
33
+ end
34
+
35
+ def rest_kwarg?
36
+ parameter_type == :keyrest
37
+ end
38
+
39
+ def opt?
40
+ opt_arg? || opt_kwarg?
41
+ end
42
+
43
+ def typed?
44
+ !type.nil?
45
+ end
46
+
47
+ def is!(value)
48
+ type.is!(value)
49
+ rescue Type::Error => e
50
+ raise ParameterTypeError.new(self, e)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Uinit
4
+ module Sig
5
+ class ParameterTypeError < StandardError
6
+ def initialize(parameter, type_error)
7
+ super()
8
+
9
+ @parameter = parameter
10
+ @type_error = type_error
11
+
12
+ set_backtrace(type_error.backtrace)
13
+ end
14
+
15
+ attr_reader :parameter, :type_error
16
+
17
+ def message
18
+ method = parameter.method
19
+ file, line = method.method_origin.source_location
20
+
21
+ "Type error on parameter '#{parameter.name}' " \
22
+ "in method '#{method.method_name}' at '#{file}:#{line}', detail:\n#{type_error.message}"
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Uinit
4
+ module Sig
5
+ class ReturnTypeError < StandardError
6
+ def initialize(method, type_error)
7
+ super()
8
+
9
+ @method = method
10
+ @type_error = type_error
11
+
12
+ set_backtrace(type_error.backtrace)
13
+ end
14
+
15
+ attr_reader :method, :type_error
16
+
17
+ def message
18
+ file, line = method.method_origin.source_location
19
+
20
+ "Return type error in method '#{method.method_name}' " \
21
+ "at '#{file}:#{line}', detail:\n#{type_error.message}"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Uinit
4
+ module Sig
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
data/lib/uinit/sig.rb ADDED
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zeitwerk'
4
+
5
+ require 'uinit/type'
6
+ require 'uinit/structure'
7
+
8
+ module Uinit; end
9
+
10
+ Zeitwerk::Loader.for_gem.tap do |loader|
11
+ loader.push_dir(__dir__, namespace: Uinit)
12
+ loader.setup
13
+ end
14
+
15
+ module Uinit
16
+ module Sig
17
+ include Memoizable
18
+
19
+ UNDEFINED = Class.new.new
20
+
21
+ def self.included(base)
22
+ base.extend(ClassMethods)
23
+ end
24
+
25
+ class SignedMethods; end
26
+
27
+ module ClassMethods
28
+ include Memoizable
29
+ include Type::Context
30
+
31
+ memo def signed_methods
32
+ if respond_to?(:superclass) && superclass.respond_to?(:signed_methods)
33
+ return Class.new(superclass.signed_methods.class).new
34
+ end
35
+
36
+ Class.new(SignedMethods).new
37
+ end
38
+
39
+ def sig(**params)
40
+ build_method(false, **params)
41
+ end
42
+
43
+ def sig_self(**params)
44
+ build_method(true, **params)
45
+ end
46
+
47
+ private
48
+
49
+ def build_method(class_method, **params)
50
+ method = Method.new(base: self, class_method:)
51
+
52
+ parameters = params.map { |name, type| Parameter.new(name:, type:, method:) }
53
+ method.parameters = parameters
54
+
55
+ signed_methods.class.define_method(method.signed_name) { method }
56
+
57
+ method
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ memo def _signed_methods = self.class.signed_methods
64
+ end
65
+ end
data/uinit-sig.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'uinit/sig/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'uinit-sig'
10
+ spec.version = Uinit::Sig::VERSION
11
+ spec.authors = ['Kimoja']
12
+ spec.email = ['joakim.carrilho@cheerz.com']
13
+
14
+ spec.summary = 'Signed methods'
15
+ spec.description = 'Signed methods'
16
+ spec.homepage = 'https://github.com/Kimoja/uinit-sig'
17
+ spec.license = 'MIT'
18
+ spec.required_ruby_version = '>= 3.2.1'
19
+ spec.files = Dir['CHANGELOG.md', 'LICENSE.txt', 'README.md', 'uinit-sig.gemspec', 'lib/**/*']
20
+ spec.require_paths = ['lib']
21
+ spec.executables = []
22
+
23
+ spec.metadata['homepage_uri'] = spec.homepage
24
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
25
+ spec.metadata['source_code_uri'] = 'https://github.com/Kimoja/uinit-sig'
26
+ spec.metadata['changelog_uri'] = 'https://github.com/Kimoja/uinit-sig/blob/main/CHANGELOG.md'
27
+ spec.metadata['bug_tracker_uri'] = 'https://github.com/Kimoja/uinit-sig/issues'
28
+
29
+ spec.add_runtime_dependency 'zeitwerk', '~> 2.6'
30
+
31
+ spec.add_dependency 'uinit-memoizable', '~> 0.1.0'
32
+ spec.add_dependency 'uinit-structure', '~> 0.1.0'
33
+ spec.add_dependency 'uinit-type', '~> 0.1.0'
34
+
35
+ spec.add_development_dependency 'bundler'
36
+ spec.add_development_dependency 'pry'
37
+ spec.add_development_dependency 'rspec'
38
+ spec.add_development_dependency 'rubocop'
39
+ spec.metadata['rubygems_mfa_required'] = 'true'
40
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uinit-sig
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kimoja
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-05-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: zeitwerk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: uinit-memoizable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: uinit-structure
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: uinit-type
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Signed methods
126
+ email:
127
+ - joakim.carrilho@cheerz.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - LICENSE.txt
133
+ - README.md
134
+ - lib/uinit/sig.rb
135
+ - lib/uinit/sig/method.rb
136
+ - lib/uinit/sig/method_compiler.rb
137
+ - lib/uinit/sig/parameter.rb
138
+ - lib/uinit/sig/parameter_type_error.rb
139
+ - lib/uinit/sig/return_type_error.rb
140
+ - lib/uinit/sig/version.rb
141
+ - uinit-sig.gemspec
142
+ homepage: https://github.com/Kimoja/uinit-sig
143
+ licenses:
144
+ - MIT
145
+ metadata:
146
+ homepage_uri: https://github.com/Kimoja/uinit-sig
147
+ allowed_push_host: https://rubygems.org
148
+ source_code_uri: https://github.com/Kimoja/uinit-sig
149
+ changelog_uri: https://github.com/Kimoja/uinit-sig/blob/main/CHANGELOG.md
150
+ bug_tracker_uri: https://github.com/Kimoja/uinit-sig/issues
151
+ rubygems_mfa_required: 'true'
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: 3.2.1
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubygems_version: 3.4.6
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: Signed methods
171
+ test_files: []