typed_struct 0.1.2 → 0.1.3
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/Gemfile.lock +1 -1
- data/lib/typed_struct/version.rb +1 -1
- data/lib/typed_struct.rb +16 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aee9975f17031f02fbf826c5d78c8d08c30027a890bbe90b53c8abc03d798501
|
4
|
+
data.tar.gz: 1b049a9ba9041053685eeced2db7dcdebc2b870fb1385911a32fccfb960bad21
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91ebd0411fb2ba31da1a04edbc5b550ebed30439c2480fb77bbaa25fa00b8baa40a8bf5e709574e6845c1a70ad8918c421894fa5cdabe0121679aa98dae904b2
|
7
|
+
data.tar.gz: 5b6be63355c328a74333869470e0bbf32101d5e117f661b85945f320c95d1a1427848d7d9c6063920985344956e9f13ee2f99e6570baab3075d5abf75da35388
|
data/Gemfile.lock
CHANGED
data/lib/typed_struct/version.rb
CHANGED
data/lib/typed_struct.rb
CHANGED
@@ -4,6 +4,10 @@ require_relative "typed_struct/version"
|
|
4
4
|
require_relative "typed_struct/type_checking"
|
5
5
|
require "rbs"
|
6
6
|
|
7
|
+
def Rbs(type_str)
|
8
|
+
RBS::Parser.parse_type(type_str)
|
9
|
+
end
|
10
|
+
|
7
11
|
class TypedStruct < Struct
|
8
12
|
include TypeChecking
|
9
13
|
|
@@ -13,8 +17,10 @@ class TypedStruct < Struct
|
|
13
17
|
# any methods which are able to be overridden
|
14
18
|
alias_method :__class__, :class
|
15
19
|
|
20
|
+
Options = nil
|
21
|
+
|
16
22
|
class << self
|
17
|
-
def new(**properties)
|
23
|
+
def new(opts = Options.new, **properties)
|
18
24
|
properties.each_key do |prop|
|
19
25
|
if method_defined?(prop)
|
20
26
|
$stdout.puts OVERRIDING_NATIVE_METHOD_MSG % [prop.inspect, caller(3).first]
|
@@ -28,7 +34,7 @@ class TypedStruct < Struct
|
|
28
34
|
end
|
29
35
|
|
30
36
|
klass.instance_eval do
|
31
|
-
@options = { types: properties }
|
37
|
+
@options = { types: properties, options: opts }
|
32
38
|
|
33
39
|
define_method :[]= do |key, val|
|
34
40
|
prop = properties[key]
|
@@ -51,17 +57,16 @@ class TypedStruct < Struct
|
|
51
57
|
|
52
58
|
def initialize(**attrs)
|
53
59
|
opts = self.__class__.options
|
54
|
-
opts[:types].
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
60
|
+
vals = opts[:types].to_h do |prop, expected_type|
|
61
|
+
value = attrs.fetch(prop, opts[:options][:default])
|
62
|
+
unless val_is_type? value, expected_type
|
63
|
+
raise "Unexpected type #{value.class} for #{prop.inspect} (expected #{expected_type})"
|
64
|
+
end
|
65
|
+
[prop, value]
|
59
66
|
end
|
60
67
|
|
61
|
-
super
|
68
|
+
super **vals
|
62
69
|
end
|
63
|
-
end
|
64
70
|
|
65
|
-
|
66
|
-
RBS::Parser.parse_type(type_str)
|
71
|
+
Options = TypedStruct.new({ default: nil }, default: Rbs("untyped"))
|
67
72
|
end
|