types 0.4.1 → 0.4.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
- checksums.yaml.gz.sig +0 -0
- data/lib/types/block.rb +2 -2
- data/lib/types/enumerator.rb +73 -0
- data/lib/types/hash.rb +2 -2
- data/lib/types/method.rb +2 -5
- data/lib/types/version.rb +1 -1
- data/lib/types.rb +1 -0
- data.tar.gz.sig +0 -0
- metadata +2 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e335bb59eee3f88c55549626aa4011a45dd37eab7942dee8ccbdf70e0fe5e7f
|
4
|
+
data.tar.gz: 357d9dee79c38053f3f9809cd342ff6bae2529d7b7e1cbe5a6fe17f5e8fd7cbb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 34065d6677004658191ede53a58ebd334c483841868c3f9b8abd1f176d8909fef1ed3c9cf67a4e29b44afa52a333fed63fc6ad8272dfc3078032fa6d810f1002
|
7
|
+
data.tar.gz: dee2f82b5dac9a2a31ad311f532f22c360d7dcf46c98c542048fd65451ade32007f6203c19371d7047cd3f3878778f032c07a5d6857a65b887d06b56c11dc934
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/lib/types/block.rb
CHANGED
@@ -42,12 +42,12 @@ module Types
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
# @returns [String] the RBS type string, e.g.
|
45
|
+
# @returns [String] the RBS type string, e.g. `^(String, Integer) -> String`.
|
46
46
|
def to_rbs
|
47
47
|
argument_types = @argument_types.map(&:to_rbs).join(", ")
|
48
48
|
return_type = @return_type ? @return_type.to_rbs : "void"
|
49
49
|
|
50
|
-
return "
|
50
|
+
return "^(#{argument_types}) -> #{return_type}"
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2022-2025, by Samuel Williams.
|
5
|
+
|
6
|
+
require_relative "generic"
|
7
|
+
|
8
|
+
module Types
|
9
|
+
# Represents an enumerator type with a specific item type.
|
10
|
+
#
|
11
|
+
# ```ruby
|
12
|
+
# type = Types::Enumerator(Types::Integer)
|
13
|
+
# type.parse([1, 2, 3].each) # => Enumerator for [1, 2, 3]
|
14
|
+
# ```
|
15
|
+
class Enumerator
|
16
|
+
include Generic
|
17
|
+
|
18
|
+
# @parameter item_type [Type] The type of the enumerator elements.
|
19
|
+
def initialize(item_type)
|
20
|
+
@item_type = item_type
|
21
|
+
end
|
22
|
+
|
23
|
+
# @returns [Boolean] true if this is a composite type.
|
24
|
+
def composite?
|
25
|
+
true
|
26
|
+
end
|
27
|
+
|
28
|
+
# Maps the given enumerator using the item type's parse method.
|
29
|
+
# @parameter enumerator [Enumerator] The enumerator to map.
|
30
|
+
# @returns [Enumerator] The mapped enumerator.
|
31
|
+
def map(enumerator)
|
32
|
+
enumerator.map{|value| @item_type.parse(value)}
|
33
|
+
end
|
34
|
+
|
35
|
+
# Parses the input as an enumerator with the specified item type.
|
36
|
+
# @parameter input [Object] The value to parse.
|
37
|
+
# @returns [Enumerator] The parsed enumerator.
|
38
|
+
# @raises [ArgumentError] if the input cannot be converted to an enumerator.
|
39
|
+
def parse(input)
|
40
|
+
case input
|
41
|
+
when ::Enumerator
|
42
|
+
return input
|
43
|
+
when ::Array
|
44
|
+
return input.each
|
45
|
+
else
|
46
|
+
raise ArgumentError, "Cannot coerce #{input.inspect} into Enumerator!"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# @returns [String] the string representation of the enumerator type.
|
51
|
+
def to_s
|
52
|
+
"Enumerator(#{@item_type})"
|
53
|
+
end
|
54
|
+
|
55
|
+
# @returns [String] the RBS type string, e.g. `Enumerator[String]`.
|
56
|
+
def to_rbs
|
57
|
+
"Enumerator[#{@item_type.to_rbs}]"
|
58
|
+
end
|
59
|
+
|
60
|
+
# Resolves to the actual Ruby Enumerator class.
|
61
|
+
# @returns [Class] The Enumerator class.
|
62
|
+
def resolve
|
63
|
+
::Enumerator
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Constructs an {Enumerator} type from the given item type.
|
68
|
+
# @parameter item_type [Type] The type of the enumerator elements.
|
69
|
+
# @returns [Enumerator] a new {Enumerator} type.
|
70
|
+
def self.Enumerator(item_type = Any)
|
71
|
+
Enumerator.new(item_type)
|
72
|
+
end
|
73
|
+
end
|
data/lib/types/hash.rb
CHANGED
@@ -47,9 +47,9 @@ module Types
|
|
47
47
|
"Hash(#{@key_type}, #{@value_type})"
|
48
48
|
end
|
49
49
|
|
50
|
-
# @returns [String] the RBS type string, e.g. `
|
50
|
+
# @returns [String] the RBS type string, e.g. `Hash[String, Integer]`.
|
51
51
|
def to_rbs
|
52
|
-
"
|
52
|
+
"Hash[#{@key_type.to_rbs}, #{@value_type.to_rbs}]"
|
53
53
|
end
|
54
54
|
|
55
55
|
# Resolves to the actual Ruby Hash class.
|
data/lib/types/method.rb
CHANGED
@@ -55,12 +55,9 @@ module Types
|
|
55
55
|
return buffer
|
56
56
|
end
|
57
57
|
|
58
|
-
# @returns [String] the RBS type string, e.g.
|
58
|
+
# @returns [String] the RBS type string, e.g. `::Method`.
|
59
59
|
def to_rbs
|
60
|
-
|
61
|
-
return_type = @return_type ? @return_type.to_rbs : "void"
|
62
|
-
|
63
|
-
return "Method[#{@receiver_type}, (#{argument_types}) -> #{return_type}]"
|
60
|
+
"Method"
|
64
61
|
end
|
65
62
|
|
66
63
|
# Parses the input as a method or proc.
|
data/lib/types/version.rb
CHANGED
data/lib/types.rb
CHANGED
@@ -11,6 +11,7 @@ require_relative "types/block"
|
|
11
11
|
require_relative "types/boolean"
|
12
12
|
require_relative "types/class"
|
13
13
|
require_relative "types/decimal"
|
14
|
+
require_relative "types/enumerator"
|
14
15
|
require_relative "types/float"
|
15
16
|
require_relative "types/hash"
|
16
17
|
require_relative "types/integer"
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: types
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
@@ -51,6 +51,7 @@ files:
|
|
51
51
|
- lib/types/boolean.rb
|
52
52
|
- lib/types/class.rb
|
53
53
|
- lib/types/decimal.rb
|
54
|
+
- lib/types/enumerator.rb
|
54
55
|
- lib/types/float.rb
|
55
56
|
- lib/types/generic.rb
|
56
57
|
- lib/types/hash.rb
|
metadata.gz.sig
CHANGED
Binary file
|