Class: Types::Enumerator

Inherits:
Object
  • Object
show all
Includes:
Generic
Defined in:
lib/types/enumerator.rb

Overview

Represents an enumerator type with a specific item type.

“‘ruby type = Types::Enumerator(Types::Integer) type.parse([1, 2, 3].each) # => Enumerator for [1, 2, 3] “`

Instance Method Summary collapse

Methods included from Generic

#|

Constructor Details

#initialize(item_type) ⇒ Enumerator

Returns a new instance of Enumerator.



20
21
22
# File 'lib/types/enumerator.rb', line 20

def initialize(item_type)
  @item_type = item_type
end

Instance Method Details

#composite?Boolean

Returns:



25
26
27
# File 'lib/types/enumerator.rb', line 25

def composite?
  true
end

#map(enumerator) ⇒ Object

Maps the given enumerator using the item type’s parse method.



32
33
34
# File 'lib/types/enumerator.rb', line 32

def map(enumerator)
  enumerator.map{|value| @item_type.parse(value)}
end

#parse(input) ⇒ Object

Parses the input as an enumerator with the specified item type.



40
41
42
43
44
45
46
47
48
49
# File 'lib/types/enumerator.rb', line 40

def parse(input)
  case input
  when ::Enumerator
    return input
  when ::Array
    return input.each
  else
    raise ArgumentError, "Cannot coerce #{input.inspect} into Enumerator!"
  end
end

#resolveObject

Resolves to the actual Ruby Enumerator class.



63
64
65
# File 'lib/types/enumerator.rb', line 63

def resolve
  ::Enumerator
end

#to_rbsObject



57
58
59
# File 'lib/types/enumerator.rb', line 57

def to_rbs
  "Enumerator[#{@item_type.to_rbs}]"
end

#to_sObject



52
53
54
# File 'lib/types/enumerator.rb', line 52

def to_s
  "Enumerator(#{@item_type})"
end