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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b464562db650de5a4d3026ca8806d85b993c8f6bfcf3e86d1988c859432a427
4
- data.tar.gz: a6d38114357f8657b675074a36cbfba6129695eb80bc1b9333219f241cc44b79
3
+ metadata.gz: aee9975f17031f02fbf826c5d78c8d08c30027a890bbe90b53c8abc03d798501
4
+ data.tar.gz: 1b049a9ba9041053685eeced2db7dcdebc2b870fb1385911a32fccfb960bad21
5
5
  SHA512:
6
- metadata.gz: b280075f73bf18618ea3ab207b9d3f632928d69a364902b3374ab4b4a728ba791874247a7d36502fd4c83e00f980e4e35e7b777d8e80a40223377a207a03432c
7
- data.tar.gz: 9ba0515a1401a4be3d0feaa4bd79fe1dee3c7d20c1d84990485c0eb411df88df16d3bb86a5703f13fca71299e925c6b0e157932a1691efdb10987be536cd1d2d
6
+ metadata.gz: 91ebd0411fb2ba31da1a04edbc5b550ebed30439c2480fb77bbaa25fa00b8baa40a8bf5e709574e6845c1a70ad8918c421894fa5cdabe0121679aa98dae904b2
7
+ data.tar.gz: 5b6be63355c328a74333869470e0bbf32101d5e117f661b85945f320c95d1a1427848d7d9c6063920985344956e9f13ee2f99e6570baab3075d5abf75da35388
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- typed_struct (0.1.2)
4
+ typed_struct (0.1.3)
5
5
  rbs (~> 1.0)
6
6
 
7
7
  GEM
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class TypedStruct < Struct
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
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].each do |prop, expected_type|
55
- passed_value = attrs[prop]
56
- next if val_is_type? passed_value, expected_type
57
-
58
- raise "Unexpected type #{passed_value.class} for #{prop.inspect} (expected #{expected_type})"
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
- def Rbs(type_str)
66
- RBS::Parser.parse_type(type_str)
71
+ Options = TypedStruct.new({ default: nil }, default: Rbs("untyped"))
67
72
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typed_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Johansen