thy 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: dede6c0b3036ff543a4dc62bc816a9e94ffd0ce16cf369a0910f47a32cc7507c
4
+ data.tar.gz: ef0219b2e4feccaf91914ecdab59c021d98aae2a28a483d760e75aacd197ab4a
5
+ SHA512:
6
+ metadata.gz: f6ce5558293a911bbae3b06da8901b3c9b8a39fbe5012fc57e7093a0c1a6d0fe801bf0148084b83d325a32bdca65a2d2c11c32af05e25fe552f116404c137d55
7
+ data.tar.gz: d344e64a85a03895171bfd76c1b3360222cf75109e16e6248f011250567f71aca6220fe8ca5844fb809e574d15e4e4b2a5fdbaf4b689ca5ae21a6d8274e0fdd8
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/.rubocop.yml ADDED
@@ -0,0 +1,26 @@
1
+ inherit_gem:
2
+ armitage-rubocop:
3
+ - lib/rubocop.general.yml
4
+ - lib/rubocop.rspec.yml
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 2.6.3
8
+ Include:
9
+ - bin/console
10
+ - lib/**/*.rb
11
+ - spec/**/*.rb
12
+ - Gemfile
13
+ - Rakefile
14
+ - thy.gemspec
15
+
16
+ Style/TrailingCommaInArguments:
17
+ EnforcedStyleForMultiline: comma
18
+
19
+ Style/TrailingCommaInArrayLiteral:
20
+ EnforcedStyleForMultiline: comma
21
+
22
+ Style/TrailingCommaInHashLiteral:
23
+ EnforcedStyleForMultiline: comma
24
+
25
+ Style/MultipleComparison:
26
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in thy.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,70 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ thy (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ armitage-rubocop (0.70.0.110)
10
+ rubocop (= 0.70.0)
11
+ rubocop-performance (= 1.3.0)
12
+ rubocop-rails (= 2.0.0)
13
+ rubocop-rspec (= 1.33.0)
14
+ ast (2.4.0)
15
+ coderay (1.1.2)
16
+ diff-lcs (1.3)
17
+ jaro_winkler (1.5.2)
18
+ method_source (0.9.2)
19
+ parallel (1.17.0)
20
+ parser (2.6.3.0)
21
+ ast (~> 2.4.0)
22
+ pry (0.12.2)
23
+ coderay (~> 1.1.0)
24
+ method_source (~> 0.9.0)
25
+ rack (2.0.7)
26
+ rainbow (3.0.0)
27
+ rake (10.5.0)
28
+ rspec (3.8.0)
29
+ rspec-core (~> 3.8.0)
30
+ rspec-expectations (~> 3.8.0)
31
+ rspec-mocks (~> 3.8.0)
32
+ rspec-core (3.8.0)
33
+ rspec-support (~> 3.8.0)
34
+ rspec-expectations (3.8.3)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.8.0)
37
+ rspec-mocks (3.8.0)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.8.0)
40
+ rspec-support (3.8.0)
41
+ rubocop (0.70.0)
42
+ jaro_winkler (~> 1.5.1)
43
+ parallel (~> 1.10)
44
+ parser (>= 2.6)
45
+ rainbow (>= 2.2.2, < 4.0)
46
+ ruby-progressbar (~> 1.7)
47
+ unicode-display_width (>= 1.4.0, < 1.7)
48
+ rubocop-performance (1.3.0)
49
+ rubocop (>= 0.68.0)
50
+ rubocop-rails (2.0.0)
51
+ rack (>= 2.0)
52
+ rubocop (>= 0.70.0)
53
+ rubocop-rspec (1.33.0)
54
+ rubocop (>= 0.60.0)
55
+ ruby-progressbar (1.10.1)
56
+ unicode-display_width (1.6.0)
57
+
58
+ PLATFORMS
59
+ ruby
60
+
61
+ DEPENDENCIES
62
+ armitage-rubocop
63
+ bundler (~> 2.0)
64
+ pry
65
+ rake (~> 10.0)
66
+ rspec (~> 3.0)
67
+ thy!
68
+
69
+ BUNDLED WITH
70
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Alexander Komarov
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,175 @@
1
+ # Thy [![Build Status](https://travis-ci.org/akxcv/thy.svg?branch=master)](https://travis-ci.org/akxcv/thy)
2
+
3
+ Thy is a minimal, strict runtime type system for Ruby.
4
+
5
+ ## Motivation
6
+
7
+ There are many existing runtime type systems for Ruby, most notably, dry-types. Here's why you might
8
+ prefer Thy:
9
+
10
+ - Thy encourages strictness. Avoiding type coercion as much as possible is an excellent way to keep
11
+ your code predictable and straightforward.
12
+ - Minimalism. Thy's API only has what's needed, nothing more.
13
+ - No Any type. Don't worry, if you *have to*, you [can get it](#custom-types)!
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'thy'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install thy
30
+
31
+ ## Usage
32
+
33
+ It's recommended not to use Thy directly (although [you can](#direct-usage)), but to create a module
34
+ for working with types, and to include `Thy::Types` into it.
35
+
36
+ ```ruby
37
+ module Types
38
+ include Thy::Types
39
+ end
40
+ ```
41
+
42
+ To run a type check, call `check` on a type you need. For example:
43
+
44
+ ```ruby
45
+ module Types
46
+ include Thy::Types
47
+ end
48
+
49
+ Types::String.check('a string').success? # => true
50
+ Types::Integer.check(3.14).success? # => false
51
+ ```
52
+
53
+ If a check fails, a message describing the failure is provided:
54
+
55
+ ```ruby
56
+ Types::String.check(3).failure? # => true
57
+ Types::String.check(3).message # => "Expected a String, but got: 3"
58
+ ```
59
+
60
+ ### Custom types
61
+
62
+ Thy allows you to define custom types via `Thy::Type`'s constructor:
63
+
64
+ ```ruby
65
+ module Types
66
+ include Thy::Types
67
+
68
+ NonZeroValue = Thy::Type.new { |value| value != 0 }
69
+ end
70
+ ```
71
+
72
+ #### Providing meaningful error messages
73
+
74
+ ```ruby
75
+ module Types
76
+ include Thy::Types
77
+
78
+ NonZeroValue = Thy::Type.new do |value|
79
+ value == 0 || Thy::Result::Failure.new("Expected #{value.inspect} to be nonzero")
80
+ end
81
+ end
82
+ ```
83
+
84
+ #### Building on top of existing types
85
+
86
+ Extending existing types is simple, too:
87
+
88
+ ```ruby
89
+ module Types
90
+ include Thy::Types
91
+
92
+ NonZeroInteger = Thy.refine_type(Integer, Thy::Type.new { |v| v != 0 })
93
+ end
94
+ ```
95
+
96
+ ### Direct usage
97
+
98
+ You can use Thy types directly, for example, if you'd like to play around in the console:
99
+
100
+ ```ruby
101
+ Thy::String.check('Bob').success? # => true
102
+ PositiveInteger = Thy.refine_type(Integer, Thy::Type.new { |v| v > 0 })
103
+ ```
104
+
105
+ ## Built-in type reference
106
+
107
+ ### Primitive types
108
+
109
+ ```ruby
110
+ # String
111
+ Thy::String.check('Bob').success? # => true
112
+
113
+ # Symbol
114
+ Thy::Symbol.check(:bob).success? # => true
115
+
116
+ # Numeric
117
+ Thy::Numeric.check(3.14).success? # => true
118
+ Thy::Numeric.check(0).success? # => true
119
+
120
+ # Float
121
+ Thy::Float.check(3.14).success? # => true
122
+
123
+ # Integer
124
+ Thy::Integer.check(3).success? # => true
125
+
126
+ # Boolean
127
+ Thy::Boolean.check(true).success? # => true
128
+ Thy::Boolean.check(false).success? # => true
129
+ ```
130
+
131
+ ### Parameterized types
132
+
133
+ ```ruby
134
+ # Array
135
+ Thy::Array(Thy::Integer).check([1, 2, 3]).success? # => true
136
+ Thy::Array(Thy::String).check(%w[hi hello sup]).success? # => true
137
+
138
+ # Hash
139
+ Thy::Hash({
140
+ name: Thy::String,
141
+ age: Thy::Integer,
142
+ }).check({ name: 'Bob', age: 18 }).success? # => true
143
+
144
+ # Map
145
+ Thy::Map(Thy::Symbol, Thy::Integer).check({ a: 1, b: 2 }).success? # => true
146
+
147
+ # Enum
148
+ Thy::Enum(Thy::Integer, Thy::Float).check(3.14).success? # => true
149
+ Thy::Enum(Thy::Integer, Thy::Float).check(3).success? # => true
150
+
151
+ # Variant
152
+ Thy::Variant('USD', 'EUR').check('EUR').success? # => true
153
+
154
+ # Option
155
+ Thy::Option(Thy::String).check('a string').success? # => true
156
+ Thy::Option(Thy::String).check(nil).success? # => true
157
+ ```
158
+
159
+ ### Other types
160
+
161
+ ```ruby
162
+ # UntypedArray
163
+ Thy::UntypedArray.check(1, 'string', 3.14).success? # => true
164
+
165
+ # UntypedHash
166
+ Thy::UntypedHash.check({ a: 1, b: 'c', 1 => true }).success? # => true
167
+ ```
168
+
169
+ ## Contributing
170
+
171
+ Bug reports and pull requests are welcome on GitHub at https://github.com/akxcv/thy.
172
+
173
+ ## License
174
+
175
+ 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,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'thy'
6
+
7
+ require 'pry'
8
+ Pry.start
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
data/lib/thy.rb ADDED
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thy/type'
4
+
5
+ require 'thy/types/string'
6
+ require 'thy/types/symbol'
7
+ require 'thy/types/numeric'
8
+ require 'thy/types/integer'
9
+ require 'thy/types/float'
10
+ require 'thy/types/boolean'
11
+
12
+ require 'thy/types/array'
13
+ require 'thy/types/untyped_array'
14
+
15
+ require 'thy/types/hash'
16
+ require 'thy/types/untyped_hash'
17
+ require 'thy/types/map'
18
+
19
+ require 'thy/types/enum'
20
+ require 'thy/types/variant'
21
+
22
+ require 'thy/types/option'
23
+
24
+ require 'thy/result/success'
25
+ require 'thy/result/failure'
26
+
27
+ module Thy
28
+ UntypedArray = Types::UntypedArray
29
+ UntypedHash = Types::UntypedHash
30
+ String = Types::String
31
+ Symbol = Types::Symbol
32
+ Numeric = Types::Numeric
33
+ Float = Types::Float
34
+ Integer = Types::Integer
35
+ Boolean = Types::Boolean
36
+
37
+ class << self
38
+ def refine_type(type_1, type_2)
39
+ Type.new do |value|
40
+ check_1 = type_1.check(value)
41
+ if check_1.success?
42
+ type_2.check(value)
43
+ else
44
+ check_1
45
+ end
46
+ end
47
+ end
48
+
49
+ # rubocop:disable Naming/MethodName
50
+ def Array(type)
51
+ Types::Array.new(type)
52
+ end
53
+
54
+ def Hash(schema)
55
+ Types::Hash.new(schema)
56
+ end
57
+
58
+ def Map(key_type, value_type)
59
+ Types::Map.new(key_type, value_type)
60
+ end
61
+
62
+ def Enum(*types)
63
+ Types::Enum.new(types)
64
+ end
65
+
66
+ def Variant(*values)
67
+ Types::Variant.new(values)
68
+ end
69
+
70
+ def Option(type)
71
+ Types::Option.new(type)
72
+ end
73
+ # rubocop:enable Naming/MethodName
74
+ end
75
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ module Result
5
+ class Failure
6
+ attr_reader :message
7
+
8
+ def initialize(message)
9
+ @message = message
10
+ end
11
+
12
+ def success?
13
+ false
14
+ end
15
+
16
+ def failure?
17
+ true
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ module Result
5
+ module Success
6
+ def self.success?
7
+ true
8
+ end
9
+
10
+ def self.failure?
11
+ false
12
+ end
13
+ end
14
+ end
15
+ end
data/lib/thy/type.rb ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ class Type
5
+ def initialize(&checker)
6
+ @checker = checker
7
+ end
8
+
9
+ def check(value)
10
+ check_result = @checker.call(value)
11
+
12
+ return Result::Success if check_result == true
13
+ return Result::Failure.new('No message provided') if check_result == false
14
+
15
+ check_result
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ module Types
5
+ class Array
6
+ def initialize(type)
7
+ @type = type
8
+ end
9
+
10
+ def check(values)
11
+ unless values.is_a?(::Array)
12
+ return Result::Failure.new("Expected an array, but got #{values.inspect}")
13
+ end
14
+
15
+ values.each do |value|
16
+ if @type.check(value).failure?
17
+ return Result::Failure.new(
18
+ "Expected an array of #{@type.inspect}, but got element: #{value.inspect}",
19
+ )
20
+ end
21
+ end
22
+
23
+ Result::Success
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ module Types
5
+ module Boolean
6
+ def self.check(value)
7
+ if value == true || value == false
8
+ Result::Success
9
+ else
10
+ Result::Failure.new("Expected a boolean value, but got: #{value.inspect}")
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ module Types
5
+ class Enum
6
+ def initialize(values)
7
+ @values = values
8
+ end
9
+
10
+ def check(value)
11
+ if @values.any? { |v| value == v }
12
+ Result::Success
13
+ else
14
+ Result::Failure.new("Expected #{value.inspect} to be one of: #{@values.inspect}")
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ module Types
5
+ module Float
6
+ def self.check(value)
7
+ if value.is_a?(::Float)
8
+ Result::Success
9
+ else
10
+ Result::Failure.new("Expected a Float, but got: #{value.inspect}")
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ module Types
5
+ class Hash
6
+ def initialize(schema)
7
+ @schema = schema
8
+ @schema_length = schema.length
9
+ @schema_keys = schema.keys
10
+ end
11
+
12
+ def check(value)
13
+ unless value.is_a?(::Hash)
14
+ return Result::Failure.new("Expected a Hash, but got: #{value.inspect}")
15
+ end
16
+ if @schema_length != value.length
17
+ return Result::Failure.new("Expected #{value.inspect} to contain #{@schema_length} keys")
18
+ end
19
+ if @schema_keys != value.keys
20
+ return Result::Failure.new(
21
+ "Expected #{value.inspect} to contain keys: #{@schema_keys.inspect}",
22
+ )
23
+ end
24
+
25
+ @schema.each do |k, t|
26
+ v = value.fetch(k)
27
+
28
+ if t.check(v).failure?
29
+ return Result::Failure.new("Expected #{v.inspect} to be of type #{t.inspect}")
30
+ end
31
+ end
32
+
33
+ Result::Success
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ module Types
5
+ module Integer
6
+ def self.check(value)
7
+ if value.is_a?(::Integer)
8
+ Result::Success
9
+ else
10
+ Result::Failure.new("Expected an Integer, but got: #{value.inspect}")
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ module Types
5
+ class Map
6
+ def initialize(key_type, value_type)
7
+ @key_type = key_type
8
+ @value_type = value_type
9
+ end
10
+
11
+ def check(value)
12
+ unless value.is_a?(::Hash)
13
+ return Result::Failure.new("Expected a Hash, but got: #{value.inspect}")
14
+ end
15
+
16
+ value.keys.each do |k|
17
+ if @key_type.check(k).failure?
18
+ return Result::Failure.new(
19
+ "Expected key #{k.inspect} to be of type #{@key_type.inspect}",
20
+ )
21
+ end
22
+ end
23
+
24
+ value.values.each do |v|
25
+ if @value_type.check(v).failure?
26
+ return Result::Failure.new(
27
+ "Expected value #{v.inspect} to be of type #{@value_type.inspect}",
28
+ )
29
+ end
30
+ end
31
+
32
+ Result::Success
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ module Types
5
+ module Numeric
6
+ def self.check(value)
7
+ if value.is_a?(::Numeric)
8
+ Result::Success
9
+ else
10
+ Result::Failure.new("Expected a Numeric, but got: #{value.inspect}")
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ module Types
5
+ class Option
6
+ def initialize(type)
7
+ @type = type
8
+ end
9
+
10
+ def check(value)
11
+ return Result::Success if value.nil?
12
+
13
+ if @type.check(value).success?
14
+ Result::Success
15
+ else
16
+ Result::Failure.new("Expected #{value.inspect} to be of type #{@type.inspect}")
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ module Types
5
+ module String
6
+ def self.check(value)
7
+ if value.is_a?(::String)
8
+ Result::Success
9
+ else
10
+ Result::Failure.new("Expected a String, but got: #{value.inspect}")
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ module Types
5
+ module Symbol
6
+ def self.check(value)
7
+ if value.is_a?(::Symbol)
8
+ Result::Success
9
+ else
10
+ Result::Failure.new("Expected a Symbol, but got: #{value.inspect}")
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ module Types
5
+ module UntypedArray
6
+ def self.check(value)
7
+ if value.is_a?(::Array)
8
+ Result::Success
9
+ else
10
+ Result::Failure.new("Expected an Array, but got: #{value.inspect}")
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ module Types
5
+ module UntypedHash
6
+ def self.check(value)
7
+ if value.is_a?(::Hash)
8
+ Result::Success
9
+ else
10
+ Result::Failure.new("Expected a Hash, but got: #{value.inspect}")
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thy
4
+ module Types
5
+ class Variant
6
+ def initialize(types)
7
+ @types = types
8
+ end
9
+
10
+ def check(value)
11
+ if @types.any? { |t| t.check(value).success? }
12
+ Result::Success
13
+ else
14
+ Result::Failure.new("Expected #{value.inspect} to be within types: #{@types.inspect}")
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
data/thy.gemspec ADDED
@@ -0,0 +1,29 @@
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
+ Gem::Specification.new do |spec|
7
+ spec.name = 'thy'
8
+ spec.version = '0.1.0'
9
+ spec.authors = ['Alexander Komarov <ak@akxcv.com>']
10
+ spec.email = ['ak@akxcv.com']
11
+
12
+ spec.summary = 'A minimal, strict runtime type system'
13
+ spec.description = '@see summary'
14
+ spec.homepage = 'https://github.com/akxcv/thy'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 2.0'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_development_dependency 'armitage-rubocop'
28
+ spec.add_development_dependency 'pry'
29
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Komarov <ak@akxcv.com>
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-09-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: armitage-rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
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
+ description: "@see summary"
84
+ email:
85
+ - ak@akxcv.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/thy.rb
102
+ - lib/thy/result/failure.rb
103
+ - lib/thy/result/success.rb
104
+ - lib/thy/type.rb
105
+ - lib/thy/types/array.rb
106
+ - lib/thy/types/boolean.rb
107
+ - lib/thy/types/enum.rb
108
+ - lib/thy/types/float.rb
109
+ - lib/thy/types/hash.rb
110
+ - lib/thy/types/integer.rb
111
+ - lib/thy/types/map.rb
112
+ - lib/thy/types/numeric.rb
113
+ - lib/thy/types/option.rb
114
+ - lib/thy/types/string.rb
115
+ - lib/thy/types/symbol.rb
116
+ - lib/thy/types/untyped_array.rb
117
+ - lib/thy/types/untyped_hash.rb
118
+ - lib/thy/types/variant.rb
119
+ - thy.gemspec
120
+ homepage: https://github.com/akxcv/thy
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubygems_version: 3.0.3
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: A minimal, strict runtime type system
143
+ test_files: []