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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ba6a881d71e9cc56f38d4e6fa2d7508b2f884f33768de92842c97e2160a6955
4
- data.tar.gz: fcf5e669cb9e7d92a8a64a47bf74a1fb7a2504686da5bb2f5ab234239275ec21
3
+ metadata.gz: 6e335bb59eee3f88c55549626aa4011a45dd37eab7942dee8ccbdf70e0fe5e7f
4
+ data.tar.gz: 357d9dee79c38053f3f9809cd342ff6bae2529d7b7e1cbe5a6fe17f5e8fd7cbb
5
5
  SHA512:
6
- metadata.gz: '02978795e5d7bd65e5c7ecd54beff026802c3d9d142986a63eb3aa833db3db85f83849a9b97524ce78daa3c65952a27af9739bc48a26af65286b3f559f41a53f'
7
- data.tar.gz: 633f75598b51ec5c36bc7b5c1498bbd78dd2b5ff0c722e633df8591a66647d1edb151980461e0b4906633bdb7910154a2adf8443a1b582ab93571d99c29cd3ac
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. `Proc[(String, Integer) -> String]`.
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 "Proc[(#{argument_types}) -> #{return_type}]"
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. `{ String => Integer }`.
50
+ # @returns [String] the RBS type string, e.g. `Hash[String, Integer]`.
51
51
  def to_rbs
52
- "{ #{@key_type.to_rbs} => #{@value_type.to_rbs} }"
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. `Method[Receiver, (Args) -> Return]`.
58
+ # @returns [String] the RBS type string, e.g. `::Method`.
59
59
  def to_rbs
60
- argument_types = @argument_types.map(&:to_rbs).join(", ")
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
@@ -4,5 +4,5 @@
4
4
  # Copyright, 2022-2025, by Samuel Williams.
5
5
 
6
6
  module Types
7
- VERSION = "0.4.1"
7
+ VERSION = "0.4.3"
8
8
  end
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.1
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