typed_struct 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c3375fc3fde1c42faa3d3ab97fd452cbe00bc869928d152a0ea67af139aab30d
4
+ data.tar.gz: b4981b8264cc69a678dca36c666ee10ee82371e451ba067e558334240621de6a
5
+ SHA512:
6
+ metadata.gz: e823becbc89695f9e7cd9e6a2c494750b5507ffd6cf6d638089007e4f1294950b140d73f4494e7edc592f1116389f4800312c406913767c2b53076e718dddb56
7
+ data.tar.gz: 6fd49203aec3eee88359bc7bb94dcc2782387c47c11f3bd4f8c53b20bfec76778061c8632575f526396795098e13d0833cbc23987a28a9667c0b72fd8785438f
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in typed_struct.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ typed_struct (0.1.0)
5
+ rbs (~> 1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.4.4)
11
+ rake (13.0.3)
12
+ rbs (1.1.1)
13
+ rspec (3.10.0)
14
+ rspec-core (~> 3.10.0)
15
+ rspec-expectations (~> 3.10.0)
16
+ rspec-mocks (~> 3.10.0)
17
+ rspec-core (3.10.1)
18
+ rspec-support (~> 3.10.0)
19
+ rspec-expectations (3.10.1)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.10.0)
22
+ rspec-mocks (3.10.2)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.10.0)
25
+ rspec-support (3.10.2)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ rake (~> 13.0)
32
+ rspec (~> 3.0)
33
+ typed_struct!
34
+
35
+ BUNDLED WITH
36
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Joseph Johansen
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,39 @@
1
+ # TypedStruct
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/typed_struct`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'typed_struct'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install typed_struct
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/typed_struct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "typed_struct"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "typed_struct/version"
4
+ require_relative "typed_struct/type_checking"
5
+ require "rbs"
6
+
7
+ class TypedStruct < Struct
8
+ include TypeChecking
9
+
10
+ class << self
11
+ def new(**properties)
12
+ super(*properties.keys, keyword_init: true).tap do |klass|
13
+ klass.class.instance_eval do
14
+ include TypeChecking
15
+ attr_reader :options
16
+ end
17
+
18
+ klass.instance_eval do
19
+ @options = { types: properties }
20
+ properties.each_key do |k|
21
+ define_method :"#{k}=" do |val|
22
+ prop = properties[k]
23
+ if val_is_type? val, prop
24
+ raise "Unexpected type #{val.class} for #{k.inspect} (expected #{prop})"
25
+ end
26
+ # it seems that structs behave in such a way that you can't call super here, but it
27
+ # will still set the value anyway
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ def initialize(**attrs)
36
+ opts = self.class.options
37
+ opts[:types].each do |prop, expected_type|
38
+ passed_value = attrs[prop]
39
+ next if val_is_type? passed_value, expected_type
40
+
41
+ raise "Unexpected type #{passed_value.class} for #{prop.inspect} (expected #{expected_type})"
42
+ end
43
+
44
+ super
45
+ end
46
+ end
47
+
48
+ def Rbs(type_str)
49
+ RBS::Parser.parse_type(type_str)
50
+ end
@@ -0,0 +1,152 @@
1
+ class TypedStruct < Struct
2
+ module TypeChecking
3
+ private
4
+
5
+ def val_is_type?(val, expected_type)
6
+ return val_is_rbs_type? val, expected_type if expected_type.class.to_s.start_with? "RBS::Types"
7
+
8
+ expected_type === val
9
+ end
10
+
11
+ def get_class(type_name)
12
+ (@const_cache ||= {})[type_name] ||= begin
13
+ Object.const_get(type_name.to_s)
14
+ rescue NameError
15
+ nil
16
+ end
17
+ end
18
+
19
+ def each_sample(array, &block)
20
+ if block
21
+ if sample_size && array.size > sample_size
22
+ if sample_size > 0
23
+ size = array.size
24
+ sample_size.times do
25
+ yield array[rand(size)]
26
+ end
27
+ end
28
+ else
29
+ array.each(&block)
30
+ end
31
+ else
32
+ enum_for :each_sample, array
33
+ end
34
+ end
35
+
36
+ def val_is_rbs_type?(val, type)
37
+ case type
38
+ when RBS::Types::Bases::Any
39
+ true
40
+ when RBS::Types::Bases::Bool
41
+ val.is_a?(TrueClass) || val.is_a?(FalseClass)
42
+ when RBS::Types::Bases::Top
43
+ true
44
+ when RBS::Types::Bases::Bottom
45
+ false
46
+ when RBS::Types::Bases::Void
47
+ true
48
+ when RBS::Types::Bases::Self
49
+ val_is_type? val, self.class
50
+ when RBS::Types::Bases::Nil
51
+ val_is_type? val, NilClass
52
+ when RBS::Types::Bases::Class
53
+ val_is_type? val, Class
54
+ when RBS::Types::Bases::Instance
55
+ val_is_type? val, Object
56
+ when RBS::Types::ClassInstance
57
+ klass = get_class(type.name) or return false
58
+ case
59
+ when klass == ::Array
60
+ val_is_type?(val, klass) && each_sample(val).all? { |v| val_is_type?(v, type.args[0]) }
61
+ when klass == ::Hash
62
+ val_is_type?(val, klass) && each_sample(val.keys).all? do |key|
63
+ val_is_type?(key, type.args[0]) && val_is_type?(val[key], type.args[1])
64
+ end
65
+ when klass == ::Range
66
+ val_is_type?(val, klass) && val_is_type?(val.begin, type.args[0]) && val_is_type?(val.end, type.args[0])
67
+ when klass == ::Enumerator
68
+ if val_is_type? val, klass
69
+ case val.size
70
+ when Float::INFINITY
71
+ values = []
72
+ ret = self
73
+ val.lazy.take(10).each do |*args|
74
+ values << args
75
+ nil
76
+ end
77
+ else
78
+ values = []
79
+ ret = val.each do |*args|
80
+ values << args
81
+ nil
82
+ end
83
+ end
84
+
85
+ each_sample(values).all? do |v|
86
+ if v.size == 1
87
+ # Only one block argument.
88
+ val_is_type?(v[0], type.args[0]) || value(v, type.args[0])
89
+ else
90
+ val_is_type?(v, type.args[0])
91
+ end
92
+ end &&
93
+ if ret.equal?(self)
94
+ type.args[1].is_a?(Types::Bases::Bottom)
95
+ else
96
+ val_is_type?(ret, type.args[1])
97
+ end
98
+ end
99
+ else
100
+ val_is_type? val, klass
101
+ end
102
+ when RBS::Types::ClassSingleton
103
+ klass = get_class(type.name) or return false
104
+ singleton_class = begin
105
+ klass.singleton_class
106
+ rescue TypeError
107
+ return false
108
+ end
109
+ val.is_a?(singleton_class)
110
+ when RBS::Types::Interface
111
+ methods = Set.new(val.methods)
112
+ if (definition = builder.build_interface(type.name))
113
+ definition.methods.each_key.all? do |method_name|
114
+ methods.member?(method_name)
115
+ end
116
+ end
117
+ when RBS::Types::Variable
118
+ true
119
+ when RBS::Types::Literal
120
+ val == type.literal
121
+ when RBS::Types::Union
122
+ type.types.any? { |type| val_is_type?(val, type) }
123
+ when RBS::Types::Intersection
124
+ type.types.all? { |type| val_is_type?(val, type) }
125
+ when RBS::Types::Optional
126
+ val_is_type?(val, NilClass) || val_is_type?(val, type.type)
127
+ when RBS::Types::Alias
128
+ val_is_type? val, builder.expand_alias(type.name)
129
+ when RBS::Types::Tuple
130
+ val_is_type?(val, Array) &&
131
+ type.types.map.with_index { |ty, index| val_is_type?(val[index], ty) }.all?
132
+ when RBS::Types::Record
133
+ val_is_type?(val, Hash) &&
134
+ type.fields.map { |key, type| val_is_type?(val[key], type) }.all?
135
+ when RBS::Types::Proc
136
+ val_is_type? val, Proc
137
+ else
138
+ false
139
+ end
140
+ end
141
+
142
+ def builder
143
+ @builder ||= RBS::DefinitionBuilder.new(
144
+ env: RBS::Environment.from_loader(RBS::EnvironmentLoader.new).resolve_type_names,
145
+ )
146
+ end
147
+
148
+ def sample_size
149
+ 100
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class TypedStruct < Struct
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/typed_struct/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "typed_struct"
7
+ spec.version = TypedStruct::VERSION
8
+ spec.authors = ["Joseph Johansen"]
9
+ spec.email = ["joe@stotles.com"]
10
+
11
+ spec.summary = "Lightweight and flexible typed structs"
12
+ spec.description = "All the flexibility of a Ruby Struct, but with type checking on its properties. Also benefit from being able to define complex types using RBS type notation."
13
+ spec.homepage = "https://github.com/johansenja/typed_struct"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
26
+ end
27
+ spec.require_paths = ["lib"]
28
+
29
+ # Uncomment to register a new dependency of your gem
30
+ spec.add_dependency "rbs", "~> 1.0"
31
+
32
+ # For more information and examples about making a new gem, checkout our
33
+ # guide at: https://bundler.io/guides/creating_gem.html
34
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: typed_struct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joseph Johansen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rbs
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description: All the flexibility of a Ruby Struct, but with type checking on its properties.
28
+ Also benefit from being able to define complex types using RBS type notation.
29
+ email:
30
+ - joe@stotles.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - ".rspec"
37
+ - Gemfile
38
+ - Gemfile.lock
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - bin/console
43
+ - bin/setup
44
+ - lib/typed_struct.rb
45
+ - lib/typed_struct/type_checking.rb
46
+ - lib/typed_struct/version.rb
47
+ - typed_struct.gemspec
48
+ homepage: https://github.com/johansenja/typed_struct
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ allowed_push_host: https://rubygems.org
53
+ homepage_uri: https://github.com/johansenja/typed_struct
54
+ source_code_uri: https://github.com/johansenja/typed_struct
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.4.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.1.2
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Lightweight and flexible typed structs
74
+ test_files: []