low_type 0.5.0 → 0.6.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/lib/low_type.rb +21 -17
- data/lib/redefiner.rb +3 -3
- data/lib/type_assignment.rb +54 -0
- data/lib/type_expression.rb +2 -2
- data/lib/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 84f89979651e711cb013a30df1a39f9becc619153f3fde1ac2f1810c6c0be9dc
|
|
4
|
+
data.tar.gz: ea670426a49377352c7b5503b897362c0aa1fbfef1b418d9908ba237386ae37c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5aeaefb6861e7500b60ecbca69192cbc0893b310e853730d876719e83adb7943cbc8cb3b8636f12a4e838da8bd4ef4aa503790a226230cc9ecd4473a1ba3faca
|
|
7
|
+
data.tar.gz: f4272f09bf0ee57dfd85df4effe1568eba8ff1e7e5136742a8f90c1735df2d32969af269c5269390da81a2c317155b1730f7a73278378ee0ad5d8402cf64f831
|
data/lib/low_type.rb
CHANGED
|
@@ -29,19 +29,6 @@ module LowType
|
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
class << klass
|
|
32
|
-
# Public API.
|
|
33
|
-
def type(expression)
|
|
34
|
-
# TODO: Runtime type expression for the supplied variable.
|
|
35
|
-
end
|
|
36
|
-
alias_method :low_type, :type
|
|
37
|
-
|
|
38
|
-
# Public API.
|
|
39
|
-
def value(expression)
|
|
40
|
-
::LowType.value(expression)
|
|
41
|
-
end
|
|
42
|
-
alias_method :low_value, :value
|
|
43
|
-
|
|
44
|
-
# Internal API.
|
|
45
32
|
def low_methods
|
|
46
33
|
@low_methods ||= {}
|
|
47
34
|
end
|
|
@@ -57,21 +44,38 @@ module LowType
|
|
|
57
44
|
Hash.define_singleton_method('[]', hash_class_method)
|
|
58
45
|
end
|
|
59
46
|
|
|
60
|
-
# Internal API.
|
|
61
47
|
class << self
|
|
48
|
+
# Public API.
|
|
49
|
+
|
|
50
|
+
def config
|
|
51
|
+
config = Struct.new(:type_assignment, :deep_type_check)
|
|
52
|
+
@config ||= config.new(false, false)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def configure
|
|
56
|
+
yield(config)
|
|
57
|
+
|
|
58
|
+
if config.type_assignment
|
|
59
|
+
require_relative 'type_assignment'
|
|
60
|
+
include TypeAssignment
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Internal API.
|
|
65
|
+
|
|
62
66
|
def file_path(klass:)
|
|
63
67
|
caller.find { |callee| callee.end_with?("<class:#{klass}>'") }.split(':').first
|
|
64
68
|
end
|
|
65
69
|
|
|
66
|
-
def type?(
|
|
67
|
-
|
|
70
|
+
def type?(type)
|
|
71
|
+
type.respond_to?(:new) || type == Integer || (type.is_a?(::Hash) && type.keys.first.respond_to?(:new) && type.values.first.respond_to?(:new))
|
|
68
72
|
end
|
|
69
73
|
|
|
70
74
|
def value?(expression)
|
|
71
75
|
!expression.respond_to?(:new) && expression != Integer
|
|
72
76
|
end
|
|
73
77
|
|
|
74
|
-
def value(type)
|
|
78
|
+
def value(type:)
|
|
75
79
|
TypeExpression.new(default_value: ValueExpression.new(value: type))
|
|
76
80
|
end
|
|
77
81
|
end
|
data/lib/redefiner.rb
CHANGED
|
@@ -114,9 +114,9 @@ module LowType
|
|
|
114
114
|
[required_args, required_kwargs]
|
|
115
115
|
end
|
|
116
116
|
|
|
117
|
-
#
|
|
118
|
-
def value(
|
|
119
|
-
|
|
117
|
+
# Value expressions are eval()'d in the context of this module class (the instance doesn't exist yet) so alias API.
|
|
118
|
+
def value(type)
|
|
119
|
+
LowType.value(type:)
|
|
120
120
|
end
|
|
121
121
|
end
|
|
122
122
|
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
require_relative 'type_expression'
|
|
2
|
+
require_relative 'value_expression'
|
|
3
|
+
|
|
4
|
+
module TypeAssignment
|
|
5
|
+
class AssignmentError < StandardError; end
|
|
6
|
+
|
|
7
|
+
def type(type_expression)
|
|
8
|
+
object = type_expression.default_value
|
|
9
|
+
|
|
10
|
+
if !LowType.value?(object)
|
|
11
|
+
raise AssignmentError, "Single-instance objects like #{object} are not supported"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
object.instance_variable_set('@type_expression', type_expression)
|
|
15
|
+
|
|
16
|
+
def object.with_type=(value)
|
|
17
|
+
type_expression = self.instance_variable_get('@type_expression')
|
|
18
|
+
type_expression.validate!(value:, name: self, error_type: TypeError, error_keyword: 'object')
|
|
19
|
+
|
|
20
|
+
# We can't re-assign self in Ruby so we re-assign instance variables instead.
|
|
21
|
+
value.instance_variables.each do |variable|
|
|
22
|
+
self.instance_variable_set(variable, value.instance_variable_get(variable))
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
self
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
return object.value if object.is_a?(ValueExpression)
|
|
29
|
+
|
|
30
|
+
object
|
|
31
|
+
end
|
|
32
|
+
alias_method :low_type, :type
|
|
33
|
+
|
|
34
|
+
def value(type)
|
|
35
|
+
LowType.value(type:)
|
|
36
|
+
end
|
|
37
|
+
alias_method :low_value, :value
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
module LowType
|
|
41
|
+
class Array < ::Array
|
|
42
|
+
def self.[](type)
|
|
43
|
+
return TypeExpression.new(type: [type]) if LowType.type?(type)
|
|
44
|
+
super
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
class Hash < ::Hash
|
|
49
|
+
def self.[](type)
|
|
50
|
+
return TypeExpression.new(type:) if LowType.type?(type)
|
|
51
|
+
super
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
data/lib/type_expression.rb
CHANGED
|
@@ -34,8 +34,8 @@ module LowType
|
|
|
34
34
|
@types.each do |type|
|
|
35
35
|
return true if LowType.type?(type) && type == value.class
|
|
36
36
|
# TODO: Shallow validation of enumerables could be made deeper with user config.
|
|
37
|
-
return true if type.class == Array && value.class == Array && type.first == value.first.class
|
|
38
|
-
if type.class == Hash && value.class == Hash && type.keys[0] == value.keys[0].class && type.values[0] == value.values[0].class
|
|
37
|
+
return true if type.class == ::Array && value.class == ::Array && type.first == value.first.class
|
|
38
|
+
if type.class == ::Hash && value.class == ::Hash && type.keys[0] == value.keys[0].class && type.values[0] == value.values[0].class
|
|
39
39
|
return true
|
|
40
40
|
end
|
|
41
41
|
end
|
data/lib/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: low_type
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- maedi
|
|
@@ -22,6 +22,7 @@ files:
|
|
|
22
22
|
- lib/param_proxy.rb
|
|
23
23
|
- lib/parser.rb
|
|
24
24
|
- lib/redefiner.rb
|
|
25
|
+
- lib/type_assignment.rb
|
|
25
26
|
- lib/type_expression.rb
|
|
26
27
|
- lib/value_expression.rb
|
|
27
28
|
- lib/version.rb
|
|
@@ -46,5 +47,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
46
47
|
requirements: []
|
|
47
48
|
rubygems_version: 3.7.2
|
|
48
49
|
specification_version: 4
|
|
49
|
-
summary:
|
|
50
|
+
summary: Elegant types in Ruby
|
|
50
51
|
test_files: []
|